jquery_dynamic_fields 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 +65 -0
- data/Rakefile +2 -0
- data/app/assets/javascripts/jquery_dynamic_fields.js.coffee +11 -0
- data/app/helpers/jquery_dynamic_fields_helper.rb +15 -0
- data/jquery_dynamic_fields.gemspec +17 -0
- data/lib/jquery_dynamic_fields.rb +2 -0
- data/lib/jquery_dynamic_fields/engine.rb +4 -0
- data/lib/jquery_dynamic_fields/version.rb +3 -0
- metadata +56 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Aaron Cruz
|
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,65 @@
|
|
1
|
+
# JqueryDynamicFields
|
2
|
+
|
3
|
+
Most of code pulled from [Railscasts 196 (revised)](http://railscasts.com/episodes/196-nested-model-form-revised)
|
4
|
+
|
5
|
+
__WIP__: very alpha bro
|
6
|
+
|
7
|
+
## Compatibility
|
8
|
+
|
9
|
+
* >= Rails 3.1
|
10
|
+
* >= Ruby 1.9.2
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
Add this line to your application's Gemfile:
|
15
|
+
|
16
|
+
gem 'jquery_dynamic_fields'
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
In your pipeline
|
23
|
+
|
24
|
+
// in app/assets/javascripts/application.js
|
25
|
+
//= require jquery_dynamic_fields
|
26
|
+
|
27
|
+
## How to use
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
class TaskList < ActiveRecord::Base
|
31
|
+
has_many :tasks
|
32
|
+
accepts_nested_attributes_for :tasks
|
33
|
+
end
|
34
|
+
|
35
|
+
class Task < ActiveRecord::Base
|
36
|
+
belongs_to :task_list
|
37
|
+
end
|
38
|
+
|
39
|
+
```
|
40
|
+
|
41
|
+
```erb
|
42
|
+
<%# app/views/task_list %>
|
43
|
+
|
44
|
+
<%= form_for @task_list do |f| %>
|
45
|
+
<%= link_to_add_fields "Add a task", f, :tasks %>
|
46
|
+
<%= f.submit "Save!" %>
|
47
|
+
<% end %>
|
48
|
+
|
49
|
+
<%# the partial name must match (relation singular)_fields so in this case "task"
|
50
|
+
and be in the same directory as the template calling #link_to_add_fields %>
|
51
|
+
|
52
|
+
<%# app/views/task_list/_task_fields.html.erb %>
|
53
|
+
<fieldset>
|
54
|
+
<%= f.text_field :label %>
|
55
|
+
<%= link_to_delete_field f %>
|
56
|
+
</fieldset>
|
57
|
+
```
|
58
|
+
|
59
|
+
## Contributing
|
60
|
+
|
61
|
+
1. Fork it
|
62
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
63
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
64
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
65
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
jQuery ->
|
2
|
+
$('form').on 'click', '.remove_field', (event) ->
|
3
|
+
$(this).prev('input[type=hidden]').val('1')
|
4
|
+
$(this).closest('fieldset').hide()
|
5
|
+
event.preventDefault()
|
6
|
+
|
7
|
+
$('form').on 'click', '.add_fields', (event) ->
|
8
|
+
time = new Date().getTime()
|
9
|
+
regexp = new RegExp($(this).data('id'), 'g')
|
10
|
+
$(this).before($(this).data('fields').replace(regexp, time))
|
11
|
+
event.preventDefault()
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module JqueryDynamicFieldsHelper
|
2
|
+
def link_to_add_fields name, f, association
|
3
|
+
new_object = f.object.send(association).klass.new
|
4
|
+
id = new_object.object_id
|
5
|
+
fields = f.fields_for(association, new_object, child_index: id) do |builder|
|
6
|
+
render(association.to_s.singularize + "_fields", f: builder)
|
7
|
+
end
|
8
|
+
link_to(name, '#', class: "add_fields", data: {id: id, fields: fields.gsub("\n", "")})
|
9
|
+
end
|
10
|
+
|
11
|
+
def link_to_remove_field f
|
12
|
+
(f.hidden_field :_destroy) +
|
13
|
+
link_to("remove", "#", class: "remove_field")
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/jquery_dynamic_fields/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Aaron Cruz"]
|
6
|
+
gem.email = ["aaron@aaroncruz.com"]
|
7
|
+
gem.description = %q{Dynamically add and remove form fields in a Rails 3.1+ app}
|
8
|
+
gem.summary = %q{Dynamically add and remove form fields in a Rails 3.1+ app}
|
9
|
+
gem.homepage = "https://github.com/pferdefleisch/jquery_dynamic_fields"
|
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 = "jquery_dynamic_fields"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = JqueryDynamicFields::VERSION
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jquery_dynamic_fields
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Aaron Cruz
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-06-11 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Dynamically add and remove form fields in a Rails 3.1+ app
|
15
|
+
email:
|
16
|
+
- aaron@aaroncruz.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- app/assets/javascripts/jquery_dynamic_fields.js.coffee
|
27
|
+
- app/helpers/jquery_dynamic_fields_helper.rb
|
28
|
+
- jquery_dynamic_fields.gemspec
|
29
|
+
- lib/jquery_dynamic_fields.rb
|
30
|
+
- lib/jquery_dynamic_fields/engine.rb
|
31
|
+
- lib/jquery_dynamic_fields/version.rb
|
32
|
+
homepage: https://github.com/pferdefleisch/jquery_dynamic_fields
|
33
|
+
licenses: []
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options: []
|
36
|
+
require_paths:
|
37
|
+
- lib
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ! '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
requirements: []
|
51
|
+
rubyforge_project:
|
52
|
+
rubygems_version: 1.8.23
|
53
|
+
signing_key:
|
54
|
+
specification_version: 3
|
55
|
+
summary: Dynamically add and remove form fields in a Rails 3.1+ app
|
56
|
+
test_files: []
|