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

Make the fields positionable, before, after, ... a given node (fix Issue #66) #70

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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ Gemfile.lock
.bundle
log
gemfiles
/.idea/
24 changes: 23 additions & 1 deletion lib/generators/nested_form/templates/jquery_nested_form.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ jQuery(function($) {
var assoc = $(this).attr('data-association'); // Name of child
var content = $('#' + assoc + '_fields_blueprint').html(); // Fields template

var insert_node = $(this).attr('data-insert-node') || this; // Insertion Node
var insert_position = $(this).attr('data-insert-position') || 'before'; // Insertion Position

// 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]+\]$'), '');
Expand Down Expand Up @@ -34,7 +37,26 @@ jQuery(function($) {
var new_id = new Date().getTime();
content = content.replace(regexp, "new_" + new_id);

var field = $(content).insertBefore(this);

insert_node = $(this).closest("form").find(insert_node);
// var field = $(content).insertBefore(this);
switch(insert_position){
case 'before':
var field = $(content).insertBefore(insert_node);
break;
case 'after':
var field = $(content).insertAfter(insert_node);
break;
case 'top':
var field = $(content).prependTo(insert_node);
break;
case 'bottom':
var field = $(content).appendTo(insert_node);
break;
default:
var field = $(content).insertBefore(this);
}

$(this).closest("form").trigger({type: 'nested:fieldAdded', field: field});
return false;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ document.observe('click', function(e, el) {
var assoc = el.readAttribute('data-association'); // Name of child
var content = $(assoc + '_fields_blueprint').innerHTML; // Fields template

var insert_node = el.readAttribute('data-insert-node') || el; // Insertion Node
var insert_position = el.readAttribute('data-insert-position') || 'before'; // Insertion Position

// Make the context correct by replacing new_<parents> with the generated ID
// of each of the parent objects
var context = (el.getOffsetParent('.fields').firstDescendant().readAttribute('name') || '').replace(new RegExp('\[[a-z]+\]$'), '');
Expand Down Expand Up @@ -34,7 +37,8 @@ document.observe('click', function(e, el) {
var new_id = new Date().getTime();
content = content.replace(regexp, "new_" + new_id);

el.insert({ before: content });
if (Object.isString(insert_node)) insert_node = e.findElement('form '+insert_node)
insert_node.insert({ insert_position: content });
return false;
}
});
Expand Down
4 changes: 4 additions & 0 deletions lib/nested_form/builder_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ def link_to_add(*args, &block)
association = args.pop
options[:class] = [options[:class], "add_nested_fields"].compact.join(" ")
options["data-association"] = association

options["data-insert-node"] = options.delete(:node)
options["data-insert-position"] = options.delete(:position)

args << (options.delete(:href) || "javascript:void(0)")
args << options
@fields ||= {}
Expand Down
2 changes: 1 addition & 1 deletion nested_form.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Gem::Specification.new do |s|

s.add_development_dependency "rspec-rails", "~> 2.6.0"
s.add_development_dependency "mocha"
# s.add_development_dependency "rails", "~> 3.1.0.rc"
s.add_development_dependency "rails", "~> 3.0.8"

s.rubyforge_project = s.name
s.required_rubygems_version = ">= 1.3.4"
Expand Down
6 changes: 6 additions & 0 deletions spec/nested_form/builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
@builder.link_to_add(:tasks) { "Add" }.should == '<a href="javascript:void(0)" class="add_nested_fields" data-association="tasks">Add</a>'
end

it "has an add link with positionable attributes" do
@builder.link_to_add("Add", :tasks, :position => :before).should == '<a href="javascript:void(0)" class="add_nested_fields" data-association="tasks" data-insert-position="before">Add</a>'
@builder.link_to_add("Add", :tasks, :node => 'table').should == '<a href="javascript:void(0)" class="add_nested_fields" data-association="tasks" data-insert-node="table">Add</a>'
@builder.link_to_add("Add", :tasks, :node => 'table', :position => :before).should == '<a href="javascript:void(0)" class="add_nested_fields" data-association="tasks" data-insert-node="table" data-insert-position="before">Add</a>'
end

it "has a remove link which behaves similar to a Rails link_to" do
@builder.link_to_remove("Remove").should == '<input id="item__destroy" name="item[_destroy]" type="hidden" value="false" /><a href="javascript:void(0)" class="remove_nested_fields">Remove</a>'
@builder.link_to_remove("Remove", :class => "foo", :href => "url").should == '<input id="item__destroy" name="item[_destroy]" type="hidden" value="false" /><a href="url" class="foo remove_nested_fields">Remove</a>'
Expand Down