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 +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 +3 -3
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA256:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: 2de812470c0c3517398732ec65d10a8a752e4c186ad2a314f9de1c46623845d9
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: ce27aaf8173f356ea64746bbd249d627417f8364ff54b1824ba6bfb64f3a6b54
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 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
         
     | 
    
        data/lib/action_form/version.rb
    CHANGED
    
    
    
        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. 
     | 
| 
      
 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. 
     | 
| 
      
 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. 
     | 
| 
      
 25 
     | 
    
         
            +
                    version: '0.8'
         
     | 
| 
       26 
26 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       27 
27 
     | 
    
         
             
              name: phlex
         
     | 
| 
       28 
28 
     | 
    
         
             
              requirement: !ruby/object:Gem::Requirement
         
     |