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 +4 -4
- data/.gitignore +2 -0
- data/Gemfile.lock +1 -1
- data/lib/formify/form.rb +2 -0
- data/lib/formify/version.rb +1 -1
- data/lib/generators/form/form_generator.rb +51 -0
- data/lib/generators/form/templates/form.rb.tt +22 -14
- data/lib/generators/form/templates/form_spec.rb.tt +24 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1bd463cb45e56a78846363c509874052ea0b0e172c8dfd4b7015f652dce998c7
|
4
|
+
data.tar.gz: ead9810fb43a0bdad43f919221ed2c964aef6e26fa032898b029ae4998b1e6aa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c8f31a0487e61fcc35ece263d92f90737b0e63e7defb194730a80fb1005695438378cf5b8c8b649715a819a94aa36b81c8fd31b5a552959c456cfc81f220505a
|
7
|
+
data.tar.gz: 7df77eb9274c738e10cc9301cc798a3a815656d18aee60887ea2feb538682f49227608e9e8a917db9e96e975fe7e87325d684f17aba705bd980a820539d90cf6
|
data/.gitignore
CHANGED
data/Gemfile.lock
CHANGED
data/lib/formify/form.rb
CHANGED
data/lib/formify/version.rb
CHANGED
@@ -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
|
-
|
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
|
-
|
10
|
-
|
11
|
-
|
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
|
-
|
14
|
-
|
15
|
-
|
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
|
-
|
20
|
-
|
21
|
-
|
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 {
|
29
|
-
.and_then { success(
|
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
|
-
|
36
|
-
|
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(
|
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
|