action_form 0.1.1 → 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/base.rb +2 -1
 - data/lib/action_form/element.rb +6 -3
 - data/lib/action_form/rails/base.rb +12 -0
 - data/lib/action_form/rendering.rb +2 -2
 - data/lib/action_form/schema_dsl.rb +29 -0
 - data/lib/action_form/subforms_collection.rb +2 -2
 - 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
         
     | 
    
        data/lib/action_form/base.rb
    CHANGED
    
    | 
         @@ -101,7 +101,8 @@ module ActionForm 
     | 
|
| 
       101 
101 
     | 
    
         
             
                def build_subform_template(name, form_definition)
         
     | 
| 
       102 
102 
     | 
    
         
             
                  html_name = subform_html_name(name, index: "NEW_RECORD")
         
     | 
| 
       103 
103 
     | 
    
         
             
                  elements_keys = form_definition.elements.keys.push(:persisted?)
         
     | 
| 
       104 
     | 
    
         
            -
                   
     | 
| 
      
 104 
     | 
    
         
            +
                  values = form_definition.elements.values.map(&:default)
         
     | 
| 
      
 105 
     | 
    
         
            +
                  value = Struct.new(*elements_keys).new(*values)
         
     | 
| 
       105 
106 
     | 
    
         
             
                  form_definition.new(name: name, scope: html_name, model: value, template: true).tap do |subform|
         
     | 
| 
       106 
107 
     | 
    
         
             
                    subform.helpers = helpers
         
     | 
| 
       107 
108 
     | 
    
         
             
                  end
         
     | 
    
        data/lib/action_form/element.rb
    CHANGED
    
    | 
         @@ -18,6 +18,8 @@ module ActionForm 
     | 
|
| 
       18 
18 
     | 
    
         
             
                end
         
     | 
| 
       19 
19 
     | 
    
         | 
| 
       20 
20 
     | 
    
         
             
                class << self
         
     | 
| 
      
 21 
     | 
    
         
            +
                  attr_reader :default
         
     | 
| 
      
 22 
     | 
    
         
            +
             
     | 
| 
       21 
23 
     | 
    
         
             
                  def label_options
         
     | 
| 
       22 
24 
     | 
    
         
             
                    @label_options ||= [{ text: nil, display: true }, {}]
         
     | 
| 
       23 
25 
     | 
    
         
             
                  end
         
     | 
| 
         @@ -38,7 +40,8 @@ module ActionForm 
     | 
|
| 
       38 
40 
     | 
    
         
             
                    @tags_list ||= {}
         
     | 
| 
       39 
41 
     | 
    
         
             
                  end
         
     | 
| 
       40 
42 
     | 
    
         | 
| 
       41 
     | 
    
         
            -
                  def input(type:, **options)
         
     | 
| 
      
 43 
     | 
    
         
            +
                  def input(type:, default: nil, **options)
         
     | 
| 
      
 44 
     | 
    
         
            +
                    @default = default
         
     | 
| 
       42 
45 
     | 
    
         
             
                    @input_options = { type: type }.merge(options)
         
     | 
| 
       43 
46 
     | 
    
         
             
                    tags_list.merge!(input: type)
         
     | 
| 
       44 
47 
     | 
    
         
             
                  end
         
     | 
| 
         @@ -104,9 +107,9 @@ module ActionForm 
     | 
|
| 
       104 
107 
     | 
    
         
             
                end
         
     | 
| 
       105 
108 
     | 
    
         | 
| 
       106 
109 
     | 
    
         
             
                def value
         
     | 
| 
       107 
     | 
    
         
            -
                  return unless object
         
     | 
| 
      
 110 
     | 
    
         
            +
                  return self.class.default unless object
         
     | 
| 
       108 
111 
     | 
    
         | 
| 
       109 
     | 
    
         
            -
                  object.public_send(name)
         
     | 
| 
      
 112 
     | 
    
         
            +
                  object.public_send(name) || self.class.default
         
     | 
| 
       110 
113 
     | 
    
         
             
                end
         
     | 
| 
       111 
114 
     | 
    
         | 
| 
       112 
115 
     | 
    
         
             
                def render?
         
     | 
| 
         @@ -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)
         
     | 
| 
         @@ -55,11 +55,11 @@ module ActionForm 
     | 
|
| 
       55 
55 
     | 
    
         
             
                end
         
     | 
| 
       56 
56 
     | 
    
         | 
| 
       57 
57 
     | 
    
         
             
                def render_remove_subform_button(**html_attributes, &block)
         
     | 
| 
       58 
     | 
    
         
            -
                  a(**html_attributes, onclick: safe(" 
     | 
| 
      
 58 
     | 
    
         
            +
                  a(**html_attributes, onclick: safe("actionFormRemoveSubform(event)"), &block)
         
     | 
| 
       59 
59 
     | 
    
         
             
                end
         
     | 
| 
       60 
60 
     | 
    
         | 
| 
       61 
61 
     | 
    
         
             
                def render_new_subform_button(**html_attributes, &block)
         
     | 
| 
       62 
     | 
    
         
            -
                  a(**html_attributes, onclick: safe(" 
     | 
| 
      
 62 
     | 
    
         
            +
                  a(**html_attributes, onclick: safe("actionFormAddSubform(event)"), &block)
         
     | 
| 
       63 
63 
     | 
    
         
             
                end
         
     | 
| 
       64 
64 
     | 
    
         | 
| 
       65 
65 
     | 
    
         
             
                private
         
     | 
| 
         @@ -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
         
     | 
| 
         @@ -44,7 +44,7 @@ module ActionForm 
     | 
|
| 
       44 
44 
     | 
    
         | 
| 
       45 
45 
     | 
    
         
             
                def add_subform_js
         
     | 
| 
       46 
46 
     | 
    
         
             
                  <<~JS
         
     | 
| 
       47 
     | 
    
         
            -
                    function  
     | 
| 
      
 47 
     | 
    
         
            +
                    function actionFormAddSubform(event) {
         
     | 
| 
       48 
48 
     | 
    
         
             
                      event.preventDefault()
         
     | 
| 
       49 
49 
     | 
    
         
             
                      var template = document.querySelector("##{template_html_id}")
         
     | 
| 
       50 
50 
     | 
    
         
             
                      const content = template.innerHTML.replace(/NEW_RECORD/g, new Date().getTime().toString())
         
     | 
| 
         @@ -60,7 +60,7 @@ module ActionForm 
     | 
|
| 
       60 
60 
     | 
    
         | 
| 
       61 
61 
     | 
    
         
             
                def remove_subform_js
         
     | 
| 
       62 
62 
     | 
    
         
             
                  <<~JS
         
     | 
| 
       63 
     | 
    
         
            -
                    function  
     | 
| 
      
 63 
     | 
    
         
            +
                    function actionFormRemoveSubform(event) {
         
     | 
| 
       64 
64 
     | 
    
         
             
                      event.preventDefault()
         
     | 
| 
       65 
65 
     | 
    
         
             
                      var subform = event.target.closest(".new_#{name}")
         
     | 
| 
       66 
66 
     | 
    
         
             
                      if (subform) { subform.remove() }
         
     | 
    
        data/lib/action_form/version.rb
    CHANGED