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

fields_blueprint_id_for takes root object into account #300

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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: 3 additions & 1 deletion lib/nested_form/builder_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ def fields_for_nested_model(name, object, options, block)
private

def fields_blueprint_id_for(association)
assocs = object_name.to_s.scan(/(\w+)_attributes/).map(&:first)
assocs = []
assocs << [object_name.to_s.split("[")[0]] # root
assocs.concat(object_name.to_s.scan(/(\w+)_attributes/).map(&:first)) # parent associations
assocs << association
assocs.join('_') + '_fields_blueprint'
end
Expand Down
10 changes: 5 additions & 5 deletions spec/nested_form/builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@

describe '#link_to_add' do
it "behaves similar to a Rails link_to" do
subject.link_to_add("Add", :tasks).should == '<a href="javascript:void(0)" class="add_nested_fields" data-association="tasks" data-blueprint-id="tasks_fields_blueprint">Add</a>'
subject.link_to_add("Add", :tasks, :class => "foo", :href => "url").should == '<a href="url" class="foo add_nested_fields" data-association="tasks" data-blueprint-id="tasks_fields_blueprint">Add</a>'
subject.link_to_add(:tasks) { "Add" }.should == '<a href="javascript:void(0)" class="add_nested_fields" data-association="tasks" data-blueprint-id="tasks_fields_blueprint">Add</a>'
subject.link_to_add("Add", :tasks).should == '<a href="javascript:void(0)" class="add_nested_fields" data-association="tasks" data-blueprint-id="item_tasks_fields_blueprint">Add</a>'
subject.link_to_add("Add", :tasks, :class => "foo", :href => "url").should == '<a href="url" class="foo add_nested_fields" data-association="tasks" data-blueprint-id="item_tasks_fields_blueprint">Add</a>'
subject.link_to_add(:tasks) { "Add" }.should == '<a href="javascript:void(0)" class="add_nested_fields" data-association="tasks" data-blueprint-id="item_tasks_fields_blueprint">Add</a>'
end

it 'raises ArgumentError when missing association is provided' do
Expand Down Expand Up @@ -115,15 +115,15 @@
output.should match(/div.+data-blueprint="#{expected}"/)
end

it "adds parent association name to the blueprint div id" do
it "adds parent associations to the blueprint div id" do
task = project.tasks.build
task.milestones.build
subject.fields_for(:tasks, :builder => builder) do |tf|
tf.fields_for(:milestones, :builder => builder) { 'Milestone' }
tf.link_to_add('Add', :milestones)
end
output = template.send(:after_nested_form_callbacks)
output.should match(/div.+id="tasks_milestones_fields_blueprint"/)
output.should match(/div.+id="item_tasks_milestones_fields_blueprint"/)
end

it "doesn't render wrapper div" do
Expand Down