cocoon 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. data/.gitignore +7 -0
  2. data/Gemfile +19 -0
  3. data/Gemfile.lock +107 -0
  4. data/MIT-LICENSE +20 -0
  5. data/README.markdown +150 -0
  6. data/Rakefile +42 -0
  7. data/VERSION +1 -0
  8. data/cocoon.gemspec +104 -0
  9. data/lib/cocoon.rb +16 -0
  10. data/lib/cocoon/view_helpers.rb +37 -0
  11. data/lib/generators/cocoon/install/install_generator.rb +13 -0
  12. data/lib/generators/cocoon/install/templates/cocoon.js +28 -0
  13. data/spec/cocoon_spec.rb +14 -0
  14. data/spec/dummy/Rakefile +7 -0
  15. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  16. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  17. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  18. data/spec/dummy/config.ru +4 -0
  19. data/spec/dummy/config/application.rb +45 -0
  20. data/spec/dummy/config/boot.rb +10 -0
  21. data/spec/dummy/config/database.yml +22 -0
  22. data/spec/dummy/config/environment.rb +5 -0
  23. data/spec/dummy/config/environments/development.rb +26 -0
  24. data/spec/dummy/config/environments/production.rb +49 -0
  25. data/spec/dummy/config/environments/test.rb +35 -0
  26. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  27. data/spec/dummy/config/initializers/inflections.rb +10 -0
  28. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  29. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  30. data/spec/dummy/config/initializers/session_store.rb +8 -0
  31. data/spec/dummy/config/locales/en.yml +5 -0
  32. data/spec/dummy/config/routes.rb +58 -0
  33. data/spec/dummy/public/404.html +26 -0
  34. data/spec/dummy/public/422.html +26 -0
  35. data/spec/dummy/public/500.html +26 -0
  36. data/spec/dummy/public/favicon.ico +0 -0
  37. data/spec/dummy/public/javascripts/application.js +2 -0
  38. data/spec/dummy/public/javascripts/controls.js +965 -0
  39. data/spec/dummy/public/javascripts/dragdrop.js +974 -0
  40. data/spec/dummy/public/javascripts/effects.js +1123 -0
  41. data/spec/dummy/public/javascripts/prototype.js +6001 -0
  42. data/spec/dummy/public/javascripts/rails.js +175 -0
  43. data/spec/dummy/public/stylesheets/.gitkeep +0 -0
  44. data/spec/dummy/script/rails +6 -0
  45. data/spec/integration/navigation_spec.rb +9 -0
  46. data/spec/spec_helper.rb +34 -0
  47. metadata +140 -0
