ajax_nested_form 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +67 -0
- data/Rakefile +2 -0
- data/ajax_nested_form.gemspec +24 -0
- data/lib/ajax_nested_form/engine.rb +10 -0
- data/lib/ajax_nested_form/helpers/view_helper.rb +19 -0
- data/lib/ajax_nested_form/version.rb +3 -0
- data/lib/ajax_nested_form.rb +11 -0
- data/vendor/assets/javascripts/ajax_nested_form.js.coffee +15 -0
- metadata +160 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 sergio1990
|
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,67 @@
|
|
1
|
+
AjaxNestedForm
|
2
|
+
==============
|
3
|
+
|
4
|
+
This gem provides adding and removing nested model fields dynamically through JavaScript using jQuery. It may using for Rails 3.1+ with assets pipeline
|
5
|
+
|
6
|
+
Installation
|
7
|
+
------------
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'ajax_nested_form'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install ajax_nested_form
|
20
|
+
|
21
|
+
Usage
|
22
|
+
-----
|
23
|
+
|
24
|
+
### Javascript
|
25
|
+
|
26
|
+
Add necessary javascript file to app/assets/javascripts/application.js
|
27
|
+
|
28
|
+
``` javascript
|
29
|
+
//= require ajax_nested_form
|
30
|
+
```
|
31
|
+
|
32
|
+
### Using ActionView helper methods
|
33
|
+
|
34
|
+
For creating link to add nested model fields use next syntax
|
35
|
+
|
36
|
+
``` ruby
|
37
|
+
link_to_add_fields "LinkText", form_builder_instance, :association_name, "container_for_adding_fields", "position_for_adding"
|
38
|
+
```
|
39
|
+
|
40
|
+
Available positions for adding are "first" and "last". Last is used by default.
|
41
|
+
|
42
|
+
Sample
|
43
|
+
|
44
|
+
``` ruby
|
45
|
+
link_to_add_fields "Add list", f, :lists, "#lists-container", "first"
|
46
|
+
```
|
47
|
+
|
48
|
+
For creating link to destroy nested model fields use following code
|
49
|
+
|
50
|
+
``` ruby
|
51
|
+
link_to_remove_fields "Remove", form_builder_instance, "nested_model_fields_container"
|
52
|
+
```
|
53
|
+
|
54
|
+
Sample
|
55
|
+
|
56
|
+
``` ruby
|
57
|
+
link_to_remove_fields "Remove", f, ".list-fields"
|
58
|
+
```
|
59
|
+
|
60
|
+
Contributing
|
61
|
+
------------
|
62
|
+
|
63
|
+
1. Fork it
|
64
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
65
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
66
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
67
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/ajax_nested_form/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["sergio1990"]
|
6
|
+
gem.email = ["sergeg1990@gmail.com"]
|
7
|
+
gem.description = %q{Add and remove nested model fields dynamically through JavaScript using jQuery for Rails 3.1+ with assets pipeline}
|
8
|
+
gem.summary = %q{Add and remove nested model fields dynamically through JavaScript using jQuery for Rails 3.1+ with assets pipeline}
|
9
|
+
gem.homepage = "https://github.com/sergio1990/ajax_nested_form"
|
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 = "ajax_nested_form"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = AjaxNestedForm::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency "railties", ">= 3.0"
|
19
|
+
gem.add_dependency "sass", ">= 3.2"
|
20
|
+
gem.add_dependency "coffee-rails"
|
21
|
+
gem.add_dependency 'jquery-rails', ">= 2.1.2"
|
22
|
+
gem.add_development_dependency "bundler", ">= 1.0"
|
23
|
+
gem.add_development_dependency "rails", ">= 3.1"
|
24
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'rails'
|
2
|
+
module AjaxNestedForm
|
3
|
+
class Engine < ::Rails::Engine
|
4
|
+
initializer 'ajax_nested_form-helpers', :group => :all do |app|
|
5
|
+
ActiveSupport.on_load :action_view do
|
6
|
+
ActionView::Base.send :include, AjaxNestedForm::Helpers::ViewHelper
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module AjaxNestedForm
|
2
|
+
module Helpers
|
3
|
+
module ViewHelper
|
4
|
+
|
5
|
+
def link_to_remove_fields(name, f, container)
|
6
|
+
f.hidden_field(:_destroy) + link_to_function(name, "AjaxNestedForm.removeFields(this, \"#{container}\")".html_safe)
|
7
|
+
end
|
8
|
+
|
9
|
+
def link_to_add_fields(name, f, association, container, position = "last")
|
10
|
+
new_object = f.object.class.reflect_on_association(association).klass.new
|
11
|
+
fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
|
12
|
+
render(association.to_s.singularize + "_fields", :f => builder)
|
13
|
+
end
|
14
|
+
link_to_function(name, "AjaxNestedForm.addFields(this, \"#{association}\", \"#{escape_javascript(fields)}\", \"#{container}\", \"#{position}\")".html_safe)
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
window.AjaxNestedForm = {}
|
2
|
+
|
3
|
+
class @AjaxNestedForm
|
4
|
+
|
5
|
+
@removeFields: (link, container) ->
|
6
|
+
$(link).prev("input[type=hidden]").value = "1"
|
7
|
+
$(link).closest(container).hide()
|
8
|
+
return
|
9
|
+
|
10
|
+
@addFields: (link, association, content, container, position) ->
|
11
|
+
new_id = new Date().getTime()
|
12
|
+
regexp = new RegExp("new_" + association, "g")
|
13
|
+
content_new = content.replace(regexp, new_id)
|
14
|
+
if position is "first" then $(container).prepend(content_new) else $(container).append(content_new)
|
15
|
+
return
|
metadata
ADDED
@@ -0,0 +1,160 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ajax_nested_form
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- sergio1990
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-09-23 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: railties
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3.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: '3.0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: sass
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '3.2'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '3.2'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: coffee-rails
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: jquery-rails
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 2.1.2
|
70
|
+
type: :runtime
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 2.1.2
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: bundler
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '1.0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '1.0'
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: rails
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '3.1'
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '3.1'
|
110
|
+
description: Add and remove nested model fields dynamically through JavaScript using
|
111
|
+
jQuery for Rails 3.1+ with assets pipeline
|
112
|
+
email:
|
113
|
+
- sergeg1990@gmail.com
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- .gitignore
|
119
|
+
- Gemfile
|
120
|
+
- LICENSE
|
121
|
+
- README.md
|
122
|
+
- Rakefile
|
123
|
+
- ajax_nested_form.gemspec
|
124
|
+
- lib/ajax_nested_form.rb
|
125
|
+
- lib/ajax_nested_form/engine.rb
|
126
|
+
- lib/ajax_nested_form/helpers/view_helper.rb
|
127
|
+
- lib/ajax_nested_form/version.rb
|
128
|
+
- vendor/assets/javascripts/ajax_nested_form.js.coffee
|
129
|
+
homepage: https://github.com/sergio1990/ajax_nested_form
|
130
|
+
licenses: []
|
131
|
+
post_install_message:
|
132
|
+
rdoc_options: []
|
133
|
+
require_paths:
|
134
|
+
- lib
|
135
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
136
|
+
none: false
|
137
|
+
requirements:
|
138
|
+
- - ! '>='
|
139
|
+
- !ruby/object:Gem::Version
|
140
|
+
version: '0'
|
141
|
+
segments:
|
142
|
+
- 0
|
143
|
+
hash: -611399917
|
144
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
145
|
+
none: false
|
146
|
+
requirements:
|
147
|
+
- - ! '>='
|
148
|
+
- !ruby/object:Gem::Version
|
149
|
+
version: '0'
|
150
|
+
segments:
|
151
|
+
- 0
|
152
|
+
hash: -611399917
|
153
|
+
requirements: []
|
154
|
+
rubyforge_project:
|
155
|
+
rubygems_version: 1.8.24
|
156
|
+
signing_key:
|
157
|
+
specification_version: 3
|
158
|
+
summary: Add and remove nested model fields dynamically through JavaScript using jQuery
|
159
|
+
for Rails 3.1+ with assets pipeline
|
160
|
+
test_files: []
|