nested_form_fields 0.3.0 → 0.4.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.
data/README.md CHANGED
@@ -19,7 +19,7 @@ Add this line to your application's Gemfile:
19
19
  And then execute:
20
20
 
21
21
  $ bundle
22
-
22
+
23
23
  In your application.js file add:
24
24
 
25
25
  //= require nested_form_fields
@@ -39,7 +39,7 @@ Use the *nested_fields_for* helper inside your user form to add the video fields
39
39
  = f.nested_fields_for :videos do |ff|
40
40
  = ff.text_field :video_title
41
41
  ..
42
-
42
+
43
43
  Links to add and remove fields can be added using the *add_nested_fields_link* and *remove_nested_fields_link* helpers:
44
44
 
45
45
  = form_for @user do |f|
@@ -48,7 +48,7 @@ Links to add and remove fields can be added using the *add_nested_fields_link* a
48
48
  = ff.text_field :video_title
49
49
  ..
50
50
  = f.add_nested_fields_link :videos
51
-
51
+
52
52
  Note that *remove_nested_fields_link* needs to be called within the *nested_fields_for* call and *add_nested_fields_link* outside of it via the parent builder.
53
53
 
54
54
  You can change the link text of *remove_nested_fields_link* and *add_nested_fields_link* like this:
@@ -62,8 +62,22 @@ You can change the type of the element wrapping the nested fields using the *wra
62
62
 
63
63
  = f.nested_fields_for :videos, wrapper_tag: :div do |ff|
64
64
 
65
- The default wrapper element is a fieldset.
65
+ The default wrapper element is a fieldset. To add legend element to the fieldset use:
66
+
67
+ = f.nested_fields_for :videos, legend: "Video" do |ff|
66
68
 
69
+ There are 4 javascipt events firing before and after addition/removal of the fields in the *nested_form_fields* namespace. Namely:
70
+ fields_adding, fields_added, fields_removing, fields_removed.
71
+
72
+ CoffeeScript sample:
73
+
74
+ $(document).on "fields_added.nested_form_fields", (event,param) ->
75
+ switch param.object_class
76
+ when "video"
77
+ console.log "Video object added"
78
+ else
79
+ console.log "INFO: Fields were successfully added, callback not handled."
80
+
67
81
 
68
82
  ## Contributing
69
83
 
@@ -76,4 +90,5 @@ The default wrapper element is a fieldset.
76
90
 
77
91
  ## Contributers
78
92
 
79
- [Tom Riley](https://github.com/tomriley)
93
+ - [Tom Riley](https://github.com/tomriley)
94
+ - [Levente Tamas](https://github.com/tamisoft)
@@ -26,13 +26,13 @@ module ActionView::Helpers
26
26
  def add_nested_fields_link association, text = nil
27
27
  @template.link_to text || "Add #{association.to_s.singularize.humanize}", '',
28
28
  class: "add_nested_fields_link",
29
- data: { association_path: association_path(association.to_s) }
29
+ data: { association_path: association_path(association.to_s), object_class: association.to_s.singularize }
30
30
  end
31
31
 
32
32
  def remove_nested_fields_link text = nil
33
33
  @template.link_to text || 'x', '',
34
34
  class: "remove_nested_fields_link",
35
- data: { delete_association_field_name: delete_association_field_name }
35
+ data: { delete_association_field_name: delete_association_field_name, object_class: @object.class.name.underscore.downcase }
36
36
  end
37
37
 
38
38
 
@@ -1,3 +1,3 @@
1
1
  module NestedFormFields
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -1,7 +1,11 @@
1
- bind_nested_forms_links = () ->
1
+ window.nested_form_fields or= {}
2
+
3
+ nested_form_fields.bind_nested_forms_links = () ->
2
4
  $('body').off("click", '.add_nested_fields_link')
3
5
  $('body').on 'click', '.add_nested_fields_link', ->
4
6
  $link = $(this)
7
+ object_class = $link.data('object-class')
8
+ $.event.trigger("fields_adding.nested_form_fields",{object_class: object_class});
5
9
  association_path = $link.data('association-path')
6
10
  $template = $("##{association_path}_template")
7
11
 
@@ -19,19 +23,23 @@ bind_nested_forms_links = () ->
19
23
  $child.replaceWith($("<script id='#{$child.attr('id')}' type='text/html' />").html($child.html()))
20
24
 
21
25
  $template.before( $parsed_template )
26
+ $.event.trigger("fields_added.nested_form_fields",{object_class: object_class});
22
27
  false
23
28
 
24
29
  $('body').off("click", '.remove_nested_fields_link')
25
30
  $('body').on 'click', '.remove_nested_fields_link', ->
26
31
  $link = $(this)
32
+ object_class = $link.data('object-class')
33
+ $.event.trigger("fields_removing.nested_form_fields",{object_class: object_class});
27
34
  delete_association_field_name = $link.data('delete-association-field-name')
28
35
  $nested_fields_container = $link.parents(".nested_fields").first()
29
36
  $nested_fields_container.before "<input type='hidden' name='#{delete_association_field_name}' value='1' />"
30
37
  $nested_fields_container.hide()
38
+ $.event.trigger("fields_removed.nested_form_fields",{object_class: object_class});
31
39
  false
32
40
 
33
41
  $(document).on "page:change", ->
34
- bind_nested_forms_links()
42
+ nested_form_fields.bind_nested_forms_links()
35
43
 
36
44
  jQuery ->
37
- bind_nested_forms_links()
45
+ nested_form_fields.bind_nested_forms_links()
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nested_form_fields
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-11-07 00:00:00.000000000 Z
12
+ date: 2013-11-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -253,7 +253,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
253
253
  version: '0'
254
254
  segments:
255
255
  - 0
256
- hash: -2204789679790025612
256
+ hash: 1076065072556406348
257
257
  required_rubygems_version: !ruby/object:Gem::Requirement
258
258
  none: false
259
259
  requirements:
@@ -262,7 +262,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
262
262
  version: '0'
263
263
  segments:
264
264
  - 0
265
- hash: -2204789679790025612
265
+ hash: 1076065072556406348
266
266
  requirements: []
267
267
  rubyforge_project:
268
268
  rubygems_version: 1.8.25