data/.gitignore ADDED
@@ -0,0 +1,7 @@
1
+ .bundle/
2
+ log/*.log
3
+ pkg/
4
+ spec/dummy/db/*.sqlite3
5
+ spec/dummy/log/*.log
6
+ spec/dummy/tmp/
7
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,19 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "rails", "3.0.3"
4
+ #gem "capybara", ">= 0.4.0"
5
+ gem "sqlite3-ruby", :require => "sqlite3"
6
+
7
+ gem "json_pure"
8
+
9
+ group :development, :test do
10
+ gem "jeweler"
11
+ gem "rspec-rails", ">= 2.0.0.beta"
12
+ gem "rspec", ">= 2.0.0.rc"
13
+ gem "actionpack", ">=3.0.0"
14
+ gem "simplecov", :require => false
15
+ end
16
+
17
+ # To use debugger (ruby-debug for Ruby 1.8.7+, ruby-debug19 for Ruby 1.9.2+)
18
+ # gem 'ruby-debug'
19
+ # gem 'ruby-debug19'
data/Gemfile.lock ADDED
@@ -0,0 +1,107 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ abstract (1.0.0)
5
+ actionmailer (3.0.3)
6
+ actionpack (= 3.0.3)
7
+ mail (~> 2.2.9)
8
+ actionpack (3.0.3)
9
+ activemodel (= 3.0.3)
10
+ activesupport (= 3.0.3)
11
+ builder (~> 2.1.2)
12
+ erubis (~> 2.6.6)
13
+ i18n (~> 0.4)
14
+ rack (~> 1.2.1)
15
+ rack-mount (~> 0.6.13)
16
+ rack-test (~> 0.5.6)
17
+ tzinfo (~> 0.3.23)
18
+ activemodel (3.0.3)
19
+ activesupport (= 3.0.3)
20
+ builder (~> 2.1.2)
21
+ i18n (~> 0.4)
22
+ activerecord (3.0.3)
23
+ activemodel (= 3.0.3)
24
+ activesupport (= 3.0.3)
25
+ arel (~> 2.0.2)
26
+ tzinfo (~> 0.3.23)
27
+ activeresource (3.0.3)
28
+ activemodel (= 3.0.3)
29
+ activesupport (= 3.0.3)
30
+ activesupport (3.0.3)
31
+ arel (2.0.7)
32
+ builder (2.1.2)
33
+ diff-lcs (1.1.2)
34
+ erubis (2.6.6)
35
+ abstract (>= 1.0.0)
36
+ gemcutter (0.6.1)
37
+ git (1.2.5)
38
+ i18n (0.5.0)
39
+ jeweler (1.4.0)
40
+ gemcutter (>= 0.1.0)
41
+ git (>= 1.2.5)
42
+ rubyforge (>= 2.0.0)
43
+ json_pure (1.4.6)
44
+ mail (2.2.15)
45
+ activesupport (>= 2.3.6)
46
+ i18n (>= 0.4.0)
47
+ mime-types (~> 1.16)
48
+ treetop (~> 1.4.8)
49
+ mime-types (1.16)
50
+ polyglot (0.3.1)
51
+ rack (1.2.1)
52
+ rack-mount (0.6.13)
53
+ rack (>= 1.0.0)
54
+ rack-test (0.5.7)
55
+ rack (>= 1.0)
56
+ rails (3.0.3)
57
+ actionmailer (= 3.0.3)
58
+ actionpack (= 3.0.3)
59
+ activerecord (= 3.0.3)
60
+ activeresource (= 3.0.3)
61
+ activesupport (= 3.0.3)
62
+ bundler (~> 1.0)
63
+ railties (= 3.0.3)
64
+ railties (3.0.3)
65
+ actionpack (= 3.0.3)
66
+ activesupport (= 3.0.3)
67
+ rake (>= 0.8.7)
68
+ thor (~> 0.14.4)
69
+ rake (0.8.7)
70
+ rspec (2.4.0)
71
+ rspec-core (~> 2.4.0)
72
+ rspec-expectations (~> 2.4.0)
73
+ rspec-mocks (~> 2.4.0)
74
+ rspec-core (2.4.0)
75
+ rspec-expectations (2.4.0)
76
+ diff-lcs (~> 1.1.2)
77
+ rspec-mocks (2.4.0)
78
+ rspec-rails (2.4.1)
79
+ actionpack (~> 3.0)
80
+ activesupport (~> 3.0)
81
+ railties (~> 3.0)
82
+ rspec (~> 2.4.0)
83
+ rubyforge (2.0.4)
84
+ json_pure (>= 1.1.7)
85
+ simplecov (0.3.7)
86
+ simplecov-html (>= 0.3.7)
87
+ simplecov-html (0.3.9)
88
+ sqlite3 (1.3.3)
89
+ sqlite3-ruby (1.3.3)
90
+ sqlite3 (>= 1.3.3)
91
+ thor (0.14.6)
92
+ treetop (1.4.9)
93
+ polyglot (>= 0.3.1)
94
+ tzinfo (0.3.24)
95
+
96
+ PLATFORMS
97
+ ruby
98
+
99
+ DEPENDENCIES
100
+ actionpack (>= 3.0.0)
101
+ jeweler
102
+ json_pure
103
+ rails (= 3.0.3)
104
+ rspec (>= 2.0.0.rc)
105
+ rspec-rails (>= 2.0.0.beta)
106
+ simplecov
107
+ sqlite3-ruby
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2011 YOURNAME
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.markdown ADDED
@@ -0,0 +1,150 @@
1
+ # cocoon
2
+
3
+ cocoon is a Rails3 gem to allow easier handling of nested forms.
4
+
5
+ Nested forms are forms that handle nested models and attributes in one form.
6
+ For example a project with its tasks, an invoice with its ordered items.
7
+
8
+ It is formbuilder-agnostic, so it works with standard Rails, or Formtastic or simple_form.
9
+
10
+ ## Prerequisites
11
+
12
+ This gem uses jQuery, it is most useful to use this gem in a rails3
13
+ project where you are already using jQuery.
14
+
15
+ Furthermore i would advice you to use either formtastic or simple_form.
16
+
17
+ I have a sample project where I demonstrate the use of cocoon with formtastic.
18
+
19
+ ## Installation
20
+
21
+ Inside your `Gemfile` add the following:
22
+
23
+ gem "cocoon"
24
+
25
+ Run the installation task:
26
+
27
+ rails g cocoon:install
28
+
29
+ This will install the needed javascript file.
30
+ Inside your `application.html.haml` you will need to add below the default javascripts:
31
+
32
+ = javascript_include_tag :cocoon
33
+
34
+ or using erb, you write
35
+
36
+ <%= javascript_include_tag :cocoon %>
37
+
38
+ That is all you need to do to start using it!
39
+
40
+ ## Usage
41
+
42
+ Suppose you have a model `Project`:
43
+
44
+ rails g scaffold Project name:string description:string
45
+
46
+ and a project has many `tasks`:
47
+
48
+ rails g model Task description:string done:boolean project_id:integer
49
+
50
+ Edit the models to code the relation:
51
+
52
+ class Project < ActiveRecord::Base
53
+ has_many :tasks
54
+ accepts_nested_attributes_for :tasks
55
+ end
56
+
57
+ class Task < ActiveRecord::Base
58
+ belongs_to :project
59
+ end
60
+
61
+ What we want to achieve is to get a form where we can add and remove the tasks dynamically.
62
+ What we need for this, is that the fields for a new/existing `task` are defined in a partial
63
+ view called `_task_fields.html`.
64
+
65
+ We will show the sample usage with the different possible form-builders.
66
+
67
+ ### Using formtastic
68
+
69
+ Inside our `projects/_form` partial we then write:
70
+
71
+ - f.inputs do
72
+ = f.input :name
73
+ = f.input :description
74
+ %h3 Tasks
75
+ #tasks
76
+ = f.semantic_fields_for :tasks do |task|
77
+ = render 'task_fields', :f => task
78
+ .links
79
+ = link_to_add_association 'add task', f, :tasks
80
+ -f.buttons do
81
+ = f.submit 'Save'
82
+
83
+ and inside the `_task_fields` partial we write:
84
+
85
+ .nested-fields
86
+ = f.inputs do
87
+ = f.input :description
88
+ = f.input :done, :as => :boolean
89
+ = link_to_remove_association "remove task", f
90
+
91
+ That is all there is to it!
92
+
93
+ There is an example project on github implementing it called [formtastic-cocoon-demo](https://github.com/nathanvda/formtastic-cocoon-demo).
94
+
95
+ ### Using simple_form
96
+
97
+ There is only line that needs to change, instead of writing `semantic_fields_for` you write `simple_fields_for`.
98
+
99
+ I will provide a full example (and a sample project) later.
100
+
101
+ ### Using standard rails forms
102
+
103
+ I provide a full example (and a sample project) later.
104
+
105
+ ## How it works
106
+
107
+ I define two helper functions:
108
+
109
+ ### link_to_add_association
110
+
111
+ This function will add a link to your markup that will, when clicked, dynamically add a new partial form for the given association.
112
+ This should be placed below the `semantic_fields_for`.
113
+
114
+ It takes three parameters:
115
+
116
+ - name: the text to show in the link
117
+ - f: referring to the containing formtastic form-object
118
+ - association: the name of the association (plural) of which a new instance needs to be added (symbol or string).
119
+
120
+ ### link_to_remove_association
121
+
122
+ This function will add a link to your markup that will, when clicked, dynamically remove the surrounding partial form.
123
+ This should be placed inside the partial `_<association-object-singular>_fields`.
124
+
125
+ ### Partial
126
+
127
+ The partial should be named `_<association-object_singular>_fields`, and should start with a div of class `.nested-fields`.
128
+
129
+ There is no limit to the amount of nesting, though.
130
+
131
+
132
+ ## Note on Patches/Pull Requests
133
+
134
+ * Fork the project.
135
+ * Make your feature addition or bug fix.
136
+ * Add tests for it. This is important so I don't break it in a
137
+ future version unintentionally.
138
+ * Commit, do not mess with rakefile, version, or history.
139
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
140
+ * Send me a pull request. Bonus points for topic branches.
141
+
142
+
143
+ ## Todo
144
+
145
+ * complete the sample projects for simple_form and normal rails forms
146
+ * complete the test-coverage
147
+
148
+ ## Copyright
149
+
150
+ Copyright (c) 2010 Nathan Van der Auwera. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,42 @@
1
+ # encoding: UTF-8
2
+ require 'rubygems'
3
+ begin
4
+ require 'bundler/setup'
5
+ rescue LoadError
6
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
7
+ end
8
+
9
+ require 'rake'
10
+ require 'rake/rdoctask'
11
+
12
+ require 'rspec/core'
13
+ require 'rspec/core/rake_task'
14
+
15
+ RSpec::Core::RakeTask.new(:spec)
16
+
17
+ task :default => :spec
18
+
19
+ Rake::RDocTask.new(:rdoc) do |rdoc|
20
+ rdoc.rdoc_dir = 'rdoc'
21
+ rdoc.title = 'Cocoon'
22
+ rdoc.options << '--line-numbers' << '--inline-source'
23
+ rdoc.rdoc_files.include('README.rdoc')
24
+ rdoc.rdoc_files.include('lib/**/*.rb')
25
+ end
26
+
27
+ begin
28
+ require 'jeweler'
29
+ Jeweler::Tasks.new do |gem|
30
+ gem.name = "cocoon"
31
+ gem.summary = %Q{gem that enables easier nested forms with standard forms, formtastic and simple-form}
32
+ gem.description = %Q{Unobtrusive nested forms handling, using jQuery. Use this and discover cocoon-heaven.}
33
+ gem.email = "nathan@dixis.com"
34
+ gem.homepage = "http://github.com/nathanvda/cocoon"
35
+ gem.authors = ["Nathan Van der Auwera"]
36
+ gem.add_development_dependency "rspec", ">= 2.0.0"
37
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
38
+ end
39
+ Jeweler::GemcutterTasks.new
40
+ rescue LoadError
41
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
42
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
data/cocoon.gemspec ADDED
@@ -0,0 +1,104 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{cocoon}
8
+ s.version = "1.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Nathan Van der Auwera"]
12
+ s.date = %q{2011-02-09}
13
+ s.description = %q{Unobtrusive nested forms handling, using jQuery. Use this and discover cocoon-heaven.}
14
+ s.email = %q{nathan@dixis.com}
15
+ s.extra_rdoc_files = [
16
+ "README.markdown"
17
+ ]
18
+ s.files = [
19
+ ".gitignore",
20
+ "Gemfile",
21
+ "Gemfile.lock",
22
+ "MIT-LICENSE",
23
+ "README.markdown",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "cocoon.gemspec",
27
+ "lib/cocoon.rb",
28
+ "lib/cocoon/view_helpers.rb",
29
+ "lib/generators/cocoon/install/install_generator.rb",
30
+ "lib/generators/cocoon/install/templates/cocoon.js",
31
+ "spec/cocoon_spec.rb",
32
+ "spec/dummy/Rakefile",
33
+ "spec/dummy/app/controllers/application_controller.rb",
34
+ "spec/dummy/app/helpers/application_helper.rb",
35
+ "spec/dummy/app/views/layouts/application.html.erb",
36
+ "spec/dummy/config.ru",
37
+ "spec/dummy/config/application.rb",
38
+ "spec/dummy/config/boot.rb",
39
+ "spec/dummy/config/database.yml",
40
+ "spec/dummy/config/environment.rb",
41
+ "spec/dummy/config/environments/development.rb",
42
+ "spec/dummy/config/environments/production.rb",
43
+ "spec/dummy/config/environments/test.rb",
44
+ "spec/dummy/config/initializers/backtrace_silencers.rb",
45
+ "spec/dummy/config/initializers/inflections.rb",
46
+ "spec/dummy/config/initializers/mime_types.rb",
47
+ "spec/dummy/config/initializers/secret_token.rb",
48
+ "spec/dummy/config/initializers/session_store.rb",
49
+ "spec/dummy/config/locales/en.yml",
50
+ "spec/dummy/config/routes.rb",
51
+ "spec/dummy/public/404.html",
52
+ "spec/dummy/public/422.html",
53
+ "spec/dummy/public/500.html",
54
+ "spec/dummy/public/favicon.ico",
55
+ "spec/dummy/public/javascripts/application.js",
56
+ "spec/dummy/public/javascripts/controls.js",
57
+ "spec/dummy/public/javascripts/dragdrop.js",
58
+ "spec/dummy/public/javascripts/effects.js",
59
+ "spec/dummy/public/javascripts/prototype.js",
60
+ "spec/dummy/public/javascripts/rails.js",
61
+ "spec/dummy/public/stylesheets/.gitkeep",
62
+ "spec/dummy/script/rails",
63
+ "spec/integration/navigation_spec.rb",
64
+ "spec/spec_helper.rb"
65
+ ]
66
+ s.homepage = %q{http://github.com/nathanvda/cocoon}
67
+ s.rdoc_options = ["--charset=UTF-8"]
68
+ s.require_paths = ["lib"]
69
+ s.rubygems_version = %q{1.3.7}
70
+ s.summary = %q{gem that enables easier nested forms with standard forms, formtastic and simple-form}
71
+ s.test_files = [
72
+ "spec/spec_helper.rb",
73
+ "spec/dummy/app/controllers/application_controller.rb",
74
+ "spec/dummy/app/helpers/application_helper.rb",
75
+ "spec/dummy/config/environment.rb",
76
+ "spec/dummy/config/initializers/session_store.rb",
77
+ "spec/dummy/config/initializers/mime_types.rb",
78
+ "spec/dummy/config/initializers/secret_token.rb",
79
+ "spec/dummy/config/initializers/backtrace_silencers.rb",
80
+ "spec/dummy/config/initializers/inflections.rb",
81
+ "spec/dummy/config/environments/production.rb",
82
+ "spec/dummy/config/environments/development.rb",
83
+ "spec/dummy/config/environments/test.rb",
84
+ "spec/dummy/config/application.rb",
85
+ "spec/dummy/config/routes.rb",
86
+ "spec/dummy/config/boot.rb",
87
+ "spec/cocoon_spec.rb",
88
+ "spec/integration/navigation_spec.rb"
89
+ ]
90
+
91
+ if s.respond_to? :specification_version then
92
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
93
+ s.specification_version = 3
94
+
95
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
96
+ s.add_development_dependency(%q<rspec>, [">= 2.0.0"])
97
+ else
98
+ s.add_dependency(%q<rspec>, [">= 2.0.0"])
99
+ end
100
+ else
101
+ s.add_dependency(%q<rspec>, [">= 2.0.0"])
102
+ end
103
+ end
104
+