formify 0.14.1 → 0.15.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e8817447649c9d1d84e70b0dc42c9bc885015b308f770590b7f259d080d17515
4
- data.tar.gz: df7070b4016f81f3a4dde7aeb9f03e4f09a316791f0e67eaa144aa3a073d4d3f
3
+ metadata.gz: 1bd463cb45e56a78846363c509874052ea0b0e172c8dfd4b7015f652dce998c7
4
+ data.tar.gz: ead9810fb43a0bdad43f919221ed2c964aef6e26fa032898b029ae4998b1e6aa
5
5
  SHA512:
6
- metadata.gz: e2562f47d7a925553d905ed031c104ecc80e031693c729c9ba8c06e2661ea7bc53c0504bb075c242c341637eb1885353eb0e54444f8ce68206d362f310fea614
7
- data.tar.gz: 2f28a0259aa0ba2b75e3b33b0101c1d93a23733fba71bd8c4435094197f213b74f8c583aec7b87fd826dd6437495ac0827fb45ad466f7f886e8a02709044ff34
6
+ metadata.gz: c8f31a0487e61fcc35ece263d92f90737b0e63e7defb194730a80fb1005695438378cf5b8c8b649715a819a94aa36b81c8fd31b5a552959c456cfc81f220505a
7
+ data.tar.gz: 7df77eb9274c738e10cc9301cc798a3a815656d18aee60887ea2feb538682f49227608e9e8a917db9e96e975fe7e87325d684f17aba705bd980a820539d90cf6
data/.gitignore CHANGED
@@ -48,3 +48,5 @@ build-iPhoneSimulator/
48
48
 
49
49
  # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
50
50
  .rvmrc
51
+
52
+ .rspec_status
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- formify (0.14.1)
4
+ formify (0.15.0)
5
5
  rails
6
6
  resonad
7
7
 
data/lib/formify/form.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'active_support/concern'
4
+
3
5
  module Formify
4
6
  module Form
5
7
  extend ActiveSupport::Concern
@@ -1,3 +1,3 @@
1
1
  module Formify
2
- VERSION = '0.14.1'.freeze
2
+ VERSION = '0.15.0'.freeze
3
3
  end
@@ -6,6 +6,17 @@ class FormGenerator < Rails::Generators::NamedBase
6
6
  source_root File.expand_path('templates', __dir__)
7
7
  # check_class_collision
8
8
 
9
+ argument :form_attributes,
10
+ type: :array,
11
+ required: true,
12
+ description: 'A list of attributes and their delegates: foo:bar,baz'
13
+
14
+ def validate_fixed_attrs
15
+ return unless duplicate_attributes.any?
16
+
17
+ raise "Cannot have duplicate attributes: #{duplicate_attributes.join(', ')}"
18
+ end
19
+
9
20
  def generate_form
10
21
  template 'form.rb', File.join('app/lib/forms', class_path, "#{file_name}.rb")
11
22
  end
@@ -16,6 +27,42 @@ class FormGenerator < Rails::Generators::NamedBase
16
27
 
17
28
  private
18
29
 
30
+ def all_attributes
31
+ @all_attributes ||= form_attributes
32
+ .map { |e| e.split(/:|,/).map(&method(:variablefy)) }
33
+ .flatten
34
+ .map(&:strip)
35
+ .sort
36
+ end
37
+
38
+ def attributes
39
+ @attributes ||= attributes_and_delegates.keys.sort
40
+ end
41
+
42
+ def attributes_and_delegates
43
+ @attributes_and_delegates ||= Hash[
44
+ form_attributes
45
+ .map { |e| e.split(/:|,/).map(&method(:variablefy)) }
46
+ .collect { |v| [v[0], v[1..-1]] }
47
+ ]
48
+ end
49
+
50
+ def delegated_attributes
51
+ @delegated_attributes ||= attributes_and_delegates
52
+ .select { |_k, v| v.sort!.present? }
53
+ end
54
+
55
+ def duplicate_attributes
56
+ @duplicate_attributes ||= all_attributes
57
+ .group_by { |e| e }
58
+ .select { |_k, v| v.size > 1 }
59
+ .map(&:first)
60
+ end
61
+
62
+ def inferred_model_name
63
+ @inferred_model_name ||= name.split('/')[-2].singularize.camelcase
64
+ end
65
+
19
66
  def module_namespacing(&block)
20
67
  content = capture(&block)
21
68
  modules.reverse.each do |mod|
@@ -28,6 +75,10 @@ class FormGenerator < Rails::Generators::NamedBase
28
75
  @modules ||= ['Forms'] + name.split('/')[0..-2].map(&:to_s).map(&:camelcase)
