formify 0.18.2 → 0.21.4

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: 19a6baa487ba12e6ff7c6a49bd3406673517279eeb4579bf7f47995e3982c684
4
- data.tar.gz: 4b9666f6b57a47605326591f34157f2d389732c77099203e08860338b4ff5f27
3
+ metadata.gz: f09550331400ffb9b540f2be6bc0228eade98c38352374cdf8e841579e528293
4
+ data.tar.gz: 10722046e10c1cc0260aa6d7e1a97df1c3c6413ffb5acef283a73eac24557f07
5
5
  SHA512:
6
- metadata.gz: 2e18be345eb6ffc2f0eb0f6e2ad1ec23f6e06080149c01d90c4968816349aeadbd8de657d0ffb13529c9165b56247b5b01ad7800322922e8d653140b08741984
7
- data.tar.gz: 5480c9f399b71d80730c72d4114e6c8b9c620888b37199f98351881cde0977c3451293d863e4d7db93ef4034fa4d2c37c0c86812638e54fce93b4b65f635f230
6
+ metadata.gz: 89da3a1e960d23ed6693f1a1eae3a538280cad97d41aca420592cdc60431a9c9a17969058fb3baab323570c5483bf1e3a34572e0033b2073b8a4ab576641dd87
7
+ data.tar.gz: 6b73750147ba2353700dc2c66c714cae5503bcb2c87f1129a96582d6ad0f422dea78ab7f128d13079fe2b6b4f91659a49e4d2e3fe95a6e53d6c2a8028aeb64f5
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- formify (0.18.2)
4
+ formify (0.21.4)
5
5
  rails
6
6
  resonad
7
7
  with_advisory_lock
@@ -1,3 +1,3 @@
1
1
  module Formify
2
- VERSION = '0.18.2'.freeze
2
+ VERSION = '0.21.4'.freeze
3
3
  end
@@ -47,9 +47,7 @@ class FormGenerator < Rails::Generators::NamedBase
47
47
  private
48
48
 
49
49
  def all_attributes
50
- @all_attributes ||= form_attributes
51
- .map { |e| e.split(/:|,/).map(&method(:variablefy)) }
52
- .flatten
50
+ @all_attributes ||= flattened_form_attributes
53
51
  .map(&:strip)
54
52
  .sort
55
53
  end
@@ -61,8 +59,8 @@ class FormGenerator < Rails::Generators::NamedBase
61
59
  def attributes_and_delegates
62
60
  @attributes_and_delegates ||= Hash[
63
61
  form_attributes
64
- .map { |e| e.split(/:|,/).map(&method(:variablefy)) }
65
- .collect { |v| [v[0], v[1..-1]] }
62
+ .map { |e| e.split(/:|,/).map(&method(:variablefy)) }
63
+ .collect { |v| [v[0], v[1..-1]] }
66
64
  ]
67
65
  end
68
66
 
@@ -75,12 +73,29 @@ class FormGenerator < Rails::Generators::NamedBase
75
73
  end
76
74
 
77
75
  def create?
78
- form.downcase.include?('create')
76
+ form == 'create'
77
+ end
78
+
79
+ def create?
80
+ form == 'create'
79
81
  end
80
82
 
81
83
  def delegated_attributes
82
84
  @delegated_attributes ||= attributes_and_delegates
83
- .select { |_k, v| v.sort!.present? }
85
+ .select { |_k, v| v.sort!.present? }
86
+ end
87
+ def destroy_attribute
88
+ @destroy_attribute ||= begin
89
+ if all_attributes.include?(collection.singularize)
90
+ collection.singularize
91
+ else
92
+ first_attribute
93
+ end
94
+ end
95
+ end
96
+
97
+ def destroy?
98
+ form == 'destroy'
84
99
  end
85
100
 
86
101
  def duplicate_attributes
@@ -102,8 +117,19 @@ class FormGenerator < Rails::Generators::NamedBase
102
117
  @factories ||= FactoryBot.factories.map(&:name)
103
118
  end
104
119
 
120
+ def first_attribute
121
+ @first_attribute ||= flattened_form_attributes.first
122
+ end
123
+
124
+ def flattened_form_attributes
125
+ @flattened_form_attributes ||= form_attributes
126
+ .map { |e| e.split(/:|,/) }
127
+ .flatten
128
+ .map(&method(:variablefy))
129
+ end
130
+
105
131
  def form
106
- @form ||= name.split('/').last.to_s.underscore
132
+ @form ||= variablefy(name.split('/').last)
107
133
  end
108
134
 
109
135
  def form_name
@@ -114,6 +140,42 @@ class FormGenerator < Rails::Generators::NamedBase
114
140
  @inferred_model_name ||= collection.singularize.camelcase
115
141
  end
116
142
 
