action_form 0.6.5 → 0.6.6
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 +4 -4
- data/lib/action_form/base.rb +1 -3
- data/lib/action_form/elements_dsl.rb +0 -3
- data/lib/action_form/rails/base.rb +0 -12
- data/lib/action_form/schema_dsl.rb +26 -28
- data/lib/action_form/subforms_collection.rb +1 -1
- data/lib/action_form/version.rb +1 -1
- 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: a30578d111d84b9c748248c82a65eac75389b0696f27dbb3a36a3c437b5643e3
|
|
4
|
+
data.tar.gz: 2da98bb16f9368a0f07a70d1c47f904b80421f584c024ae69ea18db009f23bf3
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 8c9a05b8349c2072c787d95102fd047e6a067e4d367b79c4284c326c75abc2cd05c1a09b38c41dac2d52536cdbb737c927b4f9231a38472cfda66690ed82085f
|
|
7
|
+
data.tar.gz: e654910d6f00232ee6995955f48c2089edc70de5151c07ccb247b48758b1ed5c2ad6f3306807b37ac3008d9e0bc722c6ebb7ca1b208ea49aa105d448143133d5
|
data/lib/action_form/base.rb
CHANGED
|
@@ -27,9 +27,7 @@ module ActionForm
|
|
|
27
27
|
def inherited(subclass)
|
|
28
28
|
super
|
|
29
29
|
subclass.elements = elements.dup
|
|
30
|
-
subclass.scope = scope
|
|
31
|
-
subclass.params_definition = Class.new(params_definition)
|
|
32
|
-
subclass.params_definition.form_class = subclass
|
|
30
|
+
subclass.scope = scope&.dup
|
|
33
31
|
end
|
|
34
32
|
end
|
|
35
33
|
|
|
@@ -24,7 +24,6 @@ module ActionForm
|
|
|
24
24
|
|
|
25
25
|
# TODO: add support for outputless elements
|
|
26
26
|
def element(name, &block)
|
|
27
|
-
@params_definition = nil
|
|
28
27
|
elements[name] = Class.new(ActionForm::Element)
|
|
29
28
|
elements[name].class_eval(&block)
|
|
30
29
|
define_singleton_method(:"#{name}_element") do |klass = nil, &block|
|
|
@@ -33,7 +32,6 @@ module ActionForm
|
|
|
33
32
|
end
|
|
34
33
|
|
|
35
34
|
def many(name, default: nil, &block)
|
|
36
|
-
@params_definition = nil
|
|
37
35
|
subform_definition = Class.new(ActionForm::SubformsCollection)
|
|
38
36
|
subform_definition.host_class = self
|
|
39
37
|
subform_definition.class_eval(&block) if block
|
|
@@ -45,7 +43,6 @@ module ActionForm
|
|
|
45
43
|
end
|
|
46
44
|
|
|
47
45
|
def subform(name, default: nil, &block)
|
|
48
|
-
@params_definition = nil
|
|
49
46
|
elements[name] = Class.new(subform_class)
|
|
50
47
|
elements[name].class_eval(&block)
|
|
51
48
|
elements[name].default = default if default
|
|
@@ -50,18 +50,6 @@ module ActionForm
|
|
|
50
50
|
end
|
|
51
51
|
end
|
|
52
52
|
|
|
53
|
-
def params_definition(scope: self.scope)
|
|
54
|
-
return super unless scope
|
|
55
|
-
|
|
56
|
-
@params_definitions ||= Hash.new do |h, key|
|
|
57
|
-
h[key] = begin
|
|
58
|
-
klass = super
|
|
59
|
-
Class.new(self.class.params_class) { has scope, klass }
|
|
60
|
-
end
|
|
61
|
-
end
|
|
62
|
-
@params_definitions[scope]
|
|
63
|
-
end
|
|
64
|
-
|
|
65
53
|
private
|
|
66
54
|
|
|
67
55
|
def subform_html_name(name, index: nil)
|
|
@@ -17,14 +17,27 @@ module ActionForm
|
|
|
17
17
|
@params_definition ||= create_params_definition
|
|
18
18
|
end
|
|
19
19
|
|
|
20
|
+
def params_blocks
|
|
21
|
+
@params_blocks ||= []
|
|
22
|
+
end
|
|
23
|
+
|
|
20
24
|
def params(&block)
|
|
21
|
-
|
|
25
|
+
params_blocks << block if block
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def inherited_params_blocks
|
|
29
|
+
parent = superclass
|
|
30
|
+
blocks = []
|
|
31
|
+
while parent.respond_to?(:params_blocks)
|
|
32
|
+
parent.params_blocks.each { |block| blocks.unshift(block) }
|
|
33
|
+
parent = parent.superclass
|
|
34
|
+
end
|
|
35
|
+
blocks
|
|
22
36
|
end
|
|
23
37
|
|
|
24
|
-
def create_params_definition # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
|
38
|
+
def create_params_definition(elements_definitions: elements) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
|
25
39
|
schema = Class.new(params_class)
|
|
26
|
-
|
|
27
|
-
elements.each do |name, element_definition|
|
|
40
|
+
elements_definitions.each do |name, element_definition|
|
|
28
41
|
if element_definition < ActionForm::SubformsCollection
|
|
29
42
|
# nested forms are passed as a hash that looks like this:
|
|
30
43
|
# { "0" => { "id" => "1" }, "1" => { "id" => "2" } }
|
|
@@ -44,40 +57,25 @@ module ActionForm
|
|
|
44
57
|
schema.public_send(method_name, name, **options)
|
|
45
58
|
end
|
|
46
59
|
end
|
|
60
|
+
patches = inherited_params_blocks + params_blocks
|
|
61
|
+
patches.each do |block|
|
|
62
|
+
schema = Class.new(schema, &block)
|
|
63
|
+
end
|
|
64
|
+
schema.form_class = self
|
|
47
65
|
schema
|
|
48
66
|
end
|
|
49
67
|
end
|
|
50
68
|
|
|
51
69
|
module InstanceMethods # rubocop:disable Style/Documentation
|
|
52
|
-
def params_definition
|
|
70
|
+
def params_definition
|
|
53
71
|
@params_definition ||= create_params_definition
|
|
54
72
|
end
|
|
55
73
|
|
|
56
|
-
def create_params_definition
|
|
74
|
+
def create_params_definition
|
|
57
75
|
schema = Class.new(self.class.params_class)
|
|
58
76
|
schema.form_class = self.class
|
|
59
|
-
elements_instances.select(&:render?).
|
|
60
|
-
|
|
61
|
-
when ActionForm::SubformsCollection
|
|
62
|
-
# nested forms are passed as a hash that looks like this:
|
|
63
|
-
# { "0" => { "id" => "1" }, "1" => { "id" => "2" } }
|
|
64
|
-
# it is coercing to an array of hashes:
|
|
65
|
-
# [['0', { "id" => "1" }], ['1', { "id" => "2" }]]
|
|
66
|
-
# we need to normalize it to an array of hashes:
|
|
67
|
-
# [ { "id" => "1" }, { "
|
|
68
|
-
# id" => "2" } ]
|
|
69
|
-
schema.each(:"#{element.name}_attributes", element.subforms.first.params_definition,
|
|
70
|
-
normalize: ->(value) { value.flatten.select { |v| v.is_a?(Hash) } },
|
|
71
|
-
default: element.class.default)
|
|
72
|
-
when ActionForm::Subform
|
|
73
|
-
schema.has(:"#{element.name}_attributes", element.params_definition, default: element.class.default)
|
|
74
|
-
when ActionForm::Element
|
|
75
|
-
options = element.class.output_options.dup
|
|
76
|
-
method_name = options.delete(:type)
|
|
77
|
-
schema.public_send(method_name, element.name, **options)
|
|
78
|
-
end
|
|
79
|
-
end
|
|
80
|
-
schema
|
|
77
|
+
renderable_elements = elements_instances.select(&:render?).to_h { |element| [element.name, element.class] }
|
|
78
|
+
self.class.create_params_definition(elements_definitions: renderable_elements)
|
|
81
79
|
end
|
|
82
80
|
end
|
|
83
81
|
end
|
|
@@ -22,7 +22,7 @@ module ActionForm
|
|
|
22
22
|
|
|
23
23
|
def inherited(subclass)
|
|
24
24
|
super
|
|
25
|
-
subclass.subform_definition = subform_definition
|
|
25
|
+
subclass.subform_definition = Class.new(subform_definition) if subform_definition
|
|
26
26
|
subclass.default = default
|
|
27
27
|
subclass.host_class = host_class
|
|
28
28
|
end
|
data/lib/action_form/version.rb
CHANGED