diff --git a/spec/dummy/app/assets/stylesheets/companies.css b/spec/dummy/app/assets/stylesheets/companies.css new file mode 100644 index 00000000..afad32db --- /dev/null +++ b/spec/dummy/app/assets/stylesheets/companies.css @@ -0,0 +1,4 @@ +/* + Place all the styles related to the matching controller here. + They will automatically be included in application.css. +*/ diff --git a/spec/dummy/app/controllers/companies_controller.rb b/spec/dummy/app/controllers/companies_controller.rb new file mode 100644 index 00000000..e6cf2b4b --- /dev/null +++ b/spec/dummy/app/controllers/companies_controller.rb @@ -0,0 +1,5 @@ +class CompaniesController < ApplicationController + def new + @company = Company.new + end +end diff --git a/spec/dummy/app/models/company.rb b/spec/dummy/app/models/company.rb new file mode 100644 index 00000000..1744451f --- /dev/null +++ b/spec/dummy/app/models/company.rb @@ -0,0 +1,6 @@ +class Company < ActiveRecord::Base + has_one :project + accepts_nested_attributes_for :project + + after_initialize 'self.build_project' +end diff --git a/spec/dummy/app/views/companies/new.html.erb b/spec/dummy/app/views/companies/new.html.erb new file mode 100644 index 00000000..682e2f80 --- /dev/null +++ b/spec/dummy/app/views/companies/new.html.erb @@ -0,0 +1,16 @@ +<%= nested_form_for @company do |f| -%> + <%= f.text_field :name %> + <%= f.fields_for :project do |pf| -%> + <%= pf.text_field :name %> + <%= pf.fields_for :tasks do |tf| -%> + <%= tf.text_field :name %> + <%= tf.fields_for :milestones do |mf| %> + <%= mf.text_field :name %> + <%= mf.link_to_remove 'Remove milestone' %> + <% end %> + <%= tf.link_to_add 'Add new milestone', :milestones %> + <%= tf.link_to_remove 'Remove' %> + <% end -%> + <%= pf.link_to_add 'Add new task', :tasks %> + <% end -%> +<% end -%> diff --git a/spec/dummy/config/routes.rb b/spec/dummy/config/routes.rb index 87381ab0..6f29827f 100644 --- a/spec/dummy/config/routes.rb +++ b/spec/dummy/config/routes.rb @@ -1,4 +1,5 @@ Dummy::Application.routes.draw do + resources :companies, :only => %w(new create) resources :projects, :only => %w(new create) get '/:controller/:action' diff --git a/spec/dummy/db/migrate/20130203095901_create_company.rb b/spec/dummy/db/migrate/20130203095901_create_company.rb new file mode 100644 index 00000000..e56afbfc --- /dev/null +++ b/spec/dummy/db/migrate/20130203095901_create_company.rb @@ -0,0 +1,14 @@ +class CreateCompany < ActiveRecord::Migration + def self.up + create_table :companies do |t| + t.string :name + end + + add_column :projects, :company_id, :integer + end + + def self.down + remove_column :projects, :company_id + drop_table :companies + end +end diff --git a/spec/dummy/db/schema.rb b/spec/dummy/db/schema.rb index 99752680..bae4c06d 100644 --- a/spec/dummy/db/schema.rb +++ b/spec/dummy/db/schema.rb @@ -11,7 +11,11 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20120819164528) do +ActiveRecord::Schema.define(:version => 20130203095901) do + + create_table "companies", :force => true do |t| + t.string "name" + end create_table "milestones", :force => true do |t| t.integer "task_id" @@ -24,7 +28,8 @@ end create_table "projects", :force => true do |t| - t.string "name" + t.string "name" + t.integer "company_id" end create_table "tasks", :force => true do |t| diff --git a/spec/form_spec.rb b/spec/form_spec.rb index e0a42c8e..2701051b 100644 --- a/spec/form_spec.rb +++ b/spec/form_spec.rb @@ -47,4 +47,20 @@ def check_form name = find('.fields .fields input[id$=name]')[:name] name.should match(/\Aproject\[tasks_attributes\]\[\d+\]\[milestones_attributes\]\[\d+\]\[name\]\z/) end + + it 'generates correct name for the nested input (has_one => has_many)', :js => true do + visit '/companies/new?type=jquery' + click_link 'Add new task' + name = find('.fields .fields input[id$=name]')[:name] + name.should match(/\Acompany\[project_attributes\]\[tasks_attributes\]\[\d+\]\[name\]\z/) + end + + it 'generates correct name for the nested input (has_one => has_many => has_many)', :js => true do + visit '/companies/new?type=jquery' + click_link 'Add new task' + click_link 'Add new milestone' + name = find('.fields .fields .fields input[id$=name]')[:name] + name.should match(/\Acompany\[project_attributes\]\[tasks_attributes\]\[\d+\]\[milestones_attributes\]\[\d+\]\[name\]\z/) + end + end diff --git a/vendor/assets/javascripts/jquery_nested_form.js b/vendor/assets/javascripts/jquery_nested_form.js index 5c39f4d9..3b8a5b39 100644 --- a/vendor/assets/javascripts/jquery_nested_form.js +++ b/vendor/assets/javascripts/jquery_nested_form.js @@ -21,7 +21,7 @@ // or for an edit form: // project[tasks_attributes][0][assignments_attributes][1] if (context) { - var parentNames = context.match(/[a-z_]+_attributes/g) || []; + var parentNames = context.match(/[a-z_]+_attributes(?=\]\[(new_)?\d+\])/g) || []; var parentIds = context.match(/[0-9]+/g) || []; for(var i = 0; i < parentNames.length; i++) { diff --git a/vendor/assets/javascripts/prototype_nested_form.js b/vendor/assets/javascripts/prototype_nested_form.js index dadcb9be..5261755b 100644 --- a/vendor/assets/javascripts/prototype_nested_form.js +++ b/vendor/assets/javascripts/prototype_nested_form.js @@ -14,7 +14,7 @@ document.observe('click', function(e, el) { // 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_names = context.match(/[a-z_]+_attributes(?=\]\[(new_)?\d+\])/g) || []; var parent_ids = context.match(/[0-9]+/g) || []; for(i = 0; i < parent_names.length; i++) {