143
+ def lock_key
144
+ if create?
145
+ [":#{collection}", ':create']
146
+ elsif update?
147
+ [":#{collection}", method_attribute.to_s]
148
+ elsif upsert?
149
+ [":#{collection}", method_attribute.to_s]
150
+ else
151
+ ['LOCK_KEY']
152
+ end
153
+ end
154
+
155
+ def method_attribute
156
+ @method_attribute ||= begin
157
+ if all_attributes.include?(collection.singularize)
158
+ collection.singularize
159
+ else
160
+ first_attribute
161
+ end
162
+ end
163
+ end
164
+
165
+ def method_name
166
+ if create?
167
+ "create_#{collection.singularize}"
168
+ elsif update?
169
+ "update_#{collection.singularize}"
170
+ elsif upsert?
171
+ "upsert_#{collection.singularize}"
172
+ elsif form.include?('_')
173
+ form
174
+ else
175
+ "#{form}_#{collection.singularize}"
176
+ end
177
+ end
178
+
117
179
  def module_namespacing(&block)
118
180
  content = capture(&block)
119
181
  modules.reverse.each do |mod|
@@ -166,6 +228,24 @@ class FormGenerator < Rails::Generators::NamedBase
166
228
  end
167
229
  end
168
230
 
231
+ def update?
232
+ form == 'update'
233
+ end
234
+
235
+ def upsert?
236
+ form == 'upsert'
237
+ end
238
+
239
+ def upsert_delegates
240
+ @upsert_delegates ||= begin
241
+ if delegated_attributes.key?(method_attribute)
242
+ delegated_attributes[method_attribute]
243
+ else
244
+ []
245
+ end
246
+ end
247
+ end
248
+
169
249
  def variablefy(val)
170
250
  val.to_s.underscore
171
251
  end
@@ -28,27 +28,37 @@ class <%= form_name %>
28
28
 
29
29
  initialize_with :<%= attributes_with_delegates.join(', :') %> do |attributes|
30
30
  <% if create? -%>
31
- self.<%= collection.singularize %> ||= <%= inferred_model_name %>.new
32
-
31
+ self.<%= method_attribute %> ||= <%= inferred_model_name %>.new
32
+ <% elsif upsert? -%>
33
+ self.<%= method_attribute %> ||= <%= inferred_model_name %>.find_or_initialize_by(
34
+ <% upsert_delegates.each_with_index do |attr, i| -%>
35
+ <%= attr %>: attributes[:<%= attr %>]<%= ',' unless upsert_delegates.count - 2 == i %>
33
36
  <% end -%>
37
+ )
38
+ <% else -%>
34
39
  puts attributes
40
+ <% end -%>
35
41
  end
36
42
 
37
43
  def save
38
- raise NotImplementedError
39
-
40
- with_advisory_lock_transaction(:foo) do
44
+ with_advisory_lock_transaction(<%= lock_key.join(', ') %>) do
41
45
  validate_or_fail
42
- .and_then { <%= name.split('/').reverse[0..1].map(&:singularize).join('_').underscore %> }
46
+ .and_then { <%= method_name %> }
43
47
  .and_then { success(<%= return_attribute %>) }
44
48
  end
45
49
  end
46
50
 
47
51
  private
48
52
 
49
- def <%= name.split('/').reverse[0..1].map(&:singularize).join('_').underscore %>
53
+ def <%= method_name %>
54
+ <% if create? || upsert? || update? -%>
55
+ <%= method_attribute %>.save!
56
+ <% elsif destroy? -%>
57
+ <%= method_attribute %>.destroy!
58
+ <% else -%>
50
59
  raise NotImplementedError
51
60
 
61
+ <% end -%>
52
62
  success(<%= return_attribute %>)
53
63
  end
54
64
 
@@ -8,7 +8,7 @@ describe Forms::<%= class_name %>, type: :form do
8
8
 
9
9
  <% all_attributes.each do |attr| -%>
10
10
  <% if factory_bot? && factory?(attr) -%>
11
- let(:<%= attr %>) { FacotryBot.create(<%= attr %>) }
11
+ let(:<%= attr %>) { FactoryBot.create(:<%= attr %>) }
12
12
  <% else -%>
13
13
  let(:<%= attr %>) { <%= attr.upcase %>_VALUE }
14
14
  <% end -%>
@@ -27,9 +27,9 @@ describe Forms::<%= class_name %>, type: :form do
27
27
  }
28
28
  end
29
29
 
30
- it { expect_valid }<% if spec_comments? -%> # Expect the form to be valid<% end -%>
30
+ it { expect_valid }<% if spec_comments? -%> # Expect the form to be valid<% end %>
31
31
  it { expect(result).to be_success }
32
- it { expect(value).to be_a(<%= inferred_model_name %>) }<% if spec_comments? -%> # Model name inferred<% end -%>
32
+ it { expect(value).to be_a(<%= inferred_model_name %>) }<% if spec_comments? -%> # Model name inferred<% end %>
33
33
 
34
34
  <% all_attributes.each do |attr| -%>
35
35
  describe '#<%= attr %>' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: formify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.18.2
4
+ version: 0.21.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Jackson
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-05-25 00:00:00.000000000 Z
11
+ date: 2019-06-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler