formify 0.16.0 → 0.17.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '09b226440478b6b89edbee6d137934281da2c347fd48681e5b205eb4039e0582'
4
- data.tar.gz: 85399692540ed00fe2f26f88273f300a9a7bdaa1a7c82c95102f3a59b89d8aa2
3
+ metadata.gz: e6d85707d12681f8344a9b5a8a12fae50ed194fe5c051dcbe6c1ccfc143df843
4
+ data.tar.gz: a6a565b5acfc240738049529103d90beabdd99f10b841265eb080e32afb50105
5
5
  SHA512:
6
- metadata.gz: 6646b9167f6ab241864284b64e88b3ec851e4fa7a4c00f08597e53e32bfbdc1a50a084ef65152bfa1ed9796c51f74f276652da618195f4189797fe92784de282
7
- data.tar.gz: 3af2ea3e9492d8cd839b2198bb96153fa18b71ce4890e40407cfea4b1a07b3741d3a8d0130e4c94599db6868e502f433adc9350f93fdf4d9f95c0b626f28a626
6
+ metadata.gz: 7a72509aadd70c77d3d8f23f1ad7c5148c5022658ce2c82db2d1b0020a8d4b36519c2dcaa958721d51aa24e70b6d474b3a24bc5b81a9c8e399888dc8b326f1c3
7
+ data.tar.gz: 37398a9a1fb561d7d9e9c2467dfdf9d5b61ea4295e3f92803a4a9df1d222a66bacd620aad597a3168c450cdf61c2d0e3433c248b8a9ff51038b2f23618d6489f
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- formify (0.16.0)
4
+ formify (0.17.7)
5
5
  rails
6
6
  resonad
7
7
  with_advisory_lock
data/README.md CHANGED
@@ -122,42 +122,36 @@ describe Forms::Widgets::Create, type: :form do
122
122
  it { expect(value).to be_a(Widget) } # Model name inferred
123
123
 
124
124
  context '#bar' do
125
- # Attribute: :bar
126
125
  it { expect_error_with_missing_attribute(:bar) }
127
126
  xit { expect_error_with_attribute_value(:bar, BAR_BAD_VALUE, message: nil) } # :message is optional
128
127
  xit { expect_valid_with_attribute_value(:bar, BAR_GOOD_VALUE) }
129
128
  end
130
129
 
131
130
  context '#baz' do
132
- # Attribute: :baz
133
131
  it { expect_error_with_missing_attribute(:baz) }
134
132
  xit { expect_error_with_attribute_value(:baz, BAZ_BAD_VALUE, message: nil) } # :message is optional
135
133
  xit { expect_valid_with_attribute_value(:baz, BAZ_GOOD_VALUE) }
136
134
  end
137
135
 
138
136
  context '#created_by' do
139
- # Attribute: :created_by
140
137
  it { expect_error_with_missing_attribute(:created_by) }
141
138
  xit { expect_error_with_attribute_value(:created_by, CREATED_BY_BAD_VALUE, message: nil) } # :message is optional
142
139
  xit { expect_valid_with_attribute_value(:created_by, CREATED_BY_GOOD_VALUE) }
143
140
  end
144
141
 
145
142
  context '#foo' do
146
- # Attribute: :foo
147
143
  it { expect_error_with_missing_attribute(:foo) }
148
144
  xit { expect_error_with_attribute_value(:foo, FOO_BAD_VALUE, message: nil) } # :message is optional
149
145
  xit { expect_valid_with_attribute_value(:foo, FOO_GOOD_VALUE) }
150
146
  end
151
147
 
152
148
  context '#owner' do
153
- # Attribute: :owner
154
149
  it { expect_error_with_missing_attribute(:owner) }
155
150
  xit { expect_error_with_attribute_value(:owner, OWNER_BAD_VALUE, message: nil) } # :message is optional
156
151
  xit { expect_valid_with_attribute_value(:owner, OWNER_GOOD_VALUE) }
157
152
  end
158
153
 
159
154
  context '#owner_attribute' do
160
- # Attribute: :owner_attribute
161
155
  it { expect_error_with_missing_attribute(:owner_attribute) }
162
156
  xit { expect_error_with_attribute_value(:owner_attribute, OWNER_ATTRIBUTE_BAD_VALUE, message: nil) } # :message is optional
163
157
  xit { expect_valid_with_attribute_value(:owner_attribute, OWNER_ATTRIBUTE_GOOD_VALUE) }
@@ -1,3 +1,3 @@
1
1
  module Formify
2
- VERSION = '0.16.0'.freeze
2
+ VERSION = '0.17.7'.freeze
3
3
  end
