flexible-nested-form 0.0.3

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.
@@ -0,0 +1,2 @@
1
+ ---
2
+ BUNDLE_DISABLE_SHARED_GEMS: "1"
@@ -0,0 +1,2 @@
1
+ pkg/*
2
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source :gemcutter
2
+
3
+ # Specify your gem's dependencies in nested_form.gemspec
4
+ gemspec
@@ -0,0 +1,92 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ nested_form (0.0.0)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ abstract (1.0.0)
10
+ actionmailer (3.0.1)
11
+ actionpack (= 3.0.1)
12
+ mail (~> 2.2.5)
13
+ actionpack (3.0.1)
14
+ activemodel (= 3.0.1)
15
+ activesupport (= 3.0.1)
16
+ builder (~> 2.1.2)
17
+ erubis (~> 2.6.6)
18
+ i18n (~> 0.4.1)
19
+ rack (~> 1.2.1)
20
+ rack-mount (~> 0.6.12)
21
+ rack-test (~> 0.5.4)
22
+ tzinfo (~> 0.3.23)
23
+ activemodel (3.0.1)
24
+ activesupport (= 3.0.1)
25
+ builder (~> 2.1.2)
26
+ i18n (~> 0.4.1)
27
+ activerecord (3.0.1)
28
+ activemodel (= 3.0.1)
29
+ activesupport (= 3.0.1)
30
+ arel (~> 1.0.0)
31
+ tzinfo (~> 0.3.23)
32
+ activeresource (3.0.1)
33
+ activemodel (= 3.0.1)
34
+ activesupport (= 3.0.1)
35
+ activesupport (3.0.1)
36
+ arel (1.0.1)
37
+ activesupport (~> 3.0.0)
38
+ builder (2.1.2)
39
+ diff-lcs (1.1.2)
40
+ erubis (2.6.6)
41
+ abstract (>= 1.0.0)
42
+ i18n (0.4.1)
43
+ mail (2.2.7)
44
+ activesupport (>= 2.3.6)
45
+ mime-types
46
+ treetop (>= 1.4.5)
47
+ mime-types (1.16)
48
+ polyglot (0.3.1)
49
+ rack (1.2.1)
50
+ rack-mount (0.6.13)
51
+ rack (>= 1.0.0)
52
+ rack-test (0.5.6)
53
+ rack (>= 1.0)
54
+ rails (3.0.1)
55
+ actionmailer (= 3.0.1)
56
+ actionpack (= 3.0.1)
57
+ activerecord (= 3.0.1)
58
+ activeresource (= 3.0.1)
59
+ activesupport (= 3.0.1)
60
+ bundler (~> 1.0.0)
61
+ railties (= 3.0.1)
62
+ railties (3.0.1)
63
+ actionpack (= 3.0.1)
64
+ activesupport (= 3.0.1)
65
+ rake (>= 0.8.4)
66
+ thor (~> 0.14.0)
67
+ rake (0.8.7)
68
+ rr (1.0.0)
69
+ rspec (2.0.0)
70
+ rspec-core (= 2.0.0)
71
+ rspec-expectations (= 2.0.0)
72
+ rspec-mocks (= 2.0.0)
73
+ rspec-core (2.0.0)
74
+ rspec-expectations (2.0.0)
75
+ diff-lcs (>= 1.1.2)
76
+ rspec-mocks (2.0.0)
77
+ rspec-core (= 2.0.0)
78
+ rspec-expectations (= 2.0.0)
79
+ thor (0.14.3)
80
+ treetop (1.4.8)
81
+ polyglot (>= 0.3.1)
82
+ tzinfo (0.3.23)
83
+
84
+ PLATFORMS
85
+ ruby
86
+
87
+ DEPENDENCIES
88
+ bundler (>= 1.0.0.rc.5)
89
+ nested_form!
90
+ rails (~> 3.0.0)
91
+ rr
92
+ rspec (~> 2.0.0.beta.22)
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Ryan Bates
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,62 @@
1
+ = Nested Form
2
+
3
+ A Rails gem to conveniently manage multiple nested models in a single form. It does so in an unobtrusive way through jQuery.
4
+
5
+ This has been updated. To learn more about how originally worked under the hood: http://blog.madebydna.com/dynamic-nested-forms-in-rails-3-with-the-nest
6
+
7
+ I have modified this to allow the use of nested_form as a table view. This has changed the the underlying implementation by:
8
+
9
+ * storing the blueprint template for add more in a hidden textarea, instead of a div. to maintain artibrary html snippet without getting affected with DOM rendering (like Javascript Templates). this is convinient for storing a <tr> base tempalate.
10
+ * The container <div class="fields"> is no longer automatically generated. you are assumed to generate your own entry container with that proper class "fields". as show in the updated *usage* section below.
11
+
12
+ == Install
13
+
14
+ Add it to your Gemfile
15
+
16
+ gem "nested_form", :git => "git://github.com/modsaid/nested_form.git"
17
+
18
+ Run
19
+
20
+ bundle install
21
+
22
+ Run the generator
23
+
24
+ rails generate nested_form:install
25
+
26
+ == Usage
27
+
28
+ Running the generator will add a file at public/javascripts/nested_form.js which should be included after jQuery.
29
+
30
+ <%= javascript_include_tag 'jquery', 'nested_form' %>
31
+
32
+ You can then generate a nested form using the nested_form_for helper method.
33
+
34
+ <%= nested_form_for @project do |f| %>
35
+
36
+ Use this form just like normal, including the fields_for helper method for nesting models. The benefit of this plugin comes from the link_to_add and link_to_remove helper methods on the form builder.
37
+ <table>
38
+ <%= f.fields_for :tasks do |task_form| %>
39
+ <tr class="fields">
40
+ <td><%= task_form.text_field :name %></td>
41
+ <td><%= task_form.link_to_remove "Remove this task" %></td>
42
+ <tr>
43
+ <% end %>
44
+ <tr>
45
+ <td><%= f.link_to_add "Add a task", :tasks %></td>
46
+ </tr>
47
+ </table>
48
+
49
+ This generates links which dynamically add and remove fields.
50
+
51
+ == Partials
52
+
53
+ 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.
54
+
55
+ <%= f.fields_for :tasks %>
56
+
57
+ In this case it will look for a partial called "task_fields" and pass the form builder as an f variable to it.
58
+
59
+ == Special Thanks
60
+
61
+ This gem is based on the plugin by Ryan Bates: http://github.com/ryanb/nested_form
62
+
@@ -0,0 +1,16 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rubygems'
5
+ require 'rake'
6
+ require 'spec/rake/spectask'
7
+
8
+ spec_files = Rake::FileList["spec/**/*_spec.rb"]
9
+
10
+ desc "Run specs"
11
+ Spec::Rake::SpecTask.new do |t|
12
+ t.spec_files = spec_files
13
+ t.spec_opts = ["-c"]
14
+ end
15
+
16
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ module NestedForm
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ def self.source_root
5
+ File.dirname(__FILE__) + "/templates"
6
+ end
7
+
8
+
9
+ def copy_jquery_file
10
+ copy_file 'nested_form.js', 'public/javascripts/nested_form.js'
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,46 @@
1
+ $(function() {
2
+ $('form a.add_nested_fields').live('click', function() {
3
+ // Setup
4
+ var assoc = $(this).attr('data-association'); // Name of child
5
+ var content = $('#' + assoc + '_fields_blueprint').text(); // Fields template
6
+
7
+ // Make the context correct by replacing new_<parents> with the generated ID
8
+ // of each of the parent objects
9
+ var context = ($(this).parents('.fields').children('input:first').attr('name') || '').replace(new RegExp('\[[a-z]+\]$'), '');
10
+
11
+ // context will be something like this for a brand new form:
12
+ // project[tasks_attributes][1255929127459][assignments_attributes][1255929128105]
13
+ // or for an edit form:
14
+ // project[tasks_attributes][0][assignments_attributes][1]
15
+ if(context) {
16
+ var parent_names = context.match(/[a-z_]+_attributes/g) || [];
17
+ var parent_ids = context.match(/[0-9]+/g);
18
+
19
+ for(i = 0; i < parent_names.length; i++) {
20
+ if(parent_ids[i]) {
21
+ content = content.replace(
22
+ new RegExp('(\\[' + parent_names[i] + '\\])\\[.+?\\]', 'g'),
23
+ '$1[' + parent_ids[i] + ']'
24
+ )
25
+ }
26
+ }
27
+ }
28
+
29
+ // Make a unique ID for the new child
30
+ var regexp = new RegExp('new_' + assoc, 'g');
31
+ var new_id = new Date().getTime();
32
+ content = content.replace(regexp, new_id);
33
+
34
+ $(this).before(content);
35
+ return false;
36
+ });
37
+
38
+ $('form a.remove_nested_fields').live('click', function() {
39
+ var hidden_field = $(this).prev('input[type=hidden]')[0];
40
+ if(hidden_field) {
41
+ hidden_field.value = '1';
42
+ }
43
+ $(this).closest('.fields').hide();
44
+ return false;
45
+ });
46
+ });
@@ -0,0 +1,6 @@
1
+ require "nested_form/builder"
2
+ require "nested_form/view_helper"
3
+
4
+ module NestedForm
5
+
6
+ end
@@ -0,0 +1,35 @@
1
+ module NestedForm
2
+ class Builder < ActionView::Helpers::FormBuilder
3
+ def link_to_add(name, association)
4
+ @fields ||= {}
5
+ @template.after_nested_form(association) do
6
+ model_object = object.class.reflect_on_association(association).klass.new
7
+ output = %Q[<textarea id="#{association}_fields_blueprint" style="display: none">].html_safe
8
+ output << fields_for(association, model_object, :child_index => "new_#{association}", &@fields[association])
9
+ output.safe_concat('</textarea>')
10
+ output
11
+ end
12
+ @template.link_to(name, "javascript:void(0)", :class => "add_nested_fields", "data-association" => association)
13
+ end
14
+
15
+ def link_to_remove(name)
16
+ hidden_field(:_destroy) + @template.link_to(name, "javascript:void(0)", :class => "remove_nested_fields")
17
+ end
18
+
19
+
20
+ def fields_for_with_nested_attributes(association, args, block)
21
+ @fields ||= {}
22
+ @fields[association] = block
23
+ super
24
+ end
25
+
26
+
27
+ def fields_for_nested_model(name, association, args, block)
28
+ # output = '<div class="fields">'.html_safe
29
+ # output << super
30
+ # output.safe_concat('</div>')
31
+ # output
32
+ super.html_safe
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,3 @@
1
+ module NestedForm
2
+ VERSION = "0.0.3"
3
+ end
@@ -0,0 +1,22 @@
1
+ module NestedForm
2
+ module ViewHelper
3
+ def nested_form_for(*args, &block)
4
+ options = args.extract_options!.reverse_merge(:builder => NestedForm::Builder)
5
+ output = form_for(*(args << options), &block)
6
+ @after_nested_form_callbacks ||= {}
7
+ fields = @after_nested_form_callbacks.map do |key, callback|
8
+ callback.call
9
+ end
10
+ output << fields.join(" ").html_safe
11
+ end
12
+
13
+ def after_nested_form(association, &block)
14
+ @after_nested_form_callbacks ||= {}
15
+ @after_nested_form_callbacks[association] = block
16
+ end
17
+ end
18
+ end
19
+
20
+ class ActionView::Base
21
+ include NestedForm::ViewHelper
22
+ end
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
3
+ require 'nested_form/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "flexible-nested-form"
7
+ s.version = NestedForm::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["modsaid","Ryan Bates", "Andrea Singh"]
10
+ s.email = ["mahmoud@modsaid.com","info@madebydna.com"]
11
+ s.homepage = "http://github.com/madebydna/nested_form"
12
+ s.summary = "Gem to conveniently handle multiple models in a single form."
13
+ s.description = "Gem to conveniently handle multiple models in a single form. Rails 2 plugin by Ryan Bates converted into a gem."
14
+
15
+ s.required_rubygems_version = ">= 1.3.6"
16
+ s.rubyforge_project = "nested_form"
17
+
18
+ s.add_development_dependency "bundler", ">= 1.0.0.rc.5"
19
+ s.add_development_dependency "rspec", "~> 2.0.0.beta.22"
20
+ s.add_development_dependency "rr"
21
+ s.add_development_dependency "rails", "~> 3.0.0"
22
+
23
+ s.files = `git ls-files`.split("\n")
24
+ s.executables = `git ls-files`.split("\n").select{|f| f =~ /^bin/}
25
+ s.require_path = 'lib'
26
+ end
@@ -0,0 +1,36 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe NestedForm::Builder do
4
+ describe "with no options" do
5
+ before(:each) do
6
+ @project = Project.new
7
+ @template = ActionView::Base.new
8
+ @template.output_buffer = ""
9
+ @builder = NestedForm::Builder.new(:item, @project, @template, {}, proc {})
10
+ end
11
+
12
+ it "should have an add link" do
13
+ @builder.link_to_add("Add", :tasks).should == '<a href="javascript:void(0)" class="add_nested_fields" data-association="tasks">Add</a>'
14
+ end
15
+
16
+
17
+ it "should have a remove link" do
18
+ @builder.link_to_remove("Remove").should == '<input id="item__destroy" name="item[_destroy]" type="hidden" value="false" /><a href="javascript:void(0)" class="remove_nested_fields">Remove</a>'
19
+ end
20
+
21
+ it "should wrap nested fields each in a div with class" do
22
+ 2.times { @project.tasks.build }
23
+ @builder.fields_for(:tasks) do
24
+ "Task"
25
+ end.should == '<div class="fields">Task</div><div class="fields">Task</div>'
26
+ end
27
+
28
+ it "should add task fields to hidden div after form" do
29
+ output = ""
30
+ mock(@template).after_nested_form(:tasks) { |arg, block| output << block.call }
31
+ @builder.fields_for(:tasks) { "Task" }
32
+ @builder.link_to_add("Add", :tasks)
33
+ output.should == '<div id="tasks_fields_blueprint" style="display: none"><div class="fields">Task</div></div>'
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,32 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe NestedForm::ViewHelper do
4
+ before(:each) do
5
+ @template = ActionView::Base.new
6
+ @template.output_buffer = ""
7
+ stub(@template).url_for { "" }
8
+ stub(@template).projects_path { "" }
9
+ stub(@template).protect_against_forgery? { false }
10
+ end
11
+
12
+ it "should pass nested form builder to form_for along with other options" do
13
+ mock.proxy(@template).form_for(:first, :as => :second, :other => :arg, :builder => NestedForm::Builder) do |form_html|
14
+ form_html
15
+ end
16
+ @template.nested_form_for(:first, :as => :second, :other => :arg) {"form"}
17
+ end
18
+
19
+ it "should pass instance of NestedForm::Builder to nested_form_for block" do
20
+ @template.nested_form_for(Project.new) do |f|
21
+ f.should be_instance_of(NestedForm::Builder)
22
+ end
23
+ end
24
+
25
+ it "should append content to end of nested form" do
26
+ @template.after_nested_form(:tasks) { @template.concat("123") }
27
+ @template.after_nested_form(:milestones) { @template.concat("456") }
28
+ @template.nested_form_for(Project.new) {}
29
+ @template.output_buffer.should include("123456")
30
+ end
31
+ end
32
+
@@ -0,0 +1,46 @@
1
+ require 'active_model'
2
+ require 'active_record'
3
+ require 'action_controller'
4
+ require 'action_view'
5
+ require 'action_view/template'
6
+
7
+ require 'nested_form/view_helper'
8
+ require 'nested_form/builder'
9
+
10
+ Rspec.configure do |config|
11
+ config.mock_with :rr
12
+ end
13
+
14
+ class TablelessModel < ActiveRecord::Base
15
+ def self.columns() @columns ||= []; end
16
+
17
+ def self.column(name, sql_type = nil, default = nil, null = true)
18
+ columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
19
+ end
20
+
21
+ def self.quoted_table_name
22
+ name.pluralize.underscore
23
+ end
24
+
25
+ def quoted_id
26
+ "0"
27
+ end
28
+ end
29
+
30
+ class Project < TablelessModel
31
+ column :name, :string
32
+ has_many :tasks
33
+ accepts_nested_attributes_for :tasks
34
+ end
35
+
36
+ class Task < TablelessModel
37
+ column :project_id, :integer
38
+ column :name, :string
39
+ belongs_to :project
40
+ end
41
+
42
+ class Milestone < TablelessModel
43
+ column :task_id, :integer
44
+ column :name, :string
45
+ belongs_to :task
46
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: flexible-nested-form
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - modsaid
9
+ - Ryan Bates
10
+ - Andrea Singh
11
+ autorequire:
12
+ bindir: bin
13
+ cert_chain: []
14
+ date: 2011-04-27 00:00:00.000000000 +02:00
15
+ default_executable:
16
+ dependencies:
17
+ - !ruby/object:Gem::Dependency
18
+ name: bundler
19
+ requirement: &25019900 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ! '>='
23
+ - !ruby/object:Gem::Version
24
+ version: 1.0.0.rc.5
25
+ type: :development
26
+ prerelease: false
27
+ version_requirements: *25019900
28
+ - !ruby/object:Gem::Dependency
29
+ name: rspec
30
+ requirement: &25019440 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ~>
34
+ - !ruby/object:Gem::Version
35
+ version: 2.0.0.beta.22
36
+ type: :development
37
+ prerelease: false
38
+ version_requirements: *25019440
39
+ - !ruby/object:Gem::Dependency
40
+ name: rr
41
+ requirement: &25019060 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ type: :development
48
+ prerelease: false
49
+ version_requirements: *25019060
50
+ - !ruby/object:Gem::Dependency
51
+ name: rails
52
+ requirement: &25018520 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ~>
56
+ - !ruby/object:Gem::Version
57
+ version: 3.0.0
58
+ type: :development
59
+ prerelease: false
60
+ version_requirements: *25018520
61
+ description: Gem to conveniently handle multiple models in a single form. Rails 2
62
+ plugin by Ryan Bates converted into a gem.
63
+ email:
64
+ - mahmoud@modsaid.com
65
+ - info@madebydna.com
66
+ executables: []
67
+ extensions: []
68
+ extra_rdoc_files: []
69
+ files:
70
+ - .bundle/config
71
+ - .gitignore
72
+ - Gemfile
73
+ - Gemfile.lock
74
+ - LICENSE
75
+ - README.rdoc
76
+ - Rakefile
77
+ - lib/generators/nested_form/install_generator.rb
78
+ - lib/generators/nested_form/templates/nested_form.js
79
+ - lib/nested_form.rb
80
+ - lib/nested_form/builder.rb
81
+ - lib/nested_form/version.rb
82
+ - lib/nested_form/view_helper.rb
83
+ - nested_form.gemspec
84
+ - spec/nested_form/builder_spec.rb
85
+ - spec/nested_form/view_helper_spec.rb
86
+ - spec/spec_helper.rb
87
+ has_rdoc: true
88
+ homepage: http://github.com/madebydna/nested_form
89
+ licenses: []
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ none: false
102
+ requirements:
103
+ - - ! '>='
104
+ - !ruby/object:Gem::Version
105
+ version: 1.3.6
106
+ requirements: []
107
+ rubyforge_project: nested_form
108
+ rubygems_version: 1.6.2
109
+ signing_key:
110
+ specification_version: 3
111
+ summary: Gem to conveniently handle multiple models in a single form.
112
+ test_files: []