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

fix "has_one => has_many => has_many" name generation #232

Merged
merged 1 commit into from
Feb 3, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions spec/dummy/app/assets/stylesheets/companies.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/*
Place all the styles related to the matching controller here.
They will automatically be included in application.css.
*/
5 changes: 5 additions & 0 deletions spec/dummy/app/controllers/companies_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class CompaniesController < ApplicationController
def new
@company = Company.new
end
end
6 changes: 6 additions & 0 deletions spec/dummy/app/models/company.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Company < ActiveRecord::Base
has_one :project
accepts_nested_attributes_for :project

after_initialize 'self.build_project'
end
16 changes: 16 additions & 0 deletions spec/dummy/app/views/companies/new.html.erb
Original file line number Diff line number Diff line change
@@ -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 -%>
1 change: 1 addition & 0 deletions spec/dummy/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Dummy::Application.routes.draw do
resources :companies, :only => %w(new create)
resources :projects, :only => %w(new create)
get '/:controller/:action'

Expand Down
14 changes: 14 additions & 0 deletions spec/dummy/db/migrate/20130203095901_create_company.rb
Original file line number Diff line number Diff line change
@@ -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
9 changes: 7 additions & 2 deletions spec/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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|
Expand Down
16 changes: 16 additions & 0 deletions spec/form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion vendor/assets/javascripts/jquery_nested_form.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down
2 changes: 1 addition & 1 deletion vendor/assets/javascripts/prototype_nested_form.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down