action_form 0.2.0 → 0.2.1
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/rails/base.rb +12 -0
- data/lib/action_form/schema_dsl.rb +29 -0
- 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: e18c2983eaed84aa8e1ed222fe8b3fc52b8513d599f33cdbc707a19f7b413967
|
|
4
|
+
data.tar.gz: 9d0728634b329a2e92058ce7ae692d1efc59ccf43228920a48d9f42943b9f21c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 8d323f6dc47519a8ecca04bd88bf9e00bed2a4a677b65a6b44406e1d9a87e2854c1adc69825cf0815334483d396be47ca1dd709c729b1446d0bc186642d90c9f
|
|
7
|
+
data.tar.gz: 944a0b89f57c888d53fb6fe4e19f1caa43ed9f4a4e5b8b1611070b0f3afb456d3e8d5c1affb32c2347d0e802909be4ad2177e3152243b99c6c818c86a73d6691
|
|
@@ -66,6 +66,18 @@ module ActionForm
|
|
|
66
66
|
self.class.new(model: @namespaced_model, scope: @scope, params: form_params, **html_options)
|
|
67
67
|
end
|
|
68
68
|
|
|
69
|
+
def params_definition(scope: self.scope)
|
|
70
|
+
return super unless scope
|
|
71
|
+
|
|
72
|
+
@params_definitions ||= Hash.new do |h, key|
|
|
73
|
+
h[key] = begin
|
|
74
|
+
klass = super
|
|
75
|
+
Class.new(self.class.params_class) { has scope, klass }
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
@params_definitions[scope]
|
|
79
|
+
end
|
|
80
|
+
|
|
69
81
|
private
|
|
70
82
|
|
|
71
83
|
def subform_html_name(name, index: nil)
|
|
@@ -5,6 +5,7 @@ module ActionForm
|
|
|
5
5
|
module SchemaDSL
|
|
6
6
|
def self.included(base)
|
|
7
7
|
base.extend(ClassMethods)
|
|
8
|
+
base.include(InstanceMethods)
|
|
8
9
|
end
|
|
9
10
|
|
|
10
11
|
module ClassMethods # rubocop:disable Style/Documentation
|
|
@@ -37,5 +38,33 @@ module ActionForm
|
|
|
37
38
|
schema
|
|
38
39
|
end
|
|
39
40
|
end
|
|
41
|
+
|
|
42
|
+
module InstanceMethods # rubocop:disable Style/Documentation
|
|
43
|
+
def params_definition(*) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
|
44
|
+
schema = Class.new(self.class.params_class)
|
|
45
|
+
elements_instances.select(&:render?).each do |element|
|
|
46
|
+
case element
|
|
47
|
+
when ActionForm::SubformsCollection
|
|
48
|
+
# nested forms are passed as a hash that looks like this:
|
|
49
|
+
# { "0" => { "id" => "1" }, "1" => { "id" => "2" } }
|
|
50
|
+
# it is coercing to an array of hashes:
|
|
51
|
+
# [['0', { "id" => "1" }], ['1', { "id" => "2" }]]
|
|
52
|
+
# we need to normalize it to an array of hashes:
|
|
53
|
+
# [ { "id" => "1" }, { "
|
|
54
|
+
# id" => "2" } ]
|
|
55
|
+
schema.each(:"#{element.name}_attributes", element.subforms.first.params_definition,
|
|
56
|
+
normalize: ->(value) { value.flatten.select { |v| v.is_a?(Hash) } },
|
|
57
|
+
default: element.class.default)
|
|
58
|
+
when ActionForm::Subform
|
|
59
|
+
schema.has(:"#{element.name}_attributes", element.params_definition, default: element.class.default)
|
|
60
|
+
when ActionForm::Element
|
|
61
|
+
options = element.class.output_options.dup
|
|
62
|
+
method_name = options.delete(:type)
|
|
63
|
+
schema.public_send(method_name, element.name, **options)
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
schema
|
|
67
|
+
end
|
|
68
|
+
end
|
|
40
69
|
end
|
|
41
70
|
end
|
data/lib/action_form/version.rb
CHANGED