nested_form_tougg 0.0.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.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ZTlkNjE1YWY4NTVjYmRkOTZiOWIwMDgyMDc1OTI0MTE3ZWI2NmQ3Yg==
5
+ data.tar.gz: !binary |-
6
+ ZGEyMTU2ZWQzZDgxZGFmMjhjNWNmN2FiYzM4YTdjMDBlOTVhZDQ5NA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ Zjg3MTQ1MjMyMzdhZGE2OGM5ZTFlYjVlYjliNmVhM2NmNjc5MzMzNmFkM2Nk
10
+ MGI2OWJjODc1ZGEzYmIyNmJjMGYwNWQ2ZTBhNjE2NjI0MjFlYjNjZDQwMmMx
11
+ NWQ4ZTQzYzNmZTA2ZjE0NmNiZWY3NDAxYTliYzEwYzZjZDZkMWU=
12
+ data.tar.gz: !binary |-
13
+ NmZjN2M4NjIwY2FlYjNkMGM4ZDNiYmUzYWE2ZWIyYTIyMTlhNTFkZWMwYjg0
14
+ YTBlZjdlYzc4M2FkYWI4MDUyMjkyYTYwYjczNDMwYjE3NzQwNDQzMzRiYTY2
15
+ NzZiY2NmNGY3YWE4YjU0MGUyNjMzMGYzNjRlMTdhMjllNGY0ZjY=
data/.bundle/config ADDED
@@ -0,0 +1 @@
1
+ --- {}
data/.gitignore ADDED
@@ -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
data/Gemfile.lock ADDED
@@ -0,0 +1,97 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ nested_form_tougg (0.0.0)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ abstract (1.0.0)
10
+ actionmailer (3.0.20)
11
+ actionpack (= 3.0.20)
12
+ mail (~> 2.2.19)
13
+ actionpack (3.0.20)
14
+ activemodel (= 3.0.20)
15
+ activesupport (= 3.0.20)
16
+ builder (~> 2.1.2)
17
+ erubis (~> 2.6.6)
18
+ i18n (~> 0.5.0)
19
+ rack (~> 1.2.5)
20
+ rack-mount (~> 0.6.14)
21
+ rack-test (~> 0.5.7)
22
+ tzinfo (~> 0.3.23)
23
+ activemodel (3.0.20)
24
+ activesupport (= 3.0.20)
25
+ builder (~> 2.1.2)
26
+ i18n (~> 0.5.0)
27
+ activerecord (3.0.20)
28
+ activemodel (= 3.0.20)
29
+ activesupport (= 3.0.20)
30
+ arel (~> 2.0.10)
31
+ tzinfo (~> 0.3.23)
32
+ activeresource (3.0.20)
33
+ activemodel (= 3.0.20)
34
+ activesupport (= 3.0.20)
35
+ activesupport (3.0.20)
36
+ arel (2.0.10)
37
+ builder (2.1.2)
38
+ diff-lcs (1.2.5)
39
+ erubis (2.6.6)
40
+ abstract (>= 1.0.0)
41
+ i18n (0.5.3)
42
+ json (1.8.1)
43
+ mail (2.2.20)
44
+ activesupport (>= 2.3.6)
45
+ i18n (>= 0.4.0)
46
+ mime-types (~> 1.16)
47
+ treetop (~> 1.4.8)
48
+ mime-types (1.25.1)
49
+ polyglot (0.3.4)
50
+ rack (1.2.8)
51
+ rack-mount (0.6.14)
52
+ rack (>= 1.0.0)
53
+ rack-test (0.5.7)
54
+ rack (>= 1.0)
55
+ rails (3.0.20)
56
+ actionmailer (= 3.0.20)
57
+ actionpack (= 3.0.20)
58
+ activerecord (= 3.0.20)
59
+ activeresource (= 3.0.20)
60
+ activesupport (= 3.0.20)
61
+ bundler (~> 1.0)
62
+ railties (= 3.0.20)
63
+ railties (3.0.20)
64
+ actionpack (= 3.0.20)
65
+ activesupport (= 3.0.20)
66
+ rake (>= 0.8.7)
67
+ rdoc (~> 3.4)
68
+ thor (~> 0.14.4)
69
+ rake (10.1.1)
70
+ rdoc (3.12.2)
71
+ json (~> 1.4)
72
+ rr (1.1.2)
73
+ rspec (2.0.1)
74
+ rspec-core (~> 2.0.1)
75
+ rspec-expectations (~> 2.0.1)
76
+ rspec-mocks (~> 2.0.1)
77
+ rspec-core (2.0.1)
78
+ rspec-expectations (2.0.1)
79
+ diff-lcs (>= 1.1.2)
80
+ rspec-mocks (2.0.1)
81
+ rspec-core (~> 2.0.1)
82
+ rspec-expectations (~> 2.0.1)
83
+ thor (0.14.6)
84
+ treetop (1.4.15)
85
+ polyglot
86
+ polyglot (>= 0.3.1)
87
+ tzinfo (0.3.39)
88
+
89
+ PLATFORMS
90
+ ruby
91
+
92
+ DEPENDENCIES
93
+ bundler (= 1.3.5)
94
+ nested_form_tougg!
95
+ rails (~> 3.0.0)
96
+ rr
97
+ 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.
data/README.rdoc ADDED
@@ -0,0 +1,52 @@
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
+ To learn more about how this works under the hood: http://blog.madebydna.com/dynamic-nested-forms-in-rails-3-with-the-nest
6
+
7
+ == Install
8
+
9
+ Add it to your Gemfile
10
+
11
+ gem "nested_form", :git => "git://github.com/madebydna/nested_form.git"
12
+
13
+ Run
14
+
15
+ bundle install
16
+
17
+ Run the generator
18
+
19
+ rails generate nested_form:install
20
+
21
+ == Usage
22
+
23
+ Running the generator will add a file at public/javascripts/nested_form.js which should be included after jQuery.
24
+
25
+ <%= javascript_include_tag 'jquery', 'nested_form' %>
26
+
27
+ You can then generate a nested form using the nested_form_for helper method.
28
+
29
+ <%= nested_form_for @project do |f| %>
30
+
31
+ 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.
32
+
33
+ <%= f.fields_for :tasks do |task_form| %>
34
+ <%= task_form.text_field :name %>
35
+ <%= task_form.link_to_remove "Remove this task" %>
36
+ <% end %>
37
+ <%= f.link_to_add "Add a task", :tasks %>
38
+
39
+ This generates links which dynamically add and remove fields.
40
+
41
+ == Partials
42
+
43
+ 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.
44
+
45
+ <%= f.fields_for :tasks %>
46
+
47
+ In this case it will look for a partial called "task_fields" and pass the form builder as an f variable to it.
48
+
49
+ == Special Thanks
50
+
51
+ This gem is based on the plugin by Ryan Bates: http://github.com/ryanb/nested_form
52
+
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
@@ -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').html(); // 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).closest('.fields').find('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,34 @@
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[<div 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('</div>')
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
+ end
33
+ end
34
+ end
@@ -0,0 +1,3 @@
1
+ module NestedForm
2
+ VERSION = "0.0.0"
3
+ end
@@ -0,0 +1,26 @@
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 |callback|
8
+ callback.call
9
+ end
10
+ output << fields.join(" ").html_safe
11
+ end
12
+
13
+ def after_nested_form(association, &block)
14
+ @associations ||= []
15
+ @after_nested_form_callbacks ||= []
16
+ unless @associations.include?(association)
17
+ @associations << association
18
+ @after_nested_form_callbacks << block
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ class ActionView::Base
25
+ include NestedForm::ViewHelper
26
+ end
@@ -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,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 = "nested_form_tougg"
7
+ s.version = NestedForm::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Ryan Bates", "Andrea Singh"]
10
+ s.email = ["tougg@tougg.com.br"]
11
+ s.homepage = "http://github.com/tougg/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.3.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,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nested_form_tougg
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Ryan Bates
8
+ - Andrea Singh
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-03-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - '='
19
+ - !ruby/object:Gem::Version
20
+ version: 1.3.5
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - '='
26
+ - !ruby/object:Gem::Version
27
+ version: 1.3.5
28
+ - !ruby/object:Gem::Dependency
29
+ name: rspec
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ~>
33
+ - !ruby/object:Gem::Version
34
+ version: 2.0.0.beta.22
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ~>
40
+ - !ruby/object:Gem::Version
41
+ version: 2.0.0.beta.22
42
+ - !ruby/object:Gem::Dependency
43
+ name: rr
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rails
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ~>
61
+ - !ruby/object:Gem::Version
62
+ version: 3.0.0
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 3.0.0
70
+ description: Gem to conveniently handle multiple models in a single form. Rails 2
71
+ plugin by Ryan Bates converted into a gem.
72
+ email:
73
+ - tougg@tougg.com.br
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - .bundle/config
79
+ - .gitignore
80
+ - Gemfile
81
+ - Gemfile.lock
82
+ - LICENSE
83
+ - README.rdoc
84
+ - Rakefile
85
+ - lib/generators/nested_form/install_generator.rb
86
+ - lib/generators/nested_form/templates/nested_form.js
87
+ - lib/nested_form.rb
88
+ - lib/nested_form/builder.rb
89
+ - lib/nested_form/version.rb
90
+ - lib/nested_form/view_helper.rb
91
+ - nested_form.gemspec
92
+ - spec/nested_form/builder_spec.rb
93
+ - spec/nested_form/view_helper_spec.rb
94
+ - spec/spec_helper.rb
95
+ homepage: http://github.com/tougg/nested_form
96
+ licenses: []
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ! '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ! '>='
110
+ - !ruby/object:Gem::Version
111
+ version: 1.3.6
112
+ requirements: []
113
+ rubyforge_project: nested_form
114
+ rubygems_version: 2.2.2
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: Gem to conveniently handle multiple models in a single form.
118
+ test_files: []