nested_fields_rails 0.0.3 → 0.0.4
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,4 +1,4 @@
|
|
1
|
-
module NestedFieldsRails
|
2
|
-
class Engine < ::Rails::Engine
|
3
|
-
end
|
1
|
+
module NestedFieldsRails
|
2
|
+
class Engine < ::Rails::Engine
|
3
|
+
end
|
4
4
|
end
|
@@ -1,67 +1,67 @@
|
|
1
|
-
module NestedFieldsRails
|
2
|
-
class ::ActionView::Helpers::FormBuilder
|
3
|
-
|
4
|
-
def link_to_add_nested_fields(*args, &block)
|
5
|
-
options = args.extract_options!.symbolize_keys
|
6
|
-
association = args.pop
|
7
|
-
options[:class] = [options[:class], 'add-nested-fields'].compact.join(' ')
|
8
|
-
options['data-association'] = association
|
9
|
-
args << (options.delete(:href) || 'javascript:void(0)')
|
10
|
-
args << options
|
11
|
-
@template.link_to(*args, &block)
|
12
|
-
end
|
13
|
-
|
14
|
-
def link_to_remove_nested_fields(*args, &block)
|
15
|
-
(hidden_field(:_destroy, :class => 'destroy') << link_to_action_nested_fields(:remove, *args, &block)).html_safe
|
16
|
-
end
|
17
|
-
|
18
|
-
def link_to_up_nested_fields(*args, &block)
|
19
|
-
link_to_action_nested_fields(:up, *args, &block)
|
20
|
-
end
|
21
|
-
|
22
|
-
def link_to_down_nested_fields(*args, &block)
|
23
|
-
link_to_action_nested_fields(:down, *args, &block)
|
24
|
-
end
|
25
|
-
|
26
|
-
def index_nested_fields(attribute_name)
|
27
|
-
hidden_field attribute_name, :class => 'index'
|
28
|
-
end
|
29
|
-
|
30
|
-
def nested_fields_for(association_name, record_object = nil, fields_options = {}, &block)
|
31
|
-
collection_wrapper = fields_options[:collection_wrapper] || (fields_options[:wrapper] && fields_options[:wrapper].to_sym == :table ? :tbody : nil) || :div
|
32
|
-
element_wrapper = fields_options[:element_wrapper] || (fields_options[:wrapper] && fields_options[:wrapper].to_sym == :table ? :tr : nil) || :div
|
33
|
-
|
34
|
-
fields_block = block || Proc.new { |fields| @template.render(:partial => "#{association_name.to_s.singularize}_fields", :locals => {:f => fields}) }
|
35
|
-
fields = @template.content_tag(collection_wrapper, fields_for(association_name, record_object, fields_options, &fields_block), :id => association_name, :class => 'nested-fields')
|
36
|
-
|
37
|
-
model_object = object.class.reflect_on_association(association_name).klass.new
|
38
|
-
template = fields_for(association_name, model_object, :element_wrapper => element_wrapper, :child_index => "new_#{association_name}", &fields_block)
|
39
|
-
template_options = {:id => "#{association_name}_fields_template", :type => 'text/x-jquery-tmpl'}
|
40
|
-
fields_template = @template.content_tag(:script, template, template_options)
|
41
|
-
|
42
|
-
fields + fields_template
|
43
|
-
end
|
44
|
-
|
45
|
-
alias default_fields_for_nested_model fields_for_nested_model
|
46
|
-
def fields_for_nested_model(name, object, options, block)
|
47
|
-
tag = options[:element_wrapper] || (options[:wrapper] && options[:wrapper].to_sym == :table ? :tr : nil) || :div
|
48
|
-
html_attributes = {
|
49
|
-
:class => 'fields',
|
50
|
-
'data-object-id' => options[:child_index] || object.object_id,
|
51
|
-
:style => object._destroy ? 'display: none;' : ''
|
52
|
-
}
|
53
|
-
@template.content_tag(tag, default_fields_for_nested_model(name, object, options, block), html_attributes)
|
54
|
-
end
|
55
|
-
|
56
|
-
private
|
57
|
-
|
58
|
-
def link_to_action_nested_fields(action, *args, &block)
|
59
|
-
options = args.extract_options!.symbolize_keys
|
60
|
-
options[:class] = [options[:class], "#{action}-nested-fields"].compact.join(" ")
|
61
|
-
args << (options.delete(:href) || 'javascript:void(0)')
|
62
|
-
args << options
|
63
|
-
@template.link_to(*args, &block)
|
64
|
-
end
|
65
|
-
|
66
|
-
end
|
1
|
+
module NestedFieldsRails
|
2
|
+
class ::ActionView::Helpers::FormBuilder
|
3
|
+
|
4
|
+
def link_to_add_nested_fields(*args, &block)
|
5
|
+
options = args.extract_options!.symbolize_keys
|
6
|
+
association = args.pop
|
7
|
+
options[:class] = [options[:class], 'add-nested-fields'].compact.join(' ')
|
8
|
+
options['data-association'] = association
|
9
|
+
args << (options.delete(:href) || 'javascript:void(0)')
|
10
|
+
args << options
|
11
|
+
@template.link_to(*args, &block)
|
12
|
+
end
|
13
|
+
|
14
|
+
def link_to_remove_nested_fields(*args, &block)
|
15
|
+
(hidden_field(:_destroy, :class => 'destroy') << link_to_action_nested_fields(:remove, *args, &block)).html_safe
|
16
|
+
end
|
17
|
+
|
18
|
+
def link_to_up_nested_fields(*args, &block)
|
19
|
+
link_to_action_nested_fields(:up, *args, &block)
|
20
|
+
end
|
21
|
+
|
22
|
+
def link_to_down_nested_fields(*args, &block)
|
23
|
+
link_to_action_nested_fields(:down, *args, &block)
|
24
|
+
end
|
25
|
+
|
26
|
+
def index_nested_fields(attribute_name)
|
27
|
+
hidden_field attribute_name, :class => 'index'
|
28
|
+
end
|
29
|
+
|
30
|
+
def nested_fields_for(association_name, record_object = nil, fields_options = {}, &block)
|
31
|
+
collection_wrapper = fields_options[:collection_wrapper] || (fields_options[:wrapper] && fields_options[:wrapper].to_sym == :table ? :tbody : nil) || :div
|
32
|
+
element_wrapper = fields_options[:element_wrapper] || (fields_options[:wrapper] && fields_options[:wrapper].to_sym == :table ? :tr : nil) || :div
|
33
|
+
|
34
|
+
fields_block = block || Proc.new { |fields| @template.render(:partial => "#{association_name.to_s.singularize}_fields", :locals => {:f => fields}) }
|
35
|
+
fields = @template.content_tag(collection_wrapper, fields_for(association_name, record_object, fields_options, &fields_block), :id => association_name, :class => 'nested-fields')
|
36
|
+
|
37
|
+
model_object = object.class.reflect_on_association(association_name).klass.new
|
38
|
+
template = fields_for(association_name, model_object, :element_wrapper => element_wrapper, :child_index => "new_#{association_name}", &fields_block)
|
39
|
+
template_options = {:id => "#{association_name}_fields_template", :type => 'text/x-jquery-tmpl'}
|
40
|
+
fields_template = @template.content_tag(:script, template, template_options)
|
41
|
+
|
42
|
+
fields + fields_template
|
43
|
+
end
|
44
|
+
|
45
|
+
alias default_fields_for_nested_model fields_for_nested_model
|
46
|
+
def fields_for_nested_model(name, object, options, block)
|
47
|
+
tag = options[:element_wrapper] || (options[:wrapper] && options[:wrapper].to_sym == :table ? :tr : nil) || :div
|
48
|
+
html_attributes = {
|
49
|
+
:class => 'fields',
|
50
|
+
'data-object-id' => options[:child_index] || object.object_id,
|
51
|
+
:style => object._destroy ? 'display: none;' : ''
|
52
|
+
}
|
53
|
+
@template.content_tag(tag, default_fields_for_nested_model(name, object, options, block), html_attributes)
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def link_to_action_nested_fields(action, *args, &block)
|
59
|
+
options = args.extract_options!.symbolize_keys
|
60
|
+
options[:class] = [options[:class], "#{action}-nested-fields"].compact.join(" ")
|
61
|
+
args << (options.delete(:href) || 'javascript:void(0)')
|
62
|
+
args << options
|
63
|
+
@template.link_to(*args, &block)
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
67
|
end
|
data/nested_fields_rails.gemspec
CHANGED
@@ -5,8 +5,8 @@ Gem::Specification.new do |s|
|
|
5
5
|
s.name = 'nested_fields_rails'
|
6
6
|
s.version = NestedFieldsRails::VERSION
|
7
7
|
|
8
|
-
s.authors = ["
|
9
|
-
s.email = ["
|
8
|
+
s.authors = ["Gabriel Naiman"]
|
9
|
+
s.email = ["gabynaiman@gmail.com"]
|
10
10
|
s.homepage = 'https://github.com/gabynaiman/nested_fields_rails'
|
11
11
|
s.summary = 'Manage multiple models in the same form'
|
12
12
|
s.description = 'Manage multiple models in the same form'
|
@@ -15,4 +15,6 @@ Gem::Specification.new do |s|
|
|
15
15
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
16
16
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
17
17
|
s.require_paths = ["lib"]
|
18
|
+
|
19
|
+
s.add_dependency 'jquery-rails'
|
18
20
|
end
|
@@ -1,97 +1,97 @@
|
|
1
|
-
jQuery(function($) {
|
2
|
-
|
3
|
-
NestedFields = function(associationName) {
|
4
|
-
this.associationName = associationName;
|
5
|
-
|
6
|
-
this.add = function() {
|
7
|
-
var template = $('#' + this.associationName + '_fields_template').html();
|
8
|
-
var container = $('#' + this.associationName + '.nested-fields');
|
9
|
-
var regexp = new RegExp("new_" + this.associationName, "g");
|
10
|
-
var newId = new Date().getTime();
|
11
|
-
container.append(template.replace(regexp, newId))
|
12
|
-
this.updateIndex();
|
13
|
-
};
|
14
|
-
|
15
|
-
this.remove = function(objectId) {
|
16
|
-
var fields = this.findFields(objectId);
|
17
|
-
var destroyField = fields.find('.destroy');
|
18
|
-
destroyField.val('true');
|
19
|
-
fields.hide();
|
20
|
-
this.updateIndex();
|
21
|
-
};
|
22
|
-
|
23
|
-
this.up = function(objectId) {
|
24
|
-
var fields = this.findFields(objectId);
|
25
|
-
var container = fields.closest('.nested-fields');
|
26
|
-
var index = fields.index() - 1;
|
27
|
-
|
28
|
-
if (index >= 0) {
|
29
|
-
this.findFields(objectId).remove();
|
30
|
-
container.children('.fields').eq(index).before(fields);
|
31
|
-
this.updateIndex();
|
32
|
-
}
|
33
|
-
};
|
34
|
-
|
35
|
-
this.down = function(objectId) {
|
36
|
-
var fields = this.findFields(objectId);
|
37
|
-
var container = fields.closest('.nested-fields');
|
38
|
-
var index = fields.index();
|
39
|
-
|
40
|
-
if (fields.index() < container.children('.fields').size()) {
|
41
|
-
this.findFields(objectId).remove();
|
42
|
-
container.children('.fields').eq(index).after(fields);
|
43
|
-
this.updateIndex();
|
44
|
-
}
|
45
|
-
};
|
46
|
-
|
47
|
-
this.findFields = function(objectId) {
|
48
|
-
return $('#' + this.associationName + '.nested-fields .fields[data-object-id=' + objectId + ']');
|
49
|
-
};
|
50
|
-
|
51
|
-
this.updateIndex = function() {
|
52
|
-
var container = $('#' + this.associationName + '.nested-fields');
|
53
|
-
container.find('.index').each(function(index, element) {
|
54
|
-
$(element).val(index);
|
55
|
-
});
|
56
|
-
}
|
57
|
-
|
58
|
-
this.ids = function() {
|
59
|
-
var ids = [];
|
60
|
-
$('#' + this.associationName + '.nested-fields .fields').each(function(index, element) {
|
61
|
-
ids.push($(element).attr('data-object-id'));
|
62
|
-
});
|
63
|
-
return ids;
|
64
|
-
};
|
65
|
-
};
|
66
|
-
|
67
|
-
function nestedFieldContext(element) {
|
68
|
-
var link = $(element);
|
69
|
-
var fields = link.closest('.fields');
|
70
|
-
var objectId = fields.attr('data-object-id');
|
71
|
-
var container = fields.closest('.nested-fields');
|
72
|
-
var associationName = container.attr('id');
|
73
|
-
return {'associationName': associationName, 'objectId': objectId};
|
74
|
-
};
|
75
|
-
|
76
|
-
$('.add-nested-fields').
|
77
|
-
var link = $(this);
|
78
|
-
var associationName = link.attr('data-association');
|
79
|
-
new NestedFields(associationName).add();
|
80
|
-
});
|
81
|
-
|
82
|
-
$('.remove-nested-fields').
|
83
|
-
var context = nestedFieldContext(this);
|
84
|
-
new NestedFields(context.associationName).remove(context.objectId);
|
85
|
-
});
|
86
|
-
|
87
|
-
$('.up-nested-fields').
|
88
|
-
var context = nestedFieldContext(this);
|
89
|
-
new NestedFields(context.associationName).up(context.objectId);
|
90
|
-
});
|
91
|
-
|
92
|
-
$('.down-nested-fields').
|
93
|
-
var context = nestedFieldContext(this);
|
94
|
-
new NestedFields(context.associationName).down(context.objectId);
|
95
|
-
});
|
96
|
-
|
1
|
+
jQuery(function($) {
|
2
|
+
|
3
|
+
NestedFields = function(associationName) {
|
4
|
+
this.associationName = associationName;
|
5
|
+
|
6
|
+
this.add = function() {
|
7
|
+
var template = $('#' + this.associationName + '_fields_template').html();
|
8
|
+
var container = $('#' + this.associationName + '.nested-fields');
|
9
|
+
var regexp = new RegExp("new_" + this.associationName, "g");
|
10
|
+
var newId = new Date().getTime();
|
11
|
+
container.append(template.replace(regexp, newId))
|
12
|
+
this.updateIndex();
|
13
|
+
};
|
14
|
+
|
15
|
+
this.remove = function(objectId) {
|
16
|
+
var fields = this.findFields(objectId);
|
17
|
+
var destroyField = fields.find('.destroy');
|
18
|
+
destroyField.val('true');
|
19
|
+
fields.hide();
|
20
|
+
this.updateIndex();
|
21
|
+
};
|
22
|
+
|
23
|
+
this.up = function(objectId) {
|
24
|
+
var fields = this.findFields(objectId);
|
25
|
+
var container = fields.closest('.nested-fields');
|
26
|
+
var index = fields.index() - 1;
|
27
|
+
|
28
|
+
if (index >= 0) {
|
29
|
+
this.findFields(objectId).remove();
|
30
|
+
container.children('.fields').eq(index).before(fields);
|
31
|
+
this.updateIndex();
|
32
|
+
}
|
33
|
+
};
|
34
|
+
|
35
|
+
this.down = function(objectId) {
|
36
|
+
var fields = this.findFields(objectId);
|
37
|
+
var container = fields.closest('.nested-fields');
|
38
|
+
var index = fields.index();
|
39
|
+
|
40
|
+
if (fields.index() < container.children('.fields').size()) {
|
41
|
+
this.findFields(objectId).remove();
|
42
|
+
container.children('.fields').eq(index).after(fields);
|
43
|
+
this.updateIndex();
|
44
|
+
}
|
45
|
+
};
|
46
|
+
|
47
|
+
this.findFields = function(objectId) {
|
48
|
+
return $('#' + this.associationName + '.nested-fields .fields[data-object-id=' + objectId + ']');
|
49
|
+
};
|
50
|
+
|
51
|
+
this.updateIndex = function() {
|
52
|
+
var container = $('#' + this.associationName + '.nested-fields');
|
53
|
+
container.find('.index').each(function(index, element) {
|
54
|
+
$(element).val(index);
|
55
|
+
});
|
56
|
+
}
|
57
|
+
|
58
|
+
this.ids = function() {
|
59
|
+
var ids = [];
|
60
|
+
$('#' + this.associationName + '.nested-fields .fields').each(function(index, element) {
|
61
|
+
ids.push($(element).attr('data-object-id'));
|
62
|
+
});
|
63
|
+
return ids;
|
64
|
+
};
|
65
|
+
};
|
66
|
+
|
67
|
+
function nestedFieldContext(element) {
|
68
|
+
var link = $(element);
|
69
|
+
var fields = link.closest('.fields');
|
70
|
+
var objectId = fields.attr('data-object-id');
|
71
|
+
var container = fields.closest('.nested-fields');
|
72
|
+
var associationName = container.attr('id');
|
73
|
+
return {'associationName': associationName, 'objectId': objectId};
|
74
|
+
};
|
75
|
+
|
76
|
+
$('.add-nested-fields').on('click', function() {
|
77
|
+
var link = $(this);
|
78
|
+
var associationName = link.attr('data-association');
|
79
|
+
new NestedFields(associationName).add();
|
80
|
+
});
|
81
|
+
|
82
|
+
$('.remove-nested-fields').on('click', function() {
|
83
|
+
var context = nestedFieldContext(this);
|
84
|
+
new NestedFields(context.associationName).remove(context.objectId);
|
85
|
+
});
|
86
|
+
|
87
|
+
$('.up-nested-fields').on('click', function() {
|
88
|
+
var context = nestedFieldContext(this);
|
89
|
+
new NestedFields(context.associationName).up(context.objectId);
|
90
|
+
});
|
91
|
+
|
92
|
+
$('.down-nested-fields').on('click', function() {
|
93
|
+
var context = nestedFieldContext(this);
|
94
|
+
new NestedFields(context.associationName).down(context.objectId);
|
95
|
+
});
|
96
|
+
|
97
97
|
});
|
metadata
CHANGED
@@ -1,19 +1,35 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nested_fields_rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
|
-
-
|
8
|
+
- Gabriel Naiman
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
13
|
-
dependencies:
|
12
|
+
date: 2013-04-16 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: jquery-rails
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
14
30
|
description: Manage multiple models in the same form
|
15
31
|
email:
|
16
|
-
-
|
32
|
+
- gabynaiman@gmail.com
|
17
33
|
executables: []
|
18
34
|
extensions: []
|
19
35
|
extra_rdoc_files: []
|
@@ -49,8 +65,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
49
65
|
version: '0'
|
50
66
|
requirements: []
|
51
67
|
rubyforge_project:
|
52
|
-
rubygems_version: 1.8.
|
68
|
+
rubygems_version: 1.8.24
|
53
69
|
signing_key:
|
54
70
|
specification_version: 3
|
55
71
|
summary: Manage multiple models in the same form
|
56
72
|
test_files: []
|
73
|
+
has_rdoc:
|