nested_form_fields 0.6.4 → 0.7

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
  SHA1:
3
- metadata.gz: 4e6d917676faea51365b71981a3894f71cadd08d
4
- data.tar.gz: 258aa4cd30b9e4aaa8c988a1728adce07ce8bfa5
3
+ metadata.gz: 494b811bb87fd99fa4b579a0007177634e1beadc
4
+ data.tar.gz: 2a0cb2875955c11b82eb72ce052cac5ce51444aa
5
5
  SHA512:
6
- metadata.gz: 6cd4d2b3649e84142eced58f735e41800ecbcf617f1b87148373f33fa2e21ae815b4a8b8cbcfd391bc9083c5947408e5a2abd62c28a846e4e67cdb7296d9fd8a
7
- data.tar.gz: f50d77161b8d1edeaf8a1e96793e556103d76561cfe6cfbfad4576fe932616243d311ea0050ba03d13d90c2ebf548590329893a5d039ab89d189662732da2521
6
+ metadata.gz: d405f5439c1522f6d3b0acb03618d74ec4606125c8f9238b7d1d8fed8bc49d4325ff338bac2e18819a4b3053dc23a2468ee2cf4e9ad8758c640e6c24bdeba078
7
+ data.tar.gz: 0a8d58d9e5e1f3b98cd3c87a18978dd2da98c29c01da36b65b7ca4f338212c01a8acc25d2ad60cc10334b3a8c244b6a52c7bf8dc0b9f0740f14fe0e565524371
data/README.md CHANGED
@@ -77,6 +77,29 @@ You can pass options like you would to the `content_tag` method by nesting them
77
77
 
78
78
  = f.nested_fields_for :videos, wrapper_options: { class: 'row' } do |ff|
79
79
 
80
+ If you are using Rails 4 remember to add << NESTED_MODEL >>_attributes and the attributes to the permitted params.
81
+ Also, if you want to destroy the nested model you should add :_destroy and :id.
82
+ For example:
83
+
84
+ # app/views/users/_form.haml.erb
85
+ = form_for @user do |f|
86
+ = f.nested_fields_for :videos do |ff|
87
+ = ff.remove_nested_fields_link
88
+ = ff.text_field :video_title
89
+ ..
90
+ = f.add_nested_fields_link :videos
91
+
92
+ # app/controllers/users_controller
93
+ ..
94
+ def user_params
95
+ params.require(:user)
96
+ .permit(:name,:email,videos_attributes:[:video_title,:_destroy,:id])
97
+ # ^^^ ^^^ ^^^
98
+ # nested model attrs
99
+ # they will let you delete the nested model
100
+ end
101
+
102
+
80
103
  There are 4 javascipt events firing before and after addition/removal of the fields in the *nested_form_fields* namespace. Namely:
81
104
  fields_adding, fields_added, fields_removing, fields_removed.
82
105
 
@@ -10,7 +10,7 @@ nested_form_fields.bind_nested_forms_links = () ->
10
10
  $.event.trigger("fields_adding.nested_form_fields",{object_class: object_class, added_index: added_index, association_path: association_path});
11
11
  $template = $("##{association_path}_template")
12
12
  target = $link.attr('data-insert-into')
13
-
13
+
14
14
  template_html = $template.html()
15
15
 
16
16
  # insert association indexes
@@ -34,6 +34,7 @@ nested_form_fields.bind_nested_forms_links = () ->
34
34
  $('body').off("click", '.remove_nested_fields_link')
35
35
  $('body').on 'click', '.remove_nested_fields_link', ->
36
36
  $link = $(this)
37
+ return false unless $.rails.allowAction($link)
37
38
  object_class = $link.data('object-class')
38
39
  delete_association_field_name = $link.data('delete-association-field-name')
39
40
  removed_index = parseInt(delete_association_field_name.match('(\\d+\\]\\[_destroy])')[0][0])
@@ -70,4 +71,3 @@ $.fn.closestChild = (selector) ->
70
71
  $results
71
72
  else
72
73
  $children.closestChild selector
73
-
@@ -23,22 +23,34 @@ module ActionView::Helpers
23
23
  end
24
24
 
25
25
 
26
- def add_nested_fields_link association, text = nil, html_options = {}
26
+ def add_nested_fields_link association, text = nil, html_options = {}, &block
27
27
  html_class = html_options.delete(:class) || {}
28
28
  html_data = html_options.delete(:data) || {}
29
- @template.link_to text || "Add #{association.to_s.singularize.humanize}", '', {
30
- class: "#{html_class} add_nested_fields_link",
31
- data: { association_path: association_path(association.to_s), object_class: association.to_s.singularize }.merge(html_data)
32
- }.merge(html_options)
29
+
30
+ args = []
31
+ args << (text || "Add #{association.to_s.singularize.humanize}") unless block_given?
32
+ args << ''
33
+ args << { class: "#{html_class} add_nested_fields_link",
34
+ data: { association_path: association_path(association.to_s),
35
+ object_class: association.to_s.singularize }.merge(html_data)
36
+ }.merge(html_options)
37
+
38
+ @template.link_to *args, &block
33
39
  end
34
40
 
35
- def remove_nested_fields_link text = nil, html_options = {}
41
+ def remove_nested_fields_link text = nil, html_options = {}, &block
36
42
  html_class = html_options.delete(:class) || {}
37
43
  html_data = html_options.delete(:data) || {}
38
- @template.link_to text || 'x', '', {
39
- class: "#{html_class} remove_nested_fields_link",
40
- data: { delete_association_field_name: delete_association_field_name, object_class: @object.class.name.underscore.downcase }.merge(html_data)
41
- }.merge(html_options)
44
+
45
+ args = []
46
+ args << (text || 'x') unless block_given?
47
+ args << ''
48
+ args << { class: "#{html_class} remove_nested_fields_link",
49
+ data: { delete_association_field_name: delete_association_field_name,
50
+ object_class: @object.class.name.underscore.downcase }.merge(html_data)
51
+ }.merge(html_options)
52
+
53
+ @template.link_to *args, &block
42
54
  end
43
55
 
44
56
 
@@ -1,3 +1,3 @@
1
1
  module NestedFormFields
2
- VERSION = "0.6.4"
2
+ VERSION = "0.7"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nested_form_fields
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.4
4
+ version: '0.7'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nico Ritsche
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-23 00:00:00.000000000 Z
11
+ date: 2015-05-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails