Skip to content
This repository was archived by the owner on Dec 12, 2021. It is now read-only.

Commit ae3250b

Browse files
committed
Merge pull request #232 from basvanwesting/fix_name_generation
fix "has_one => has_many => has_many" name generation
2 parents f674457 + c84a2af commit ae3250b

File tree

10 files changed

+71
-4
lines changed

10 files changed

+71
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/*
2+
Place all the styles related to the matching controller here.
3+
They will automatically be included in application.css.
4+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class CompaniesController < ApplicationController
2+
def new
3+
@company = Company.new
4+
end
5+
end

spec/dummy/app/models/company.rb

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Company < ActiveRecord::Base
2+
has_one :project
3+
accepts_nested_attributes_for :project
4+
5+
after_initialize 'self.build_project'
6+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<%= nested_form_for @company do |f| -%>
2+
<%= f.text_field :name %>
3+
<%= f.fields_for :project do |pf| -%>
4+
<%= pf.text_field :name %>
5+
<%= pf.fields_for :tasks do |tf| -%>
6+
<%= tf.text_field :name %>
7+
<%= tf.fields_for :milestones do |mf| %>
8+
<%= mf.text_field :name %>
9+
<%= mf.link_to_remove 'Remove milestone' %>
10+
<% end %>
11+
<%= tf.link_to_add 'Add new milestone', :milestones %>
12+
<%= tf.link_to_remove 'Remove' %>
13+
<% end -%>
14+
<%= pf.link_to_add 'Add new task', :tasks %>
15+
<% end -%>
16+
<% end -%>

spec/dummy/config/routes.rb

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Dummy::Application.routes.draw do
2+
resources :companies, :only => %w(new create)
23
resources :projects, :only => %w(new create)
34
get '/:controller/:action'
45

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class CreateCompany < ActiveRecord::Migration
2+
def self.up
3+
create_table :companies do |t|
4+
t.string :name
5+
end
6+
7+
add_column :projects, :company_id, :integer
8+
end
9+
10+
def self.down
11+
remove_column :projects, :company_id
12+
drop_table :companies
13+
end
14+
end

spec/dummy/db/schema.rb

+7-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
#
1212
# It's strongly recommended to check this file into your version control system.
1313

14-
ActiveRecord::Schema.define(:version => 20120819164528) do
14+
ActiveRecord::Schema.define(:version => 20130203095901) do
15+
16+
create_table "companies", :force => true do |t|
17+
t.string "name"
18+
end
1519

1620
create_table "milestones", :force => true do |t|
1721
t.integer "task_id"
@@ -24,7 +28,8 @@
2428
end
2529

2630
create_table "projects", :force => true do |t|
27-
t.string "name"
31+
t.string "name"
32+
t.integer "company_id"
2833
end
2934

3035
create_table "tasks", :force => true do |t|

spec/form_spec.rb

+16
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,20 @@ def check_form
4747
name = find('.fields .fields input[id$=name]')[:name]
4848
name.should match(/\Aproject\[tasks_attributes\]\[\d+\]\[milestones_attributes\]\[\d+\]\[name\]\z/)
4949
end
50+
51+
it 'generates correct name for the nested input (has_one => has_many)', :js => true do
52+
visit '/companies/new?type=jquery'
53+
click_link 'Add new task'
54+
name = find('.fields .fields input[id$=name]')[:name]
55+
name.should match(/\Acompany\[project_attributes\]\[tasks_attributes\]\[\d+\]\[name\]\z/)
56+
end
57+
58+
it 'generates correct name for the nested input (has_one => has_many => has_many)', :js => true do
59+
visit '/companies/new?type=jquery'
60+
click_link 'Add new task'
61+
click_link 'Add new milestone'
62+
name = find('.fields .fields .fields input[id$=name]')[:name]
63+
name.should match(/\Acompany\[project_attributes\]\[tasks_attributes\]\[\d+\]\[milestones_attributes\]\[\d+\]\[name\]\z/)
64+
end
65+
5066
end

vendor/assets/javascripts/jquery_nested_form.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
// or for an edit form:
2222
// project[tasks_attributes][0][assignments_attributes][1]
2323
if (context) {
24-
var parentNames = context.match(/[a-z_]+_attributes/g) || [];
24+
var parentNames = context.match(/[a-z_]+_attributes(?=\]\[(new_)?\d+\])/g) || [];
2525
var parentIds = context.match(/[0-9]+/g) || [];
2626

2727
for(var i = 0; i < parentNames.length; i++) {

vendor/assets/javascripts/prototype_nested_form.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ document.observe('click', function(e, el) {
1414
// or for an edit form:
1515
// project[tasks_attributes][0][assignments_attributes][1]
1616
if(context) {
17-
var parent_names = context.match(/[a-z_]+_attributes/g) || [];
17+
var parent_names = context.match(/[a-z_]+_attributes(?=\]\[(new_)?\d+\])/g) || [];
1818
var parent_ids = context.match(/[0-9]+/g) || [];
1919

2020
for(i = 0; i < parent_names.length; i++) {

0 commit comments

Comments
 (0)