nested_form 0.1.1 → 0.2.0
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/CHANGELOG.rdoc +12 -0
- data/README.rdoc +30 -2
- data/Rakefile +23 -4
- data/lib/generators/nested_form/install_generator.rb +1 -1
- data/lib/nested_form/{builder.rb → builder_mixin.rb} +4 -4
- data/lib/nested_form/builders.rb +23 -0
- data/lib/nested_form/engine.rb +14 -0
- data/lib/nested_form/view_helper.rb +25 -9
- data/lib/nested_form.rb +1 -2
- data/spec/dummy/Rakefile +7 -0
- data/spec/dummy/app/assets/javascripts/application.js +5 -0
- data/spec/dummy/app/assets/javascripts/jquery.js +8981 -0
- data/spec/dummy/app/assets/javascripts/jquery_nested_form.js +71 -0
- data/spec/dummy/app/assets/javascripts/projects.js +2 -0
- data/spec/dummy/app/assets/javascripts/prototype.js +6082 -0
- data/{lib/generators/nested_form/templates → spec/dummy/app/assets/javascripts}/prototype_nested_form.js +0 -0
- data/spec/dummy/app/assets/stylesheets/application.css +7 -0
- data/spec/dummy/app/assets/stylesheets/projects.css +4 -0
- data/spec/dummy/app/controllers/application_controller.rb +3 -0
- data/spec/dummy/app/controllers/projects_controller.rb +5 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/helpers/projects_helper.rb +2 -0
- data/spec/dummy/app/models/milestone.rb +3 -0
- data/spec/dummy/app/models/project.rb +4 -0
- data/spec/dummy/app/models/task.rb +3 -0
- data/spec/dummy/app/views/layouts/application.html.erb +11 -0
- data/spec/dummy/app/views/projects/new.html.erb +14 -0
- data/spec/dummy/config/application.rb +42 -0
- data/spec/dummy/config/boot.rb +10 -0
- data/spec/dummy/config/database.yml +25 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +27 -0
- data/spec/dummy/config/environments/production.rb +54 -0
- data/spec/dummy/config/environments/test.rb +39 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/inflections.rb +10 -0
- data/spec/dummy/config/initializers/mime_types.rb +5 -0
- data/spec/dummy/config/initializers/secret_token.rb +7 -0
- data/spec/dummy/config/initializers/session_store.rb +8 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +12 -0
- data/spec/dummy/config/locales/en.yml +5 -0
- data/spec/dummy/config/routes.rb +59 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/db/development.sqlite3 +0 -0
- data/spec/dummy/db/migrate/20110710143903_initial_tables.rb +23 -0
- data/spec/dummy/db/schema.rb +29 -0
- data/spec/dummy/db/test.sqlite3 +0 -0
- data/spec/dummy/public/404.html +26 -0
- data/spec/dummy/public/422.html +26 -0
- data/spec/dummy/public/500.html +26 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/script/rails +6 -0
- data/spec/dummy/test/functional/projects_controller_test.rb +7 -0
- data/spec/dummy/test/unit/helpers/projects_helper_test.rb +4 -0
- data/spec/form_spec.rb +31 -0
- data/spec/nested_form/builder_spec.rb +33 -31
- data/spec/nested_form/view_helper_spec.rb +26 -12
- data/spec/spec_helper.rb +9 -48
- metadata +84 -59
- data/Gemfile +0 -3
- data/lib/generators/nested_form/templates/jquery_nested_form.js +0 -51
data/CHANGELOG.rdoc
CHANGED
@@ -1,9 +1,21 @@
|
|
1
|
+
0.2.0 (February 7, 2012)
|
2
|
+
|
3
|
+
* Integration tests (thanks fxposter) - issue #58
|
4
|
+
|
5
|
+
* Improved simple_form and 3.1 support (thanks fxposter)
|
6
|
+
|
7
|
+
* nested:fieldAdded event includes the newly added field (thanks harrigan)
|
8
|
+
|
9
|
+
* other minor bug fixes
|
10
|
+
|
11
|
+
|
1
12
|
0.1.1 (April 23, 2011)
|
2
13
|
|
3
14
|
* Support HTML options and block in add/remove link - issue #31
|
4
15
|
|
5
16
|
* Added new RegEx to generate new objects IDs correctly - issue #26 and issue #30
|
6
17
|
|
18
|
+
|
7
19
|
0.1.0 (March 26, 2011)
|
8
20
|
|
9
21
|
* Prefix new records with "new_" so there's no possible conflict with existing records - issue #21
|
data/README.rdoc
CHANGED
@@ -7,6 +7,29 @@ This gem only works with Rails 3. See the {rails2 branch}[https://github.com/rya
|
|
7
7
|
An example project showing how this works is available in the {complex-nested-forms/nested_form branch}[https://github.com/ryanb/complex-form-examples/tree/nested_form].
|
8
8
|
|
9
9
|
|
10
|
+
== Changes in this repo
|
11
|
+
|
12
|
+
=== Enhanced jQuery javascript template
|
13
|
+
|
14
|
+
Now you can override default behavior of inserting new subforms into your form (jQuery-only feature, sorry). For example:
|
15
|
+
|
16
|
+
window.nestedFormEvents.insertFields = function(content, assoc, link) {
|
17
|
+
return $(link).closest('form').find(assoc + '_fields').append($(content));
|
18
|
+
}
|
19
|
+
|
20
|
+
=== Rails 3.1 support (with asset pipeline)
|
21
|
+
|
22
|
+
There's a small fix for Rails 3.1 form handling.
|
23
|
+
Also support of Rails 3.1 led me to huge refactor of specs setup (now using Helper specs from rspec-rails instead of custom implementation spec context).
|
24
|
+
|
25
|
+
Formtastic is supported only with Rails 3.0, Rails 3.1 integration is not available now.
|
26
|
+
|
27
|
+
Asset pipeline is supported. So you can use
|
28
|
+
|
29
|
+
//= require jquery_nested_form
|
30
|
+
// or
|
31
|
+
//= require prototype_nested_form
|
32
|
+
|
10
33
|
== Setup
|
11
34
|
|
12
35
|
Add it to your Gemfile then run +bundle+ to install it.
|
@@ -41,6 +64,11 @@ You will then be able to use +link_to_add+ and +link_to_remove+ helper methods o
|
|
41
64
|
<p><%= f.link_to_add "Add a task", :tasks %></p>
|
42
65
|
|
43
66
|
|
67
|
+
== SimpleForm and Formtastic Support
|
68
|
+
|
69
|
+
Use <tt>simple_nested_form_for</tt> or <tt>semantic_nested_form_for</tt> for SimpleForm and Formtastic support respectively. This is feature is not yet in a Gem release but is in the Git repo.
|
70
|
+
|
71
|
+
|
44
72
|
== Partials
|
45
73
|
|
46
74
|
It is often desirable to move the nested fields into a partial to keep things organized. If you don't supply a block to fields_for it will look for a partial and use that.
|
@@ -57,10 +85,10 @@ If you are using jQuery, <tt>nested:fieldAdded</tt> and <tt>nested:fieldRemoved<
|
|
57
85
|
|
58
86
|
== Special Thanks
|
59
87
|
|
60
|
-
First of all, thanks ro Ryan Bates for this gem. Awesome work.
|
61
|
-
|
62
88
|
This gem was originally based on the solution by Tim Riley in his {complex-form-examples fork}[https://github.com/timriley/complex-form-examples/tree/unobtrusive-jquery-deep-fix2].
|
63
89
|
|
64
90
|
Thank you Andrew Manshin for the Rails 3 transition, {Andrea Singh}[https://github.com/madebydna] for converting to a gem and {Peter Giacomo Lombardo}[https://github.com/pglombardo] for Prototype support.
|
65
91
|
|
66
92
|
Andrea also wrote a great {blog post}[http://blog.madebydna.com/all/code/2010/10/07/dynamic-nested-froms-with-the-nested-form-gem.html] on the internal workings of this gem.
|
93
|
+
|
94
|
+
Thanks {Pavel Forkert}[https://github.com/fxposter] for the SimpleForm and Formtastic support.
|
data/Rakefile
CHANGED
@@ -1,10 +1,29 @@
|
|
1
1
|
require 'rubygems'
|
2
|
+
# require 'appraisal'
|
2
3
|
require 'rake'
|
3
|
-
require 'rspec/core/rake_task'
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
begin
|
6
|
+
require 'rspec/core/rake_task'
|
7
|
+
desc "Run RSpec"
|
8
|
+
RSpec::Core::RakeTask.new do |t|
|
9
|
+
t.verbose = false
|
10
|
+
end
|
11
|
+
rescue LoadError
|
12
|
+
puts "You should run rake spec:install in order to install all corresponding gems!"
|
8
13
|
end
|
9
14
|
|
10
15
|
task :default => :spec
|
16
|
+
namespace :spec do
|
17
|
+
task :install do
|
18
|
+
puts `bundle install --gemfile=gemfiles/Gemfile.rails3_0`
|
19
|
+
puts `bundle install --gemfile=gemfiles/Gemfile.rails3_1`
|
20
|
+
end
|
21
|
+
|
22
|
+
task :all do
|
23
|
+
ENV['BUNDLE_GEMFILE'] = File.expand_path('../gemfiles/Gemfile.rails3_0', __FILE__)
|
24
|
+
Rake::Task["spec"].execute
|
25
|
+
|
26
|
+
ENV['BUNDLE_GEMFILE'] = File.expand_path('../gemfiles/Gemfile.rails3_1', __FILE__)
|
27
|
+
Rake::Task["spec"].execute
|
28
|
+
end
|
29
|
+
end
|
@@ -1,5 +1,5 @@
|
|
1
1
|
module NestedForm
|
2
|
-
|
2
|
+
module BuilderMixin
|
3
3
|
# Adds a link to insert a new associated records. The first argument is the name of the link, the second is the name of the association.
|
4
4
|
#
|
5
5
|
# f.link_to_add("Add Task", :tasks)
|
@@ -48,12 +48,12 @@ module NestedForm
|
|
48
48
|
hidden_field(:_destroy) + @template.link_to(*args, &block)
|
49
49
|
end
|
50
50
|
|
51
|
-
def fields_for_with_nested_attributes(association_name, args
|
51
|
+
def fields_for_with_nested_attributes(association_name, *args)
|
52
52
|
# TODO Test this better
|
53
|
-
block
|
53
|
+
block = args.pop || Proc.new { |fields| @template.render(:partial => "#{association_name.to_s.singularize}_fields", :locals => {:f => fields}) }
|
54
54
|
@fields ||= {}
|
55
55
|
@fields[association_name] = block
|
56
|
-
super(association_name, args
|
56
|
+
super(association_name, *(args << block))
|
57
57
|
end
|
58
58
|
|
59
59
|
def fields_for_nested_model(name, object, options, block)
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'nested_form/builder_mixin'
|
2
|
+
|
3
|
+
module NestedForm
|
4
|
+
class Builder < ::ActionView::Helpers::FormBuilder
|
5
|
+
include ::NestedForm::BuilderMixin
|
6
|
+
end
|
7
|
+
|
8
|
+
begin
|
9
|
+
require 'simple_form'
|
10
|
+
class SimpleBuilder < ::SimpleForm::FormBuilder
|
11
|
+
include ::NestedForm::BuilderMixin
|
12
|
+
end
|
13
|
+
rescue LoadError
|
14
|
+
end
|
15
|
+
|
16
|
+
begin
|
17
|
+
require 'formtastic'
|
18
|
+
class FormtasticBuilder < (defined?(::Formtastic::FormBuilder) ? Formtastic::FormBuilder : ::Formtastic::SemanticFormBuilder)
|
19
|
+
include ::NestedForm::BuilderMixin
|
20
|
+
end
|
21
|
+
rescue LoadError
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'rails'
|
2
|
+
|
3
|
+
module NestedForm
|
4
|
+
class Engine < ::Rails::Engine
|
5
|
+
initializer 'nested_form' do |app|
|
6
|
+
ActiveSupport.on_load(:action_view) do
|
7
|
+
require "nested_form/view_helper"
|
8
|
+
class ActionView::Base
|
9
|
+
include NestedForm::ViewHelper
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -1,13 +1,24 @@
|
|
1
|
+
require 'nested_form/builders'
|
2
|
+
|
1
3
|
module NestedForm
|
2
4
|
module ViewHelper
|
3
5
|
def nested_form_for(*args, &block)
|
4
6
|
options = args.extract_options!.reverse_merge(:builder => NestedForm::Builder)
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
7
|
+
form_for(*(args << options), &block) << after_nested_form_callbacks
|
8
|
+
end
|
9
|
+
|
10
|
+
if defined?(NestedForm::SimpleBuilder)
|
11
|
+
def simple_nested_form_for(*args, &block)
|
12
|
+
options = args.extract_options!.reverse_merge(:builder => NestedForm::SimpleBuilder)
|
13
|
+
simple_form_for(*(args << options), &block) << after_nested_form_callbacks
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
if defined?(NestedForm::FormtasticBuilder)
|
18
|
+
def semantic_nested_form_for(*args, &block)
|
19
|
+
options = args.extract_options!.reverse_merge(:builder => NestedForm::FormtasticBuilder)
|
20
|
+
semantic_form_for(*(args << options), &block) << after_nested_form_callbacks
|
9
21
|
end
|
10
|
-
output << fields.join(" ").html_safe
|
11
22
|
end
|
12
23
|
|
13
24
|
def after_nested_form(association, &block)
|
@@ -18,9 +29,14 @@ module NestedForm
|
|
18
29
|
@after_nested_form_callbacks << block
|
19
30
|
end
|
20
31
|
end
|
21
|
-
end
|
22
|
-
end
|
23
32
|
|
24
|
-
|
25
|
-
|
33
|
+
private
|
34
|
+
def after_nested_form_callbacks
|
35
|
+
@after_nested_form_callbacks ||= []
|
36
|
+
fields = @after_nested_form_callbacks.map do |callback|
|
37
|
+
callback.call
|
38
|
+
end
|
39
|
+
fields.join(" ").html_safe
|
40
|
+
end
|
41
|
+
end
|
26
42
|
end
|
data/lib/nested_form.rb
CHANGED
@@ -1,2 +1 @@
|
|
1
|
-
require "nested_form/
|
2
|
-
require "nested_form/view_helper"
|
1
|
+
require "nested_form/engine"
|
data/spec/dummy/Rakefile
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
# Add your own tasks in files placed in lib/tasks ending in .rake,
|
3
|
+
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
|
4
|
+
|
5
|
+
require File.expand_path('../config/application', __FILE__)
|
6
|
+
|
7
|
+
Dummy::Application.load_tasks
|
@@ -0,0 +1,5 @@
|
|
1
|
+
// This is a manifest file that'll be compiled into including all the files listed below.
|
2
|
+
// Add new JavaScript/Coffee code in separate files in this directory and they'll automatically
|
3
|
+
// be included in the compiled file accessible from http://example.com/assets/application.js
|
4
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
5
|
+
// the compiled file.
|