@@ -49,8 +49,11 @@ class FormGenerator < Rails::Generators::NamedBase
49
49
  .sort
50
50
  end
51
51
 
52
- def attributes
53
- @attributes ||= attributes_and_delegates.keys.sort
52
+ def attributes_with_delegates
53
+ @attributes_with_delegates ||= attributes_and_delegates
54
+ .select { |k, v| v.present? }
55
+ .keys
56
+ .sort
54
57
  end
55
58
 
56
59
  def attributes_and_delegates
@@ -69,6 +72,10 @@ class FormGenerator < Rails::Generators::NamedBase
69
72
  @collection_name ||= collection.camelcase
70
73
  end
71
74
 
75
+ def create?
76
+ form.downcase.include?('create')
77
+ end
78
+
72
79
  def delegated_attributes
73
80
  @delegated_attributes ||= attributes_and_delegates
74
81
  .select { |_k, v| v.sort!.present? }
@@ -109,6 +116,16 @@ class FormGenerator < Rails::Generators::NamedBase
109
116
  @pluralize_collection ||= options[:pluralize_collection]
110
117
  end
111
118
 
119
+ def return_attribute
120
+ @return_attribute ||= begin
121
+ if all_attributes.include?(collection.singularize)
122
+ collection.singularize
123
+ else
124
+ form_attributes.split(':').first
125
+ end
126
+ end
127
+ end
128
+
112
129
  def scopes
113
130
  @scopes ||= split_name[0..-3].map(&:underscore)
114
131
  end
@@ -4,9 +4,9 @@
4
4
  class <%= form_name %>
5
5
  include Formify::Form
6
6
 
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 %>
7
+ attr_accessor :<%= attributes_with_delegates.first %><%= ',' if attributes_with_delegates.count > 1 %>
8
+ <% attributes_with_delegates[1..-1].each_with_index do |attr, i| -%>
9
+ :<%= attr %><%= ',' unless attributes_with_delegates.count - 2 == i %>
10
10
  <% end -%>
11
11
 
12
12
  <% delegated_attributes.each do |to_attr, delegates| -%>
@@ -17,6 +17,8 @@ class <%= form_name %>
17
17
  to: :<%= to_attr %>
18
18
  <% end -%>
19
19
 
20
+ before_validation :before_validation_do_something
21
+
20
22
  validates_presence_of :<%= all_attributes.first %><%= ',' if all_attributes.count > 1 %>
21
23
  <% all_attributes[1..-1].each_with_index do |attr, i| -%>
22
24
  :<%= attr %><%= ',' unless all_attributes.count - 2 == i %>
@@ -24,7 +26,11 @@ class <%= form_name %>
24
26
 
25
27
  # validate :validate_something
26
28
 
27
- initialize_with :<%= attributes.join(', :') %> do |attributes|
29
+ initialize_with :<%= attributes_with_delegates.join(', :') %> do |attributes|
30
+ <% if create? -%>
31
+ self.<%= collection.singularize %> ||= <%= inferred_model_name %>.new
32
+
33
+ <% end -%>
28
34
  puts attributes
29
35
  end
30
36
 
@@ -34,15 +40,21 @@ class <%= form_name %>
34
40
  with_advisory_lock_transaction(:foo) do
35
41
  validate_or_fail
36
42
  .and_then { <%= name.split('/').reverse[0..1].map(&:singularize).join('_').underscore %> }
37
- .and_then { success(<%= attributes.first %>) }
43
+ .and_then { success(<%= return_attribute %>) }
38
44
  end
39
45
  end
40
46
 
41
47
  private
42
48
 
43
49
  def <%= name.split('/').reverse[0..1].map(&:singularize).join('_').underscore %>
50
+ raise NotImplementedError
51
+
52
+ success(<%= return_attribute %>)
44
53
  end
45
54
 
55
+ # def before_validation_do_something
56
+ # end
57
+
46
58
  # def validate_something
47
59
  # end
48
60
  end
@@ -23,7 +23,6 @@ describe Forms::<%= class_name %>, type: :form do
23
23
 
24
24
  <% all_attributes.each do |attr| -%>
25
25
  context '#<%= attr %>' do
26
- # Attribute: :<%= attr %>
27
26
  it { expect_error_with_missing_attribute(:<%= attr %>) }
28
27
  xit { expect_error_with_attribute_value(:<%= attr %>, <%= attr.upcase %>_BAD_VALUE, message: nil) } # :message is optional
29
28
  xit { expect_valid_with_attribute_value(:<%= attr %>, <%= attr.upcase %>_GOOD_VALUE) }
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.16.0
4
+ version: 0.17.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Jackson