action_form 0.2.0 → 0.2.2

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: 3c388d4f18fe078d19f3bbc594f38785dbab5894f78296c2ca497de55babf291
4
- data.tar.gz: 30dea5a93bf2f856f473cfd3332d0422f8bec36460f47141845fc0ded75b1c1a
3
+ metadata.gz: 2de812470c0c3517398732ec65d10a8a752e4c186ad2a314f9de1c46623845d9
4
+ data.tar.gz: ce27aaf8173f356ea64746bbd249d627417f8364ff54b1824ba6bfb64f3a6b54
5
5
  SHA512:
6
- metadata.gz: f1e40b17cdc9f0afa700128fb05a724c6f6695881ea5047ceb4c00e3a2d7dc75915dfd7bd1de230cada42dc093b2e6b39b8fc978c04337d426a63b8930162dc9
7
- data.tar.gz: b5e8960c78350ec32bfd4a665cafceb62287f7eabdd55b434fc85380fcaa3f95cd18f05fbf49d989736a4569f8c4b56cd23ef95682f006a9851ee3a46f9ff7ba
6
+ metadata.gz: 5f420e141100db71aa260831efa8936a6ac90331464d67428fbbce83ea0a7a15db69f36aa31cddadb3d3227b3833d253e2eb7aa7c1a8a4f13a255b9973158d24
7
+ data.tar.gz: bbbc77014e7ee1b807abbb11030553120232a7d2bcaee5b376de306d8f864e0887ccd0afac3aa3a2a4b9868db2e30b5aa6b5b1b01b4032848fbad8f6d4e75e49
@@ -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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActionForm
4
- VERSION = "0.2.0"
4
+ VERSION = "0.2.2"
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.2.0
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrii Baran
@@ -15,14 +15,14 @@ dependencies:
15
15
  requirements:
16
16
  - - "~>"
17
17
  - !ruby/object:Gem::Version
18
- version: '0.7'
18
+ version: '0.8'
19
19
  type: :runtime
20
20
  prerelease: false
21
21
  version_requirements: !ruby/object:Gem::Requirement
22
22
  requirements:
23
23
  - - "~>"
24
24
  - !ruby/object:Gem::Version
25
- version: '0.7'
25
+ version: '0.8'
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: phlex
28
28
  requirement: !ruby/object:Gem::Requirement