nested_form 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -13,8 +13,7 @@ jQuery(function($) {
13
13
 
14
14
  // Make the context correct by replacing new_<parents> with the generated ID
15
15
  // of each of the parent objects
16
- var context = ($(link).closest('.fields').closestChild('input:first').attr('name') || '').replace(new RegExp('\[[a-z]+\]$'), '');
17
-
16
+ var context = ($(link).closest('.fields').find('input:first').attr('name') || '').replace(new RegExp('\[[a-z]+\]$'), '');
18
17
 
19
18
  // context will be something like this for a brand new form:
20
19
  // project[tasks_attributes][new_1255929127459][assignments_attributes][new_1255929128105]
@@ -70,34 +69,3 @@ jQuery(function($) {
70
69
  $('form a.add_nested_fields').live('click', nestedFormEvents.addFields);
71
70
  $('form a.remove_nested_fields').live('click', nestedFormEvents.removeFields);
72
71
  });
73
-
74
-
75
- // http://plugins.jquery.com/project/closestChild
76
- /*
77
- * Copyright 2011, Tobias Lindig
78
- *
79
- * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
80
- * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
81
- *
82
- */
83
- (function($) {
84
- $.fn.closestChild = function(selector) {
85
- // breadth first search for the first matched node
86
- if (selector && selector != '') {
87
- var queue = [];
88
- queue.push(this);
89
- while(queue.length > 0) {
90
- var node = queue.shift();
91
- var children = node.children();
92
- for(var i = 0; i < children.length; ++i) {
93
- var child = $(children[i]);
94
- if (child.is(selector)) {
95
- return child; //well, we found one
96
- }
97
- queue.push(child);
98
- }
99
- }
100
- }
101
- return $();//nothing found
102
- };
103
- })(jQuery);
@@ -1,3 +1,5 @@
1
1
  class Task < ActiveRecord::Base
2
2
  belongs_to :project
3
+ has_many :milestones
4
+ accepts_nested_attributes_for :milestones
3
5
  end
@@ -6,9 +6,14 @@
6
6
 
7
7
  <%= nested_form_for @project do |f| -%>
8
8
  <%= f.text_field :name %>
9
- <%= f.fields_for :tasks do |ff| -%>
10
- <%= ff.text_field :name %>
11
- <%= ff.link_to_remove 'Remove' %>
9
+ <%= f.fields_for :tasks do |tf| -%>
10
+ <%= tf.text_field :name %>
11
+ <%= tf.fields_for :milestones do |mf| %>
12
+ <%= mf.text_field :name %>
13
+ <%= mf.link_to_remove 'Remove milestone' %>
14
+ <% end %>
15
+ <%= tf.link_to_add 'Add new milestone', :milestones %>
16
+ <%= tf.link_to_remove 'Remove' %>
12
17
  <% end -%>
13
18
  <%= f.link_to_add 'Add new task', :tasks %>
14
19
  <% end -%>
@@ -1,10 +1,10 @@
1
1
  require 'rubygems'
2
2
 
3
- gemfile = ENV['BUNDLE_GEMFILE'] || File.expand_path('../../../../gemfiles/Gemfile.rails3_1', __FILE__)
3
+ gemfile = ENV['BUNDLE_GEMFILE'] || File.expand_path('../../../../Gemfile', __FILE__)
4
4
  if File.exist?(gemfile)
5
5
  ENV['BUNDLE_GEMFILE'] = gemfile
6
6
  require 'bundler'
7
7
  Bundler.setup
8
8
  end
9
9
 
10
- $:.unshift File.expand_path('../../../../lib', __FILE__)
10
+ $:.unshift File.expand_path('../../../../lib', __FILE__)
@@ -30,4 +30,12 @@ describe 'NestedForm' do
30
30
  visit '/projects/new?type=prototype'
31
31
  check_form
32
32
  end
33
+
34
+ it 'generates correct name for the nested input', :js => true do
35
+ visit '/projects/new?type=jquery'
36
+ click_link 'Add new task'
37
+ click_link 'Add new milestone'
38
+ name = find('.fields .fields input[id$=name]')[:name]
39
+ name.should match(/\Aproject\[tasks_attributes\]\[new_\d+\]\[milestones_attributes\]\[new_\d+\]\[name\]\z/)
40
+ end
33
41
  end
@@ -13,8 +13,7 @@ jQuery(function($) {
13
13
 
14
14
  // Make the context correct by replacing new_<parents> with the generated ID
15
15
  // of each of the parent objects
16
- var context = ($(link).closest('.fields').closestChild('input:first').attr('name') || '').replace(new RegExp('\[[a-z]+\]$'), '');
17
-
16
+ var context = ($(link).closest('.fields').find('input:first').attr('name') || '').replace(new RegExp('\[[a-z]+\]$'), '');
18
17
 
19
18
  // context will be something like this for a brand new form:
20
19
  // project[tasks_attributes][new_1255929127459][assignments_attributes][new_1255929128105]
@@ -70,34 +69,3 @@ jQuery(function($) {
70
69
  $('form a.add_nested_fields').live('click', nestedFormEvents.addFields);
71
70
  $('form a.remove_nested_fields').live('click', nestedFormEvents.removeFields);
72
71
  });
73
-
74
-
75
- // http://plugins.jquery.com/project/closestChild
76
- /*
77
- * Copyright 2011, Tobias Lindig
78
- *
79
- * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
80
- * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
81
- *
82
- */
83
- (function($) {
84
- $.fn.closestChild = function(selector) {
85
- // breadth first search for the first matched node
86
- if (selector && selector != '') {
87
- var queue = [];
88
- queue.push(this);
89
- while(queue.length > 0) {
90
- var node = queue.shift();
91
- var children = node.children();
92
- for(var i = 0; i < children.length; ++i) {
93
- var child = $(children[i]);
94
- if (child.is(selector)) {
95
- return child; //well, we found one
96
- }
97
- queue.push(child);
98
- }
99
- }
100
- }
101
- return $();//nothing found
102
- };
103
- })(jQuery);
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nested_form
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-06-04 00:00:00.000000000 Z
13
+ date: 2012-07-09 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rake