Permalink
Browse files

upgrading nested_form gem

  • Loading branch information...
1 parent b771465 commit 99f5680c82154602d8ece22f0470aee8a2729094 @ryanb committed Apr 23, 2011
Showing with 79 additions and 78 deletions.
  1. +1 −1 Gemfile
  2. +34 −34 Gemfile.lock
  3. +1 −3 app/views/surveys/_form.html.erb
  4. +1 −3 app/views/surveys/_question_fields.html.erb
  5. +42 −37 public/javascripts/nested_form.js
View
@@ -1,6 +1,6 @@
source 'http://rubygems.org'
-gem 'rails', '3.0.4'
+gem 'rails', '3.0.7'
gem 'sqlite3'
gem 'nifty-generators'
gem "mocha", :group => :test
View
@@ -2,74 +2,74 @@ GEM
remote: http://rubygems.org/
specs:
abstract (1.0.0)
- actionmailer (3.0.4)
- actionpack (= 3.0.4)
+ actionmailer (3.0.7)
+ actionpack (= 3.0.7)
mail (~> 2.2.15)
- actionpack (3.0.4)
- activemodel (= 3.0.4)
- activesupport (= 3.0.4)
+ actionpack (3.0.7)
+ activemodel (= 3.0.7)
+ activesupport (= 3.0.7)
builder (~> 2.1.2)
erubis (~> 2.6.6)
- i18n (~> 0.4)
+ i18n (~> 0.5.0)
rack (~> 1.2.1)
- rack-mount (~> 0.6.13)
+ rack-mount (~> 0.6.14)
rack-test (~> 0.5.7)
tzinfo (~> 0.3.23)
- activemodel (3.0.4)
- activesupport (= 3.0.4)
+ activemodel (3.0.7)
+ activesupport (= 3.0.7)
builder (~> 2.1.2)
- i18n (~> 0.4)
- activerecord (3.0.4)
- activemodel (= 3.0.4)
- activesupport (= 3.0.4)
+ i18n (~> 0.5.0)
+ activerecord (3.0.7)
+ activemodel (= 3.0.7)
+ activesupport (= 3.0.7)
arel (~> 2.0.2)
tzinfo (~> 0.3.23)
- activeresource (3.0.4)
- activemodel (= 3.0.4)
- activesupport (= 3.0.4)
- activesupport (3.0.4)
- arel (2.0.8)
+ activeresource (3.0.7)
+ activemodel (= 3.0.7)
+ activesupport (= 3.0.7)
+ activesupport (3.0.7)
+ arel (2.0.9)
builder (2.1.2)
erubis (2.6.6)
abstract (>= 1.0.0)
i18n (0.5.0)
jquery-rails (0.2.7)
rails (~> 3.0)
thor (~> 0.14.4)
- mail (2.2.15)
+ mail (2.2.17)
activesupport (>= 2.3.6)
i18n (>= 0.4.0)
mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.16)
mocha (0.9.12)
- nested_form (0.0.0)
+ nested_form (0.1.1)
nifty-generators (0.4.5)
polyglot (0.3.1)
- rack (1.2.1)
- rack-mount (0.6.13)
+ rack (1.2.2)
+ rack-mount (0.6.14)
rack (>= 1.0.0)
rack-test (0.5.7)
rack (>= 1.0)
- rails (3.0.4)
- actionmailer (= 3.0.4)
- actionpack (= 3.0.4)
- activerecord (= 3.0.4)
- activeresource (= 3.0.4)
- activesupport (= 3.0.4)
+ rails (3.0.7)
+ actionmailer (= 3.0.7)
+ actionpack (= 3.0.7)
+ activerecord (= 3.0.7)
+ activeresource (= 3.0.7)
+ activesupport (= 3.0.7)
bundler (~> 1.0)
- railties (= 3.0.4)
- railties (3.0.4)
- actionpack (= 3.0.4)
- activesupport (= 3.0.4)
+ railties (= 3.0.7)
+ railties (3.0.7)
+ actionpack (= 3.0.7)
+ activesupport (= 3.0.7)
rake (>= 0.8.7)
thor (~> 0.14.4)
rake (0.8.7)
sqlite3 (1.3.3)
thor (0.14.6)
treetop (1.4.9)
polyglot (>= 0.3.1)
- tzinfo (0.3.24)
+ tzinfo (0.3.26)
PLATFORMS
ruby
@@ -79,5 +79,5 @@ DEPENDENCIES
mocha
nested_form
nifty-generators
- rails (= 3.0.4)
+ rails (= 3.0.7)
sqlite3
@@ -4,9 +4,7 @@
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
- <%= f.fields_for :questions do |builder| %>
- <%= render "question_fields", :f => builder %>
- <% end %>
+ <%= f.fields_for :questions %>
<p><%= f.link_to_add "Add a question", :questions %></p>
<p><%= f.submit "Submit" %></p>
<% end %>
@@ -3,7 +3,5 @@
<%= f.text_area :content, :rows => 3 %><br />
<%= f.link_to_remove "remove" %>
</p>
-<%= f.fields_for :answers do |builder| %>
- <%= render "answer_fields", :f => builder %>
-<% end %>
+<%= f.fields_for :answers %>
<p><%= f.link_to_add "Add an answer", :answers %></p>
@@ -1,46 +1,51 @@
-$(function() {
-$('form a.add_nested_fields').live('click', function() {
- // Setup
- var assoc = $(this).attr('data-association'); // Name of child
- var content = $('#' + assoc + '_fields_blueprint').html(); // Fields template
+jQuery(function($) {
+ $('form a.add_nested_fields').live('click', function() {
+ // Setup
+ var assoc = $(this).attr('data-association'); // Name of child
+ var content = $('#' + assoc + '_fields_blueprint').html(); // Fields template
- // Make the context correct by replacing new_<parents> with the generated ID
- // of each of the parent objects
- var context = ($(this).closest('.fields').find('input:first').attr('name') || '').replace(new RegExp('\[[a-z]+\]$'), '');
+ // Make the context correct by replacing new_<parents> with the generated ID
+ // of each of the parent objects
+ var context = ($(this).closest('.fields').find('input:first').attr('name') || '').replace(new RegExp('\[[a-z]+\]$'), '');
- // context will be something like this for a brand new form:
- // project[tasks_attributes][1255929127459][assignments_attributes][1255929128105]
- // or for an edit form:
- // project[tasks_attributes][0][assignments_attributes][1]
- if(context) {
- var parent_names = context.match(/[a-z_]+_attributes/g) || [];
- var parent_ids = context.match(/[0-9]+/g);
+ // context will be something like this for a brand new form:
+ // project[tasks_attributes][new_1255929127459][assignments_attributes][new_1255929128105]
+ // or for an edit form:
+ // project[tasks_attributes][0][assignments_attributes][1]
+ if(context) {
+ var parent_names = context.match(/[a-z_]+_attributes/g) || [];
+ var parent_ids = context.match(/(new_)?[0-9]+/g) || [];
- for(i = 0; i < parent_names.length; i++) {
- if(parent_ids[i]) {
- content = content.replace(
- new RegExp('(\\[' + parent_names[i] + '\\])\\[.+?\\]', 'g'),
- '$1[' + parent_ids[i] + ']'
- )
+ for(i = 0; i < parent_names.length; i++) {
+ if(parent_ids[i]) {
+ content = content.replace(
+ new RegExp('(_' + parent_names[i] + ')_.+?_', 'g'),
+ '$1_' + parent_ids[i] + '_');
+
+ content = content.replace(
+ new RegExp('(\\[' + parent_names[i] + '\\])\\[.+?\\]', 'g'),
+ '$1[' + parent_ids[i] + ']');
+ }
}
}
- }
- // Make a unique ID for the new child
- var regexp = new RegExp('new_' + assoc, 'g');
- var new_id = new Date().getTime();
- content = content.replace(regexp, new_id);
+ // Make a unique ID for the new child
+ var regexp = new RegExp('new_' + assoc, 'g');
+ var new_id = new Date().getTime();
+ content = content.replace(regexp, "new_" + new_id);
- $(this).before(content);
- return false;
-});
+ $(this).before(content);
+ $(this).closest("form").trigger('nested:fieldAdded');
+ return false;
+ });
-$('form a.remove_nested_fields').live('click', function() {
- var hidden_field = $(this).prev('input[type=hidden]')[0];
- if(hidden_field) {
- hidden_field.value = '1';
- }
- $(this).closest('.fields').hide();
- return false;
+ $('form a.remove_nested_fields').live('click', function() {
+ var hidden_field = $(this).prev('input[type=hidden]')[0];
+ if(hidden_field) {
+ hidden_field.value = '1';
+ }
+ $(this).closest('.fields').hide();
+ $(this).closest("form").trigger('nested:fieldRemoved');
+ return false;
+ });
});
-});

0 comments on commit 99f5680

Please sign in to comment.