simple_form_nested_fields 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6503b66d302d584519904f940376f2d31dad4c1d8b70ed35f21e1b18949236db
4
- data.tar.gz: 944ab3847f631786be2f648c99c0cff69def74146edc9fe6065e61c2cd3691ca
3
+ metadata.gz: 777df20aaed96a6a38e9a46763102c7f14bf64e729a7ccf20dda5f12fb3fc389
4
+ data.tar.gz: c0e99aa18e7cfabfba0dc388efe87bbaffa171bd5e929ad30f163b43c544d8cc
5
5
  SHA512:
6
- metadata.gz: ad749bc8d209f5c081b3228a7139bd4e4038e08ebb1057c68ee487fb28bfe07885d7be2b9fba2f289b3c9fb8ab3c40ef82b37600afd8c34a531aa20edebfb9cc
7
- data.tar.gz: d92e37368e2f8db9e85c52d554c2f4cc82f05a5b84f8eee29141bffcba6c09cf9da5dc1e944dfec3ec7ab7754aea007a16c75eb1ab6c89e23a74ccb8309abbff
6
+ metadata.gz: db98c8de682982c7491974f975d6bcb9adf294c750b6b0255a5b0d9b50726aea3ad10112e62b48232f0985857fef1a77305a8640028c31527ac9f34dc6bc5e13
7
+ data.tar.gz: f33490c2edb4542051188f4131805445fa19bdb8df917b21b0b757917ae35b05ac8704cbe142635c52017476c96fc0b84fa52dba8120cff219f79d605616459e
@@ -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
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- simple_form_nested_fields (0.2.1)
4
+ simple_form_nested_fields (0.3.0)
5
5
  coffee-rails
6
6
  simple_form
7
7
 
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
- And in your `Text` fields partial add the position input:
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
- get_template: (link) -> $(link).data('template').replace(@options.regexp, @get_index())
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[:sortable] == true
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, File.join(object.model_name.collection, relation.klass.model_name.collection, 'fields'))
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, m: relation.klass)
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 render(partial_path, fields: fields)
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, link_to_add, class: dom_class).html_safe
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 = [bem_class(e: :link), bem_class(e: :link, m: :add)]
77
- dom_data = { template: CGI.escapeHTML(nested_fields_template).html_safe, turbolinks: 'false' }
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 nested_fields_template
88
- dom_class = bem_class(e: :item, m: relation.klass)
89
- content_tag :div, nested_fields_template_string, class: dom_class
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 nested_fields_template_string
93
- simple_fields_for(record_name, relation.klass.new, child_index: CHILD_INDEX_STRING) do |fields|
94
- nested_fields_item_handle.to_s.html_safe +
95
- render(partial_path, fields: fields).html_safe +
96
- link_to_remove(fields)
97
- end.html_safe
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 = [bem_class(e: :link), bem_class(e: :link, m: :remove)]
155
+ dom_class = bem_class(e: :link, m: :remove)
108
156
  dom_data = { turbolinks: 'false' }
109
157
  [
110
158
  destroy_field_tag(fields),
@@ -1,3 +1,3 @@
1
1
  module SimpleFormNestedFields
2
- VERSION = '0.2.1'.freeze
2
+ VERSION = '0.3.0'.freeze
3
3
  end
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "simple_form_nested_fields",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "…",
5
5
  "main": "package/dist/index.js",
6
6
  "repository": "https://github.com/tomasc/simple_form_nested_fields.git",
@@ -154,15 +154,28 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
154
154
  return new Date().getTime();
155
155
  }
156
156
  }, {
157
- key: 'get_template',
158
- value: function get_template(link) {
159
- return $(link).data('template').replace(this.options.regexp, this.get_index());
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) {
@@ -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
- get_template: (link) -> $(link).data('template').replace(@options.regexp, @get_index())
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.2.1
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-21 00:00:00.000000000 Z
12
+ date: 2018-12-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: simple_form