formify 0.18.2 → 0.21.4
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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f09550331400ffb9b540f2be6bc0228eade98c38352374cdf8e841579e528293
|
4
|
+
data.tar.gz: 10722046e10c1cc0260aa6d7e1a97df1c3c6413ffb5acef283a73eac24557f07
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 89da3a1e960d23ed6693f1a1eae3a538280cad97d41aca420592cdc60431a9c9a17969058fb3baab323570c5483bf1e3a34572e0033b2073b8a4ab576641dd87
|
7
|
+
data.tar.gz: 6b73750147ba2353700dc2c66c714cae5503bcb2c87f1129a96582d6ad0f422dea78ab7f128d13079fe2b6b4f91659a49e4d2e3fe95a6e53d6c2a8028aeb64f5
|
data/Gemfile.lock
CHANGED
data/lib/formify/version.rb
CHANGED
@@ -47,9 +47,7 @@ class FormGenerator < Rails::Generators::NamedBase
|
|
47
47
|
private
|
48
48
|
|
49
49
|
def all_attributes
|
50
|
-
@all_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
|
-
|
65
|
-
|
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
|
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
|
-
|
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
|
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.<%=
|
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
|
-
|
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 { <%=
|
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 <%=
|
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 %>) {
|
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.
|
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-
|
11
|
+
date: 2019-06-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|