nested_form_fields 0.0.1
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.
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +66 -0
- data/Rakefile +2 -0
- data/lib/nested_form_fields/version.rb +3 -0
- data/lib/nested_form_fields.rb +109 -0
- data/nested_form_fields.gemspec +17 -0
- data/vendor/assets/javascripts/nested_form_fields.js.coffee +31 -0
- metadata +57 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2012 Nico Ritsche
|
|
2
|
+
|
|
3
|
+
MIT License
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
a copy of this software and associated documentation files (the
|
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+
the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be
|
|
14
|
+
included in all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# Nested Form Fields
|
|
2
|
+
|
|
3
|
+
This gem helps creating forms for models with nested has_many associations.
|
|
4
|
+
|
|
5
|
+
It uses jQuery to dynamically add and remove nested associations.
|
|
6
|
+
|
|
7
|
+
- Works for arbitrarily deeply nested associations (tested up to 4 levels).
|
|
8
|
+
- Works with form builders like simple_form.
|
|
9
|
+
- Requires the Rails asset pipeline
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
Add this line to your application's Gemfile:
|
|
16
|
+
|
|
17
|
+
gem 'nested_form_fields'
|
|
18
|
+
|
|
19
|
+
And then execute:
|
|
20
|
+
|
|
21
|
+
$ bundle
|
|
22
|
+
|
|
23
|
+
Or install it yourself as:
|
|
24
|
+
|
|
25
|
+
$ gem install nested_form_fields
|
|
26
|
+
|
|
27
|
+
In your application.js file add:
|
|
28
|
+
|
|
29
|
+
//= require nested_form_fields
|
|
30
|
+
|
|
31
|
+
## Usage
|
|
32
|
+
|
|
33
|
+
Assume you have a user model with nested videos:
|
|
34
|
+
|
|
35
|
+
class User < ActiveRecord::Base
|
|
36
|
+
attr_accessible :name, :videos_attributes
|
|
37
|
+
has_many :videos
|
|
38
|
+
accepts_nested_attributes_for :videos, allow_destroy: true
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
Use the *nested_fields_for* helper inside your user form to add the video fields:
|
|
42
|
+
|
|
43
|
+
= form_for @user do |f|
|
|
44
|
+
= f.nested_fields_for :videos do |ff|
|
|
45
|
+
= ff.text_field :video_title
|
|
46
|
+
..
|
|
47
|
+
|
|
48
|
+
Links to add and remove fields can be added using the *add_nested_fields_link* and *remove_nested_fields_link* helpers:
|
|
49
|
+
|
|
50
|
+
= form_for @user do |f|
|
|
51
|
+
= f.nested_fields_for :videos do |ff|
|
|
52
|
+
= ff.remove_nested_fields_link
|
|
53
|
+
= ff.text_field :video_title
|
|
54
|
+
..
|
|
55
|
+
= f.add_nested_fields_link :videos
|
|
56
|
+
|
|
57
|
+
Note that *remove_nested_fields_link* needs to be called within the *nested_fields_for* call and *add_nested_fields_link* outside of it via the parent builder.
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
## Contributing
|
|
61
|
+
|
|
62
|
+
1. Fork it
|
|
63
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
|
64
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
|
65
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
|
66
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
require "nested_form_fields/version"
|
|
2
|
+
|
|
3
|
+
module NestedFormFields
|
|
4
|
+
module Rails
|
|
5
|
+
class Engine < ::Rails::Engine
|
|
6
|
+
end
|
|
7
|
+
end
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
module ActionView::Helpers
|
|
11
|
+
|
|
12
|
+
class FormBuilder
|
|
13
|
+
|
|
14
|
+
def nested_fields_for(record_name, record_object = nil, fields_options = {}, &block)
|
|
15
|
+
fields_options, record_object = record_object, nil if record_object.is_a?(Hash) && record_object.extractable_options?
|
|
16
|
+
fields_options[:builder] ||= options[:builder]
|
|
17
|
+
fields_options[:parent_builder] = self
|
|
18
|
+
fields_options[:namespace] = fields_options[:parent_builder].options[:namespace]
|
|
19
|
+
|
|
20
|
+
return fields_for_with_nested_attributes(record_name, record_object, fields_options, block)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def add_nested_fields_link association, text = nil
|
|
25
|
+
@template.link_to text || "Add #{association.to_s.singularize.humanize}", '',
|
|
26
|
+
class: "add_nested_fields_link",
|
|
27
|
+
data: { association_path: association_path(association.to_s) }
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def remove_nested_fields_link text = nil
|
|
31
|
+
@template.link_to text || 'x', '',
|
|
32
|
+
class: "remove_nested_fields_link",
|
|
33
|
+
data: { delete_association_field_name: delete_association_field_name }
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
private
|
|
38
|
+
|
|
39
|
+
def fields_for_with_nested_attributes(association_name, association, options, block)
|
|
40
|
+
name = "#{object_name}[#{association_name}_attributes]"
|
|
41
|
+
association = convert_to_model(association)
|
|
42
|
+
|
|
43
|
+
if association.respond_to?(:persisted?)
|
|
44
|
+
association = [association] if @object.send(association_name).is_a?(Array)
|
|
45
|
+
elsif !association.respond_to?(:to_ary)
|
|
46
|
+
association = @object.send(association_name)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
if association.respond_to?(:to_ary)
|
|
50
|
+
explicit_child_index = options[:child_index]
|
|
51
|
+
output = ActiveSupport::SafeBuffer.new
|
|
52
|
+
association.each do |child|
|
|
53
|
+
output << nested_fields_wrapper(association_name) do
|
|
54
|
+
fields_for_nested_model("#{name}[#{explicit_child_index || nested_child_index(name)}]", child, options, block)
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
templates = nested_model_template(name, association_name, options, block)
|
|
59
|
+
output << templates
|
|
60
|
+
|
|
61
|
+
output
|
|
62
|
+
elsif association
|
|
63
|
+
fields_for_nested_model(name, association, options, block)
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def nested_model_template name, association_name, options, block
|
|
69
|
+
for_template = self.options[:for_template]
|
|
70
|
+
@template.content_tag( for_template ? :div : :script,
|
|
71
|
+
type: for_template ? nil : 'text/html',
|
|
72
|
+
id: template_id(association_name),
|
|
73
|
+
class: for_template ? 'form_template' : nil,
|
|
74
|
+
style: for_template ? 'display:none' : nil ) do
|
|
75
|
+
nested_fields_wrapper(association_name) do
|
|
76
|
+
fields_for_nested_model("#{name}[#{index_placeholder(association_name)}]",
|
|
77
|
+
association_name.to_s.classify.constantize.new,
|
|
78
|
+
options.merge(for_template: true), block)
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def template_id association_name
|
|
85
|
+
"#{association_path(association_name)}_template"
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def association_path association_name
|
|
89
|
+
"#{object_name.gsub('][','_').gsub(/_attributes/,'').sub('[','_').sub(']','')}_#{association_name}"
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def index_placeholder association_name
|
|
93
|
+
"__#{association_path(association_name)}_index__"
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def delete_association_field_name
|
|
97
|
+
"#{object_name}[_destroy]"
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def nested_fields_wrapper association_name
|
|
102
|
+
@template.content_tag :div, class: "nested_fields nested_#{association_path(association_name)}" do
|
|
103
|
+
yield
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
|
2
|
+
require File.expand_path('../lib/nested_form_fields/version', __FILE__)
|
|
3
|
+
|
|
4
|
+
Gem::Specification.new do |gem|
|
|
5
|
+
gem.authors = ["Nico Ritsche"]
|
|
6
|
+
gem.email = ["ncrdevmail@gmail.com"]
|
|
7
|
+
gem.description = %q{Allows to dynamically add and remove nested has_many association fields in a form.}
|
|
8
|
+
gem.summary = %q{Allows to dynamically add and remove nested has_many association fields in a form.}
|
|
9
|
+
gem.homepage = ""
|
|
10
|
+
|
|
11
|
+
gem.files = `git ls-files`.split($\)
|
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
|
14
|
+
gem.name = "nested_form_fields"
|
|
15
|
+
gem.require_paths = ["lib"]
|
|
16
|
+
gem.version = NestedFormFields::VERSION
|
|
17
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
jQuery ->
|
|
2
|
+
|
|
3
|
+
$('body').on 'click', '.add_nested_fields_link', ->
|
|
4
|
+
$link = $(this)
|
|
5
|
+
association_path = $link.data('association-path')
|
|
6
|
+
$template = $("##{association_path}_template")
|
|
7
|
+
|
|
8
|
+
template_html = $template.html()
|
|
9
|
+
|
|
10
|
+
# insert association indexes
|
|
11
|
+
index_placeholder = "__#{association_path}_index__"
|
|
12
|
+
template_html = template_html.replace(new RegExp(index_placeholder,"g"), $(".nested_#{association_path}").length)
|
|
13
|
+
|
|
14
|
+
# replace child template div tags with script tags to avoid form submission of templates
|
|
15
|
+
$parsed_template = $(template_html)
|
|
16
|
+
$child_templates = $parsed_template.children('.form_template')
|
|
17
|
+
$child_templates.each () ->
|
|
18
|
+
$child = $(this)
|
|
19
|
+
$child.replaceWith($("<script id='#{$child.attr('id')}' type='text/html' />").html($child.html()))
|
|
20
|
+
|
|
21
|
+
$link.before( $parsed_template )
|
|
22
|
+
false
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
$('body').on 'click', '.remove_nested_fields_link', ->
|
|
26
|
+
$link = $(this)
|
|
27
|
+
delete_association_field_name = $link.data('delete-association-field-name')
|
|
28
|
+
$nested_fields_container = $link.parents(".nested_fields").first()
|
|
29
|
+
$nested_fields_container.before "<input type='hidden' name='#{delete_association_field_name}' value='1' />"
|
|
30
|
+
$nested_fields_container.hide()
|
|
31
|
+
false
|
metadata
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: nested_form_fields
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
prerelease:
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- Nico Ritsche
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2012-05-20 00:00:00.000000000Z
|
|
13
|
+
dependencies: []
|
|
14
|
+
description: Allows to dynamically add and remove nested has_many association fields
|
|
15
|
+
in a form.
|
|
16
|
+
email:
|
|
17
|
+
- ncrdevmail@gmail.com
|
|
18
|
+
executables: []
|
|
19
|
+
extensions: []
|
|
20
|
+
extra_rdoc_files: []
|
|
21
|
+
files:
|
|
22
|
+
- .gitignore
|
|
23
|
+
- Gemfile
|
|
24
|
+
- LICENSE
|
|
25
|
+
- README.md
|
|
26
|
+
- Rakefile
|
|
27
|
+
- lib/nested_form_fields.rb
|
|
28
|
+
- lib/nested_form_fields/version.rb
|
|
29
|
+
- nested_form_fields.gemspec
|
|
30
|
+
- vendor/assets/javascripts/nested_form_fields.js.coffee
|
|
31
|
+
homepage: ''
|
|
32
|
+
licenses: []
|
|
33
|
+
post_install_message:
|
|
34
|
+
rdoc_options: []
|
|
35
|
+
require_paths:
|
|
36
|
+
- lib
|
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
38
|
+
none: false
|
|
39
|
+
requirements:
|
|
40
|
+
- - ! '>='
|
|
41
|
+
- !ruby/object:Gem::Version
|
|
42
|
+
version: '0'
|
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
44
|
+
none: false
|
|
45
|
+
requirements:
|
|
46
|
+
- - ! '>='
|
|
47
|
+
- !ruby/object:Gem::Version
|
|
48
|
+
version: '0'
|
|
49
|
+
requirements: []
|
|
50
|
+
rubyforge_project:
|
|
51
|
+
rubygems_version: 1.8.15
|
|
52
|
+
signing_key:
|
|
53
|
+
specification_version: 3
|
|
54
|
+
summary: Allows to dynamically add and remove nested has_many association fields in
|
|
55
|
+
a form.
|
|
56
|
+
test_files: []
|
|
57
|
+
has_rdoc:
|