action_form 0.6.4 → 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: 3e194888488e1747bdc4e384b5cfb2f6fd78ab9cbcf16c9bf00fd81fda9bb849
4
- data.tar.gz: bb3f84b823d7fc870fbd84fcfbbdf58259792345b47aba4ac9681fdd58d1c5e6
3
+ metadata.gz: a30578d111d84b9c748248c82a65eac75389b0696f27dbb3a36a3c437b5643e3
4
+ data.tar.gz: 2da98bb16f9368a0f07a70d1c47f904b80421f584c024ae69ea18db009f23bf3
5
5
  SHA512:
6
- metadata.gz: 22dd6884375dd7eaa180de1210a8ab702a1aefaa9e72c4988bc00f3f5ec65db16c5018281fabfbc5bd770a355460aa077eca1d3b66766269ed13f2ab09d9d0dc
7
- data.tar.gz: 8216c17312f90892b83cae63291222d6630a7d5c33ea8b623e3785b1d68e0b7f82b56d8952164f1960762c5b40274639cbe2662ec792239c0395a091312b2ea7
6
+ metadata.gz: 8c9a05b8349c2072c787d95102fd047e6a067e4d367b79c4284c326c75abc2cd05c1a09b38c41dac2d52536cdbb737c927b4f9231a38472cfda66690ed82085f
7
+ data.tar.gz: e654910d6f00232ee6995955f48c2089edc70de5151c07ccb247b48758b1ed5c2ad6f3306807b37ac3008d9e0bc722c6ebb7ca1b208ea49aa105d448143133d5
@@ -12,7 +12,7 @@ module ActionForm
12
12
  attr_reader :elements_instances, :scope, :object, :html_options, :errors
13
13
 
14
14
  class << self
15
- attr_writer :elements, :scope
15
+ attr_writer :elements, :scope, :params_definition
16
16
 
17
17
  def subform_class
18
18
  ActionForm::Subform
@@ -27,7 +27,7 @@ module ActionForm
27
27
  def inherited(subclass)
28
28
  super
29
29
  subclass.elements = elements.dup
30
- subclass.scope = scope.dup
30
+ subclass.scope = scope&.dup
31
31
  end
32
32
  end
33
33
 
@@ -35,7 +35,7 @@ module ActionForm
35
35
  super()
36
36
  @object = object
37
37
  @scope ||= scope
38
- @params = @scope && params.respond_to?(@scope) ? params.public_send(@scope) : params
38
+ @params = params
39
39
  @html_options = html_options
40
40
  @elements_instances = []
41
41
  @owner = owner
@@ -31,19 +31,6 @@ module ActionForm
31
31
  @scope = model.model_name.param_key.to_sym
32
32
  end
33
33
 
34
- def params_definition(scope: self.scope)
35
- return super unless scope
36
-
37
- @params_definitions ||= Hash.new do |h, key|
38
- h[key] = begin
39
- klass = super
40
- Class.new(params_class) { has scope, klass }
41
- end
42
- end
43
- @params_definitions[scope].form_class = self
44
- @params_definitions[scope]
45
- end
46
-
47
34
  def many(name, default: nil, &block)
48
35
  super
49
36
  elements[name].subform_definition.add_primary_key_element
@@ -63,18 +50,6 @@ module ActionForm
63
50
  end
64
51
  end
65
52
 
66
- def params_definition(scope: self.scope)
67
- return super unless scope
68
-
69
- @params_definitions ||= Hash.new do |h, key|
70
- h[key] = begin
71
- klass = super
72
- Class.new(self.class.params_class) { has scope, klass }
73
- end
74
- end
75
- @params_definitions[scope]
76
- end
77
-
78
53
  private
79
54
 
80
55
  def subform_html_name(name, index: nil)
@@ -13,19 +13,31 @@ module ActionForm
13
13
  ActionForm::Params
14
14
  end
15
15
 
16
- def params_definition(*)
16
+ def params_definition
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 = scope ? params_definition(scope: scope) : create_params_definition(&block)
22
- @params_definition.class_eval(&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
23
36
  end
24
37
 
25
- def create_params_definition # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
38
+ def create_params_definition(elements_definitions: elements) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
26
39
  schema = Class.new(params_class)
27
- schema.form_class = self
28
- elements.each do |name, element_definition|
40
+ elements_definitions.each do |name, element_definition|
29
41
  if element_definition < ActionForm::SubformsCollection
30
42
  # nested forms are passed as a hash that looks like this:
31
43
  # { "0" => { "id" => "1" }, "1" => { "id" => "2" } }
@@ -45,40 +57,25 @@ module ActionForm
45
57
  schema.public_send(method_name, name, **options)
46
58
  end
47
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
48
65
  schema
49
66
  end
50
67
  end
51
68
 
52
69
  module InstanceMethods # rubocop:disable Style/Documentation
53
- def params_definition(*)
70
+ def params_definition
54
71
  @params_definition ||= create_params_definition
55
72
  end
56
73
 
57
- def create_params_definition # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
74
+ def create_params_definition
58
75
  schema = Class.new(self.class.params_class)
59
76
  schema.form_class = self.class
60
- elements_instances.select(&:render?).each do |element|
61
- case element
62
- when ActionForm::SubformsCollection
63
- # nested forms are passed as a hash that looks like this:
64
- # { "0" => { "id" => "1" }, "1" => { "id" => "2" } }
65
- # it is coercing to an array of hashes:
66
- # [['0', { "id" => "1" }], ['1', { "id" => "2" }]]
67
- # we need to normalize it to an array of hashes:
68
- # [ { "id" => "1" }, { "
69
- # id" => "2" } ]
70
- schema.each(:"#{element.name}_attributes", element.subforms.first.params_definition,
71
- normalize: ->(value) { value.flatten.select { |v| v.is_a?(Hash) } },
72
- default: element.class.default)
73
- when ActionForm::Subform
74
- schema.has(:"#{element.name}_attributes", element.params_definition, default: element.class.default)
75
- when ActionForm::Element
76
- options = element.class.output_options.dup
77
- method_name = options.delete(:type)
78
- schema.public_send(method_name, element.name, **options)
79
- end
80
- end
81
- 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)
82
79
  end
83
80
  end
84
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.4"
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.4
4
+ version: 0.6.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrii Baran