simple_nested_form 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,11 @@
|
|
1
1
|
jQuery(function($) {
|
2
|
-
|
2
|
+
function fire(obj, name, data) {
|
3
|
+
var event = new $.Event(name);
|
4
|
+
obj.trigger(event, data);
|
5
|
+
return event.result !== false;
|
6
|
+
}
|
7
|
+
|
8
|
+
$("form").delegate("a[data-association]", "click", function() {
|
3
9
|
var association = $(this).attr("data-association");
|
4
10
|
var content = $("#" + association + "_fields_blueprint").html();
|
5
11
|
|
@@ -21,18 +27,31 @@ jQuery(function($) {
|
|
21
27
|
|
22
28
|
content = content.replace(new RegExp("new_" + association, "g"), new Date().getTime());
|
23
29
|
|
24
|
-
$(this)
|
30
|
+
if(fire($(this), 'nestedFields:beforeAdd', [content, association])) {
|
31
|
+
$(this).before(content);
|
32
|
+
$(this).trigger('nestedFields:afterAdd', [$(this).prev(), association]);
|
33
|
+
}
|
34
|
+
|
25
35
|
return false;
|
26
36
|
});
|
27
37
|
|
28
|
-
$("form
|
38
|
+
$("form").delegate("a[data-remove-association]", "click", function() {
|
29
39
|
var destroyField = $(this).prev("input[type=hidden]")[0];
|
40
|
+
var element = $(this).closest(".fields");
|
30
41
|
|
31
|
-
if
|
32
|
-
destroyField
|
42
|
+
if(fire($(this), "nestedFields:beforeRemove", [element])) {
|
43
|
+
if (destroyField) {
|
44
|
+
destroyField.value = "1";
|
45
|
+
}
|
46
|
+
|
47
|
+
var waitForPossibleAnimations = setInterval(function() {
|
48
|
+
if(!element.is(":animated")) {
|
49
|
+
clearInterval(waitForPossibleAnimations);
|
50
|
+
element.hide();
|
51
|
+
}
|
52
|
+
}, 100);
|
33
53
|
}
|
34
54
|
|
35
|
-
$(this).closest(".fields").hide();
|
36
55
|
return false;
|
37
56
|
});
|
38
57
|
});
|
@@ -1,8 +1,19 @@
|
|
1
1
|
module SimpleNestedForm
|
2
2
|
class Builder < ::SimpleForm::FormBuilder
|
3
|
-
delegate :content_tag, :link_to, :after_nested_form, :to => :@template
|
3
|
+
delegate :content_tag, :link_to, :after_nested_form, :capture, :to => :@template
|
4
|
+
|
5
|
+
def link_to_add(*args, &block)
|
6
|
+
if block_given?
|
7
|
+
association = args.first
|
8
|
+
options = args.second || {}
|
9
|
+
else
|
10
|
+
name = args.first
|
11
|
+
association = args.second
|
12
|
+
options = args.third || {}
|
13
|
+
end
|
14
|
+
|
15
|
+
options.merge!(:"data-association" => association)
|
4
16
|
|
5
|
-
def link_to_add(name, association)
|
6
17
|
after_nested_form(association) do
|
7
18
|
model_object = object.class.reflect_on_association(association).klass.new
|
8
19
|
|
@@ -11,11 +22,28 @@ module SimpleNestedForm
|
|
11
22
|
end
|
12
23
|
end
|
13
24
|
|
14
|
-
|
25
|
+
if block_given?
|
26
|
+
link_to(capture(&block), "javascript:void(0)", options)
|
27
|
+
else
|
28
|
+
link_to(name, "javascript:void(0)", options)
|
29
|
+
end
|
15
30
|
end
|
16
31
|
|
17
|
-
def link_to_remove(
|
18
|
-
|
32
|
+
def link_to_remove(*args, &block)
|
33
|
+
if block_given?
|
34
|
+
options = args.first || {}
|
35
|
+
else
|
36
|
+
name = args.first
|
37
|
+
options = args.second || {}
|
38
|
+
end
|
39
|
+
|
40
|
+
options.merge!(:"data-remove-association" => 1)
|
41
|
+
|
42
|
+
hidden_field(:_destroy) + if block_given?
|
43
|
+
link_to(capture(&block), "javascript:void(0)", options)
|
44
|
+
else
|
45
|
+
link_to(name, "javascript:void(0)", options)
|
46
|
+
end
|
19
47
|
end
|
20
48
|
|
21
49
|
def fields_for(record_or_name_or_array, *args, &block)
|
@@ -16,35 +16,75 @@ describe SimpleNestedForm::Builder do
|
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
|
-
|
20
|
-
|
19
|
+
shared_examples_for "a link_to_add" do |link_name|
|
20
|
+
let(:link) { builder.link_to_add(link_name, :tasks, :class => "add") }
|
21
21
|
|
22
|
-
|
22
|
+
subject { Nokogiri::HTML(link).css("a").first }
|
23
23
|
|
24
|
-
|
25
|
-
subject.attr("class").should include "add_nested_fields"
|
26
|
-
end
|
24
|
+
its(:text) { should == link_name }
|
27
25
|
|
28
26
|
it "contains an attribute named data-association" do
|
29
27
|
subject.attr("data-association").should == "tasks"
|
30
28
|
end
|
29
|
+
|
30
|
+
describe "options" do
|
31
|
+
it "contains a class named add" do
|
32
|
+
subject.attr("class").should include "add"
|
33
|
+
end
|
34
|
+
end
|
31
35
|
end
|
32
36
|
|
33
|
-
|
34
|
-
let(:
|
37
|
+
shared_examples_for "a link_to_remove" do |link_name|
|
38
|
+
let(:link) { builder.link_to_remove(link_name, :class => "remove") }
|
35
39
|
|
36
|
-
|
37
|
-
subject { output.css("a").first }
|
40
|
+
subject { Nokogiri::HTML(link).css("a").first }
|
38
41
|
|
39
|
-
|
42
|
+
its(:text) { should == link_name }
|
40
43
|
|
41
|
-
|
42
|
-
|
43
|
-
end
|
44
|
+
it "contains an attribute named data-remove-association" do
|
45
|
+
subject.attr("data-remove-association").should be_present
|
44
46
|
end
|
45
47
|
|
46
48
|
it "generate a destroy hidden field" do
|
47
|
-
|
49
|
+
Nokogiri::HTML(link).css("input[type='hidden'][name*='_destroy']").should have(1).element
|
50
|
+
end
|
51
|
+
|
52
|
+
describe "options" do
|
53
|
+
it "contains a class named add" do
|
54
|
+
subject.attr("class").should include "remove"
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe "link to add" do
|
60
|
+
context "without a block" do
|
61
|
+
it_should_behave_like "a link_to_add", "Add me plz" do
|
62
|
+
let(:link) { builder.link_to_add("Add me plz", :tasks, :class => "add") }
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
context "with a block" do
|
67
|
+
it_should_behave_like "a link_to_add", "A label in a block baby!" do
|
68
|
+
let(:link) do
|
69
|
+
builder.link_to_add(:tasks, :class => "add") { "A label in a block baby!" }
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
describe "link to remove" do
|
76
|
+
context "without a block" do
|
77
|
+
it_should_behave_like "a link_to_remove", "Remove me plz" do
|
78
|
+
let(:link) { builder.link_to_remove("Remove me plz", :class => "remove") }
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
context "with a block" do
|
83
|
+
it_should_behave_like "a link_to_remove", "Remove me plz" do
|
84
|
+
let(:link) do
|
85
|
+
builder.link_to_remove(:class => "remove") { "Remove me plz" }
|
86
|
+
end
|
87
|
+
end
|
48
88
|
end
|
49
89
|
end
|
50
90
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_nested_form
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Rodrigo Navarro
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2011-03-
|
19
|
+
date: 2011-03-08 00:00:00 -03:00
|
20
20
|
default_executable:
|
21
21
|
dependencies:
|
22
22
|
- !ruby/object:Gem::Dependency
|