simple_form_nested_fields 0.2.1 → 0.3.0
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/CHANGELOG.md +6 -0
- data/Gemfile.lock +1 -1
- data/README.md +14 -7
- data/lib/assets/javascripts/simple_form_nested_fields__links.coffee +6 -1
- data/lib/simple_form_nested_fields/nested_fields_builder.rb +68 -20
- data/lib/simple_form_nested_fields/version.rb +1 -1
- data/package.json +1 -1
- data/package/dist/index.js +16 -3
- data/package/src/__links.coffee +6 -1
- metadata +2 -2
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA256:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 777df20aaed96a6a38e9a46763102c7f14bf64e729a7ccf20dda5f12fb3fc389
         | 
| 4 | 
            +
              data.tar.gz: c0e99aa18e7cfabfba0dc388efe87bbaffa171bd5e929ad30f163b43c544d8cc
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: db98c8de682982c7491974f975d6bcb9adf294c750b6b0255a5b0d9b50726aea3ad10112e62b48232f0985857fef1a77305a8640028c31527ac9f34dc6bc5e13
         | 
| 7 | 
            +
              data.tar.gz: f33490c2edb4542051188f4131805445fa19bdb8df917b21b0b757917ae35b05ac8704cbe142635c52017476c96fc0b84fa52dba8120cff219f79d605616459e
         | 
    
        data/CHANGELOG.md
    CHANGED
    
    | @@ -1,5 +1,11 @@ | |
| 1 1 | 
             
            # CHANGELOG
         | 
| 2 2 |  | 
| 3 | 
            +
            ## 0.3.0
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            * added support for inheritance and multiple item types (configurable via the `item_classes` option)
         | 
| 6 | 
            +
            * with subclasses, the `_type` hidden field is inserted automatically
         | 
| 7 | 
            +
            * when sortable, the `position` hidden field is inserted automatically
         | 
| 8 | 
            +
             | 
| 3 9 | 
             
            ## 0.2.1
         | 