29
76
  end
30
77
 
78
+ def variablefy(val)
79
+ val.to_s.underscore
80
+ end
81
+
31
82
  def wrap_with_module(content, mod)
32
83
  content = indent(content).chomp
33
84
  "module #{mod}\n#{content}\nend\n"
@@ -4,36 +4,44 @@
4
4
  class <%= name.split('/').last.to_s.camelcase %>
5
5
  include Formify::Form
6
6
 
7
- # attr_accessor :fixed_attr
7
+ attr_accessor :<%= attributes.first %><%= ',' if attributes.count > 1 %>
8
+ <% attributes[1..-1].each_with_index do |attr, i| -%>
9
+ :<%= attr %><%= ',' unless attributes.count - 2 == i %>
10
+ <% end -%>
8
11
 
9
- # delegate_accessor :bar
10
- # :baz,
11
- # to: :fixed_attr
12
+ <% delegated_attributes.each do |to_attr, delegates| -%>
13
+ delegate_accessor :<%= delegates.first %>,
14
+ <% delegates[1..-1].each_with_index do |attr, i| -%>
15
+ :<%= attr %>,
16
+ <% end -%>
17
+ to: :<%= to_attr %>
18
+ <% end -%>
12
19
 
13
- # validates_presence_of :foo
14
- # :bar,
15
- # :baz
20
+ validates_presence_of :<%= all_attributes.first %><%= ',' if all_attributes.count > 1 %>
21
+ <% all_attributes[1..-1].each_with_index do |attr, i| -%>
22
+ :<%= attr %><%= ',' unless all_attributes.count - 2 == i %>
23
+ <% end -%>
16
24
 
17
25
  # validate :validate_something
18
26
 
19
- # initialize_with :fixed_attr do |attributes|
20
- # puts attributes
21
- # end
27
+ initialize_with :<%= attributes.join(', :') %> do |attributes|
28
+ puts attributes
29
+ end
22
30
 
23
31
  def save
24
32
  raise NotImplementedError
25
33
 
26
34
  with_advisory_lock_transaction(:foo) do
27
35
  validate_or_fail
28
- .and_then { form_method }
29
- .and_then { success(fixed_attr) }
36
+ .and_then { <%= name.split('/').reverse[0..1].map(&:singularize).join('_').underscore %> }
37
+ .and_then { success(<%= attributes.first %>) }
30
38
  end
31
39
  end
32
40
 
33
41
  private
34
42
 
35
- # def form_method
36
- # end
43
+ def <%= name.split('/').reverse[0..1].map(&:singularize).join('_').underscore %>
44
+ end
37
45
 
38
46
  # def validate_something
39
47
  # end
@@ -6,12 +6,34 @@ require 'formify/spec_helpers'
6
6
  describe Forms::<%= class_name %>, type: :form do
7
7
  include Formify::SpecHelpers
8
8
 
9
+ # :attributes is used to initialize the form.
10
+ # These values should result in a valid form.
11
+ # You can override these in blocks or use let(:attributes_override) { { foo: bar } }
9
12
  let(:attributes) do
10
13
  {
11
-
14
+ <% all_attributes.each_with_index do |attr, i| -%>
15
+ <%= attr %>: <%= attr.upcase %>_VALUE<%= ',' unless all_attributes.count == (i + 1)%>
16
+ <% end -%>
12
17
  }
13
18
  end
14
19
 
20
+ it { expect_valid } # Expect the form to be valid
15
21
  it { expect(result).to be_success }
16
- it { expect(value).to be_a(KLASS) }
22
+ it { expect(value).to be_a(<%= inferred_model_name %>) } # Model name inferred
23
+
24
+ ### START: Attribute Expectations
25
+
26
+ <% all_attributes.each do |attr| -%>
27
+ # Attribute: :<%= attr %>
28
+ it { expect_error_with_missing_attribute(:<%= attr %>) }
29
+ xit { expect_error_with_attribute_value(:<%= attr %>, <%= attr.upcase %>_BAD_VALUE, message: nil) } # :message is optional
30
+ xit { expect_valid_with_attribute_value(:<%= attr %>, <%= attr.upcase %>_GOOD_VALUE) }
31
+
32
+ <% end -%>
33
+ ### END: Attribute Expectations
34
+
35
+ # Other Expectation Helpers
36
+ # xit { expect_error_message(message) }
37
+ # xit { expect_error_with_attribute(attribute) }
38
+ # xit { expect_not_valid(attribute: nil, message: nil) } # :attribute and :message are optional
17
39
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: formify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.14.1
4
+ version: 0.15.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Jackson