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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 72382d4935502742cc4736c21c7964185e8fdb154972bc4ae38ae9497c18009d
4
- data.tar.gz: a28f5cc2229551ce6a116721d8c34a4d134a2acc1bea9c1b081e633c20f60834
3
+ metadata.gz: a30578d111d84b9c748248c82a65eac75389b0696f27dbb3a36a3c437b5643e3
4
+ data.tar.gz: 2da98bb16f9368a0f07a70d1c47f904b80421f584c024ae69ea18db009f23bf3
5
5
  SHA512:
6
- metadata.gz: 6efb2e07f6de58b2d25af0e9dcaf3d0c779a7a72ea7d70acf0a840ceb70d74053902742b659369e99d7b723660e2bcac87784a9c17b1bfa3ad2f876d7e92d93b
7
- data.tar.gz: 4fd0ede2b1ef5bc8a74f12ceefd75e4d5af245abe4a26cf60fa294320198904af6f2be66245beac5a96f45c7bb72f4f0409a2f352e6f068e0cdf3f558f338dd8
6
+ metadata.gz: 8c9a05b8349c2072c787d95102fd047e6a067e4d367b79c4284c326c75abc2cd05c1a09b38c41dac2d52536cdbb737c927b4f9231a38472cfda66690ed82085f
7
+ data.tar.gz: e654910d6f00232ee6995955f48c2089edc70de5151c07ccb247b48758b1ed5c2ad6f3306807b37ac3008d9e0bc722c6ebb7ca1b208ea49aa105d448143133d5
@@ -27,9 +27,7 @@ module ActionForm
27
27
  def inherited(subclass)
28
28
  super
29
29
  subclass.elements = elements.dup
30
- subclass.scope = scope.dup
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
- @params_definition = Class.new(params_definition, &block) if block
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
- schema.form_class = self
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 # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
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?).each do |element|
60
- case element
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActionForm
4
- VERSION = "0.6.5"
4
+ VERSION = "0.6.6"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: action_form
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.5
4
+ version: 0.6.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrii Baran