| 4 10 |  | 
| 5 11 | 
             
            * [PR#3](https://github.com/tomasc/simple_form_nested_fields/pull/3) fix duplicate dom classes
         | 
    
        data/Gemfile.lock
    CHANGED
    
    
    
        data/README.md
    CHANGED
    
    | @@ -83,6 +83,19 @@ helper: | |
| 83 83 | 
             
            = fields.input :body
         | 
| 84 84 | 
             
            ```
         | 
| 85 85 |  | 
| 86 | 
            +
            ### Inheritance
         | 
| 87 | 
            +
             | 
| 88 | 
            +
            Subclassing of the relation class is handled automatically by default with:
         | 
| 89 | 
            +
             | 
| 90 | 
            +
            * partials corresponding to each subclass
         | 
| 91 | 
            +
            * select input next to the add link that allows the user choose the subclass to be added
         | 
| 92 | 
            +
             | 
| 93 | 
            +
            The choice of classes can be configured as follows:
         | 
| 94 | 
            +
             | 
| 95 | 
            +
            ```slim
         | 
| 96 | 
            +
            = f.nested_fields_for :variants, nil, item_classes: [MyDoc::Variant::One, MyDoc::Variant::Two]
         | 
| 97 | 
            +
            ```
         | 
| 98 | 
            +
             | 
| 86 99 | 
             
            ### Sortable
         | 
| 87 100 |  | 
| 88 101 | 
             
            Making the nested fields sortable is straight forward.
         | 
| @@ -117,13 +130,7 @@ have to pass the second parameter: the collection/record object): | |
| 117 130 | 
             
              = f.submit
         | 
| 118 131 | 
             
            ```
         | 
| 119 132 |  | 
| 120 | 
            -
             | 
| 121 | 
            -
             | 
| 122 | 
            -
            ```slim
         | 
| 123 | 
            -
            // app/views/my_docs/texts/_fields.html.slim
         | 
| 124 | 
            -
            = fields.input :position, as: :hidden
         | 
| 125 | 
            -
            = fields.input :body
         | 
| 126 | 
            -
            ```
         | 
| 133 | 
            +
            Please note the `position` input is automatically added to the partial.
         | 
| 127 134 |  | 
| 128 135 | 
             
            ### Configuration
         | 
| 129 136 |  | 
| @@ -25,8 +25,13 @@ do ($ = jQuery, window, document) -> | |
| 25 25 | 
             
                  @$element.off "click.#{@_name}", '.simple_form_nested_fields__link--add'
         | 
| 26 26 |  | 
| 27 27 | 
             
                get_index: -> new Date().getTime()
         | 
| 28 | 
            -
                 | 
| 28 | 
            +
                get_item_class_name: -> @get_select().val()
         | 
| 29 29 | 
             
                get_items_container: -> @$element.find('.simple_form_nested_fields__items')
         | 
| 30 | 
            +
                get_template: (link) ->
         | 
| 31 | 
            +
                  item_class_name = @get_item_class_name()
         | 
| 32 | 
            +
                  $template = @$element.find("template[data-class='#{item_class_name}']").first()
         | 
| 33 | 
            +
                  $template.html().replace(@options.regexp, @get_index())
         | 
| 34 | 
            +
                get_select: -> @$element.find('.simple_form_nested_fields__select--add')
         | 
| 30 35 |  | 
| 31 36 | 
             
                add_new_item: (link) ->
         | 
| 32 37 | 
             
                  $template = $(@get_template(link))
         | 
| @@ -16,13 +16,17 @@ module SimpleFormNestedFields | |
| 16 16 | 
             
                end
         | 
| 17 17 |  | 
| 18 18 | 
             
                def_delegators :builder, :object, :object_name, :simple_fields_for
         | 
| 19 | 
            -
                def_delegators :template, :concat, :content_tag, :hidden_field_tag, :link_to, :render
         | 
| 19 | 
            +
                def_delegators :template, :concat, :content_tag, :hidden_field_tag, :select_tag, :options_for_select, :link_to, :render
         | 
| 20 20 |  | 
| 21 21 | 
             
                def nested_fields_for
         | 
| 22 22 | 
             
                  content_tag(:div, class: wrapper_class) do
         | 
| 23 23 | 
             
                    concat nested_fields_title
         | 
| 24 24 | 
             
                    concat nested_fields_items
         | 
| 25 25 | 
             
                    concat nested_fields_links
         | 
| 26 | 
            +
             | 
| 27 | 
            +
                    item_classes.each do |cls|
         | 
| 28 | 
            +
                      concat nested_fields_template(cls)
         | 
| 29 | 
            +
                    end
         | 
| 26 30 | 
             
                  end
         | 
| 27 31 | 
             
                end
         | 
| 28 32 |  | 
| @@ -34,11 +38,24 @@ module SimpleFormNestedFields | |
| 34 38 | 
             
                end
         | 
| 35 39 |  | 
| 36 40 | 
             
                def is_sortable?
         | 
| 37 | 
            -
                  options | 
| 41 | 
            +
                  options.fetch(:sortable, false)
         | 
| 42 | 
            +
                end
         | 
| 43 | 
            +
             | 
| 44 | 
            +
                def multiple_item_classes?
         | 
| 45 | 
            +
                  item_classes.length > 1
         | 
| 46 | 
            +
                end
         | 
| 47 | 
            +
             | 
| 48 | 
            +
                def item_classes
         | 
| 49 | 
            +
                  options.fetch(:item_classes) do
         | 
| 50 | 
            +
                    return relation.klass.descendants if relation.klass.descendants.present?
         | 
| 51 | 
            +
                    [relation.klass]
         | 
| 52 | 
            +
                  end
         | 
| 38 53 | 
             
                end
         | 
| 39 54 |  | 
| 40 | 
            -
                def partial_path
         | 
| 41 | 
            -
                  options.fetch(:partial | 
| 55 | 
            +
                def partial_path(cls)
         | 
| 56 | 
            +
                  options.fetch(:partial) do
         | 
| 57 | 
            +
                    File.join(object.model_name.collection, cls.model_name.collection, 'fields')
         | 
| 58 | 
            +
                  end
         | 
| 42 59 | 
             
                end
         | 
| 43 60 |  | 
| 44 61 | 
             
                def relation
         | 
| @@ -51,15 +68,24 @@ module SimpleFormNestedFields | |
| 51 68 | 
             
                  content_tag(:div, title, class: dom_class).html_safe
         | 
| 52 69 | 
             
                end
         | 
| 53 70 |  | 
| 71 | 
            +
                def nested_fields_item_class_name(cls)
         | 
| 72 | 
            +
                  return unless multiple_item_classes?
         | 
| 73 | 
            +
                  dom_class = bem_class(e: :item_class_name)
         | 
| 74 | 
            +
                  content_tag(:div, cls.model_name.human, class: dom_class).html_safe
         | 
| 75 | 
            +
                end
         | 
| 76 | 
            +
             | 
| 54 77 | 
             
                def nested_fields_items
         | 
| 55 78 | 
             
                  content_tag(:div, class: bem_class(e: :items)) do
         | 
| 56 79 | 
             
                    simple_fields_for(record_name, record_object, options) do |fields|
         | 
| 57 | 
            -
                      dom_class = bem_class(e: :item | 
| 58 | 
            -
                      dom_data = { id: fields.object.id.to_s }
         | 
| 80 | 
            +
                      dom_class = bem_class(e: :item)
         | 
| 81 | 
            +
                      dom_data = { id: fields.object.id.to_s, class: fields.object.class.to_s }
         | 
| 59 82 |  | 
| 60 83 | 
             
                      content_tag(:div, class: dom_class, data: dom_data) do
         | 
| 84 | 
            +
                        concat nested_fields_item_class_name(fields.object.class)
         | 
| 61 85 | 
             
                        concat nested_fields_item_handle
         | 
| 62 | 
            -
                        concat  | 
| 86 | 
            +
                        concat nested_fields__type_input(fields)
         | 
| 87 | 
            +
                        concat nested_fields_position_input(fields)
         | 
| 88 | 
            +
                        concat render(partial_path(fields.object.class), fields: fields)
         | 
| 63 89 | 
             
                        concat link_to_remove(fields)
         | 
| 64 90 | 
             
                      end
         | 
| 65 91 | 
             
                    end
         | 
| @@ -68,13 +94,23 @@ module SimpleFormNestedFields | |
| 68 94 |  | 
| 69 95 | 
             
                def nested_fields_links
         | 
| 70 96 | 
             
                  dom_class = bem_class(e: :links)
         | 
| 71 | 
            -
                  content_tag(:div,  | 
| 97 | 
            +
                  content_tag(:div, class: dom_class) do
         | 
| 98 | 
            +
                    concat select_for_add
         | 
| 99 | 
            +
                    concat link_to_add
         | 
| 100 | 
            +
                  end.html_safe
         | 
| 101 | 
            +
                end
         | 
| 102 | 
            +
             | 
| 103 | 
            +
                def select_for_add
         | 
| 104 | 
            +
                  dom_class = bem_class(e: :select, m: :add)
         | 
| 105 | 
            +
                  dom_style = ""
         | 
| 106 | 
            +
                  dom_style = "display: none;" unless multiple_item_classes?
         | 
| 107 | 
            +
                  select_tag nil, options_for_select(item_classes.map { |kls| [kls.model_name.human, kls.to_s] }), class: dom_class, style: dom_style
         | 
| 72 108 | 
             
                end
         | 
| 73 109 |  | 
| 74 110 | 
             
                def link_to_add
         | 
| 75 111 | 
             
                  label = options.fetch(:label_add, ::I18n.t(:add, scope: %i[simple_form_nested_fields links], model_name: relation.klass.model_name.human))
         | 
| 76 | 
            -
                  dom_class =  | 
| 77 | 
            -
                  dom_data = {  | 
| 112 | 
            +
                  dom_class = bem_class(e: :link, m: :add)
         | 
| 113 | 
            +
                  dom_data = { turbolinks: 'false' }
         | 
| 78 114 | 
             
                  link_to(label, '#', class: dom_class, data: dom_data).html_safe
         | 
| 79 115 | 
             
                end
         | 
| 80 116 |  | 
| @@ -84,17 +120,29 @@ module SimpleFormNestedFields | |
| 84 120 | 
             
                  content_tag(:div, nil, class: dom_class).html_safe
         | 
| 85 121 | 
             
                end
         | 
| 86 122 |  | 
| 87 | 
            -
                def  | 
| 88 | 
            -
                   | 
| 89 | 
            -
                   | 
| 123 | 
            +
                def nested_fields_position_input(fields)
         | 
| 124 | 
            +
                  return unless is_sortable?
         | 
| 125 | 
            +
                  fields.input(:position, as: :hidden).html_safe
         | 
| 126 | 
            +
                end
         | 
| 127 | 
            +
             | 
| 128 | 
            +
                def nested_fields__type_input(fields)
         | 
| 129 | 
            +
                  return unless fields.object.respond_to?(:_type)
         | 
| 130 | 
            +
                  fields.input(:_type, as: :hidden).html_safe
         | 
| 90 131 | 
             
                end
         | 
| 91 132 |  | 
| 92 | 
            -
                def  | 
| 93 | 
            -
                   | 
| 94 | 
            -
                     | 
| 95 | 
            -
                       | 
| 96 | 
            -
             | 
| 97 | 
            -
             | 
| 133 | 
            +
                def nested_fields_template(cls)
         | 
| 134 | 
            +
                  content_tag :template, data: { class: cls.to_s } do
         | 
| 135 | 
            +
                    content_tag :div, class: bem_class(e: :item), data: { class: cls.to_s } do
         | 
| 136 | 
            +
                      simple_fields_for(record_name, cls.new, child_index: CHILD_INDEX_STRING) do |fields|
         | 
| 137 | 
            +
                        concat nested_fields_item_class_name(cls)
         | 
| 138 | 
            +
                        concat nested_fields_item_handle
         | 
| 139 | 
            +
                        concat nested_fields__type_input(fields)
         | 
| 140 | 
            +
                        concat nested_fields_position_input(fields)
         | 
| 141 | 
            +
                        concat render(partial_path(cls), fields: fields)
         | 
| 142 | 
            +
                        concat link_to_remove(fields)
         | 
| 143 | 
            +
                      end
         | 
| 144 | 
            +
                    end
         | 
| 145 | 
            +
                  end
         | 
| 98 146 | 
             
                end
         | 
| 99 147 |  | 
| 100 148 | 
             
                def destroy_field_tag(fields)
         | 
| @@ -104,7 +152,7 @@ module SimpleFormNestedFields | |
| 104 152 |  | 
| 105 153 | 
             
                def link_to_remove(fields, options = {})
         | 
| 106 154 | 
             
                  label = options.fetch(:label, ::I18n.t(:remove, scope: %i[simple_form_nested_fields links]))
         | 
| 107 | 
            -
                  dom_class =  | 
| 155 | 
            +
                  dom_class = bem_class(e: :link, m: :remove)
         | 
| 108 156 | 
             
                  dom_data = { turbolinks: 'false' }
         | 
| 109 157 | 
             
                  [
         | 
| 110 158 | 
             
                    destroy_field_tag(fields),
         | 
    
        data/package.json
    CHANGED
    
    
    
        data/package/dist/index.js
    CHANGED
    
    | @@ -154,15 +154,28 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons | |
| 154 154 | 
             
                    return new Date().getTime();
         | 
| 155 155 | 
             
                  }
         | 
| 156 156 | 
             
                }, {
         | 
| 157 | 
            -
                  key: ' | 
| 158 | 
            -
                  value: function  | 
| 159 | 
            -
                    return  | 
| 157 | 
            +
                  key: 'get_item_class_name',
         | 
| 158 | 
            +
                  value: function get_item_class_name() {
         | 
| 159 | 
            +
                    return this.get_select().val();
         | 
| 160 160 | 
             
                  }
         | 
| 161 161 | 
             
                }, {
         | 
| 162 162 | 
             
                  key: 'get_items_container',
         | 
| 163 163 | 
             
                  value: function get_items_container() {
         | 
| 164 164 | 
             
                    return this.$element.find('.simple_form_nested_fields__items');
         | 
| 165 165 | 
             
                  }
         | 
| 166 | 
            +
                }, {
         | 
| 167 | 
            +
                  key: 'get_template',
         | 
| 168 | 
            +
                  value: function get_template(link) {
         | 
| 169 | 
            +
                    var $template, item_class_name;
         | 
| 170 | 
            +
                    item_class_name = this.get_item_class_name();
         | 
| 171 | 
            +
                    $template = this.$element.find('template[data-class=\'' + item_class_name + '\']').first();
         | 
| 172 | 
            +
                    return $template.html().replace(this.options.regexp, this.get_index());
         | 
| 173 | 
            +
                  }
         | 
| 174 | 
            +
                }, {
         | 
| 175 | 
            +
                  key: 'get_select',
         | 
| 176 | 
            +
                  value: function get_select() {
         | 
| 177 | 
            +
                    return this.$element.find('.simple_form_nested_fields__select--add');
         | 
| 178 | 
            +
                  }
         | 
| 166 179 | 
             
                }, {
         | 
| 167 180 | 
             
                  key: 'add_new_item',
         | 
| 168 181 | 
             
                  value: function add_new_item(link) {
         | 
    
        data/package/src/__links.coffee
    CHANGED
    
    | @@ -24,8 +24,13 @@ do ($ = jQuery, window, document) -> | |
| 24 24 | 
             
                  @$element.off "click.#{@_name}", '.simple_form_nested_fields__link--add'
         | 
| 25 25 |  | 
| 26 26 | 
             
                get_index: -> new Date().getTime()
         | 
| 27 | 
            -
                 | 
| 27 | 
            +
                get_item_class_name: -> @get_select().val()
         | 
| 28 28 | 
             
                get_items_container: -> @$element.find('.simple_form_nested_fields__items')
         | 
| 29 | 
            +
                get_template: (link) ->
         | 
| 30 | 
            +
                  item_class_name = @get_item_class_name()
         | 
| 31 | 
            +
                  $template = @$element.find("template[data-class='#{item_class_name}']").first()
         | 
| 32 | 
            +
                  $template.html().replace(@options.regexp, @get_index())
         | 
| 33 | 
            +
                get_select: -> @$element.find('.simple_form_nested_fields__select--add')
         | 
| 29 34 |  | 
| 30 35 | 
             
                add_new_item: (link) ->
         | 
| 31 36 | 
             
                  $template = $(@get_template(link))
         | 
    
        metadata
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: simple_form_nested_fields
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0. | 
| 4 | 
            +
              version: 0.3.0
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Tomas Celizna
         | 
| @@ -9,7 +9,7 @@ authors: | |
| 9 9 | 
             
            autorequire: 
         | 
| 10 10 | 
             
            bindir: exe
         | 
| 11 11 | 
             
            cert_chain: []
         | 
| 12 | 
            -
            date: 2018-12- | 
| 12 | 
            +
            date: 2018-12-22 00:00:00.000000000 Z
         | 
| 13 13 | 
             
            dependencies:
         | 
| 14 14 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 15 15 | 
             
              name: simple_form
         |