giga-safe-tool 0.0.1

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.
Files changed (65) hide show
  1. checksums.yaml +7 -0
  2. data/cocoon-1.2.15/Gemfile +33 -0
  3. data/cocoon-1.2.15/History.md +197 -0
  4. data/cocoon-1.2.15/LICENSE +20 -0
  5. data/cocoon-1.2.15/README.markdown +612 -0
  6. data/cocoon-1.2.15/Rakefile +89 -0
  7. data/cocoon-1.2.15/VERSION +1 -0
  8. data/cocoon-1.2.15/app/assets/javascripts/cocoon.js +146 -0
  9. data/cocoon-1.2.15/cocoon.gemspec +149 -0
  10. data/cocoon-1.2.15/gemfiles/Gemfile.default +31 -0
  11. data/cocoon-1.2.15/gemfiles/Gemfile.rails-3.2.13 +30 -0
  12. data/cocoon-1.2.15/gemfiles/Gemfile.rails-4-r2 +27 -0
  13. data/cocoon-1.2.15/lib/cocoon/view_helpers.rb +160 -0
  14. data/cocoon-1.2.15/lib/cocoon.rb +20 -0
  15. data/cocoon-1.2.15/lib/generators/cocoon/install/install_generator.rb +17 -0
  16. data/cocoon-1.2.15/npm/README.md +8 -0
  17. data/cocoon-1.2.15/npm/package.json.erb +21 -0
  18. data/cocoon-1.2.15/spec/cocoon_spec.rb +446 -0
  19. data/cocoon-1.2.15/spec/dummy/Rakefile +7 -0
  20. data/cocoon-1.2.15/spec/dummy/app/controllers/application_controller.rb +3 -0
  21. data/cocoon-1.2.15/spec/dummy/app/decorators/comment_decorator.rb +17 -0
  22. data/cocoon-1.2.15/spec/dummy/app/helpers/application_helper.rb +2 -0
  23. data/cocoon-1.2.15/spec/dummy/app/models/comment.rb +7 -0
  24. data/cocoon-1.2.15/spec/dummy/app/models/person.rb +3 -0
  25. data/cocoon-1.2.15/spec/dummy/app/models/post.rb +9 -0
  26. data/cocoon-1.2.15/spec/dummy/app/views/layouts/application.html.erb +14 -0
  27. data/cocoon-1.2.15/spec/dummy/config/application.rb +45 -0
  28. data/cocoon-1.2.15/spec/dummy/config/boot.rb +10 -0
  29. data/cocoon-1.2.15/spec/dummy/config/database.yml +22 -0
  30. data/cocoon-1.2.15/spec/dummy/config/environment.rb +5 -0
  31. data/cocoon-1.2.15/spec/dummy/config/environments/development.rb +25 -0
  32. data/cocoon-1.2.15/spec/dummy/config/environments/production.rb +50 -0
  33. data/cocoon-1.2.15/spec/dummy/config/environments/test.rb +33 -0
  34. data/cocoon-1.2.15/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  35. data/cocoon-1.2.15/spec/dummy/config/initializers/inflections.rb +10 -0
  36. data/cocoon-1.2.15/spec/dummy/config/initializers/mime_types.rb +5 -0
  37. data/cocoon-1.2.15/spec/dummy/config/initializers/rails_version_helper.rb +6 -0
  38. data/cocoon-1.2.15/spec/dummy/config/initializers/secret_token.rb +8 -0
  39. data/cocoon-1.2.15/spec/dummy/config/initializers/session_store.rb +8 -0
  40. data/cocoon-1.2.15/spec/dummy/config/locales/en.yml +5 -0
  41. data/cocoon-1.2.15/spec/dummy/config/routes.rb +58 -0
  42. data/cocoon-1.2.15/spec/dummy/config.ru +4 -0
  43. data/cocoon-1.2.15/spec/dummy/db/migrate/20110306212208_create_posts.rb +14 -0
  44. data/cocoon-1.2.15/spec/dummy/db/migrate/20110306212250_create_comments.rb +15 -0
  45. data/cocoon-1.2.15/spec/dummy/db/migrate/20110420222224_create_people.rb +15 -0
  46. data/cocoon-1.2.15/spec/dummy/db/schema.rb +39 -0
  47. data/cocoon-1.2.15/spec/dummy/public/404.html +26 -0
  48. data/cocoon-1.2.15/spec/dummy/public/422.html +26 -0
  49. data/cocoon-1.2.15/spec/dummy/public/500.html +26 -0
  50. data/cocoon-1.2.15/spec/dummy/public/favicon.ico +0 -0
  51. data/cocoon-1.2.15/spec/dummy/public/javascripts/application.js +2 -0
  52. data/cocoon-1.2.15/spec/dummy/public/javascripts/controls.js +965 -0
  53. data/cocoon-1.2.15/spec/dummy/public/javascripts/dragdrop.js +974 -0
  54. data/cocoon-1.2.15/spec/dummy/public/javascripts/effects.js +1123 -0
  55. data/cocoon-1.2.15/spec/dummy/public/javascripts/prototype.js +6001 -0
  56. data/cocoon-1.2.15/spec/dummy/public/javascripts/rails.js +175 -0
  57. data/cocoon-1.2.15/spec/dummy/script/rails +6 -0
  58. data/cocoon-1.2.15/spec/generators/install_generator_spec.rb +48 -0
  59. data/cocoon-1.2.15/spec/integration/navigation_spec.rb +9 -0
  60. data/cocoon-1.2.15/spec/spec_helper.rb +40 -0
  61. data/cocoon-1.2.15/spec/support/i18n.rb +3 -0
  62. data/cocoon-1.2.15/spec/support/rails_version_helper.rb +3 -0
  63. data/cocoon-1.2.15/spec/support/shared_examples.rb +73 -0
  64. data/giga-safe-tool.gemspec +11 -0
  65. metadata +103 -0
@@ -0,0 +1,146 @@
1
+ (function($) {
2
+
3
+ var cocoon_element_counter = 0;
4
+
5
+ var create_new_id = function() {
6
+ return (new Date().getTime() + cocoon_element_counter++);
7
+ }
8
+
9
+ var newcontent_braced = function(id) {
10
+ return '[' + id + ']$1';
11
+ }
12
+
13
+ var newcontent_underscord = function(id) {
14
+ return '_' + id + '_$1';
15
+ }
16
+
17
+ var getInsertionNodeElem = function(insertionNode, insertionTraversal, $this){
18
+
19
+ if (!insertionNode){
20
+ return $this.parent();
21
+ }
22
+
23
+ if (typeof insertionNode == 'function'){
24
+ if(insertionTraversal){
25
+ console.warn('association-insertion-traversal is ignored, because association-insertion-node is given as a function.')
26
+ }
27
+ return insertionNode($this);
28
+ }
29
+
30
+ if(typeof insertionNode == 'string'){
31
+ if (insertionTraversal){
32
+ return $this[insertionTraversal](insertionNode);
33
+ }else{
34
+ return insertionNode == "this" ? $this : $(insertionNode);
35
+ }
36
+ }
37
+
38
+ }
39
+
40
+ $(document).on('click', '.add_fields', function(e) {
41
+ e.preventDefault();
42
+ e.stopPropagation();
43
+
44
+ var $this = $(this),
45
+ assoc = $this.data('association'),
46
+ assocs = $this.data('associations'),
47
+ content = $this.data('association-insertion-template'),
48
+ insertionMethod = $this.data('association-insertion-method') || $this.data('association-insertion-position') || 'before',
49
+ insertionNode = $this.data('association-insertion-node'),
50
+ insertionTraversal = $this.data('association-insertion-traversal'),
51
+ count = parseInt($this.data('count'), 10),
52
+ regexp_braced = new RegExp('\\[new_' + assoc + '\\](.*?\\s)', 'g'),
53
+ regexp_underscord = new RegExp('_new_' + assoc + '_(\\w*)', 'g'),
54
+ new_id = create_new_id(),
55
+ new_content = content.replace(regexp_braced, newcontent_braced(new_id)),
56
+ new_contents = [],
57
+ originalEvent = e;
58
+
59
+
60
+ if (new_content == content) {
61
+ regexp_braced = new RegExp('\\[new_' + assocs + '\\](.*?\\s)', 'g');
62
+ regexp_underscord = new RegExp('_new_' + assocs + '_(\\w*)', 'g');
63
+ new_content = content.replace(regexp_braced, newcontent_braced(new_id));
64
+ }
65
+
66
+ new_content = new_content.replace(regexp_underscord, newcontent_underscord(new_id));
67
+ new_contents = [new_content];
68
+
69
+ count = (isNaN(count) ? 1 : Math.max(count, 1));
70
+ count -= 1;
71
+
72
+ while (count) {
73
+ new_id = create_new_id();
74
+ new_content = content.replace(regexp_braced, newcontent_braced(new_id));
75
+ new_content = new_content.replace(regexp_underscord, newcontent_underscord(new_id));
76
+ new_contents.push(new_content);
77
+
78
+ count -= 1;
79
+ }
80
+
81
+ var insertionNodeElem = getInsertionNodeElem(insertionNode, insertionTraversal, $this)
82
+
83
+ if( !insertionNodeElem || (insertionNodeElem.length == 0) ){
84
+ console.warn("Couldn't find the element to insert the template. Make sure your `data-association-insertion-*` on `link_to_add_association` is correct.")
85
+ }
86
+
87
+ $.each(new_contents, function(i, node) {
88
+ var contentNode = $(node);
89
+
90
+ var before_insert = jQuery.Event('cocoon:before-insert');
91
+ insertionNodeElem.trigger(before_insert, [contentNode, originalEvent]);
92
+
93
+ if (!before_insert.isDefaultPrevented()) {
94
+ // allow any of the jquery dom manipulation methods (after, before, append, prepend, etc)
95
+ // to be called on the node. allows the insertion node to be the parent of the inserted
96
+ // code and doesn't force it to be a sibling like after/before does. default: 'before'
97
+ var addedContent = insertionNodeElem[insertionMethod](contentNode);
98
+
99
+ insertionNodeElem.trigger('cocoon:after-insert', [contentNode,
100
+ originalEvent]);
101
+ }
102
+ });
103
+ });
104
+
105
+ $(document).on('click', '.remove_fields.dynamic, .remove_fields.existing', function(e) {
106
+ var $this = $(this),
107
+ wrapper_class = $this.data('wrapper-class') || 'nested-fields',
108
+ node_to_delete = $this.closest('.' + wrapper_class),
109
+ trigger_node = node_to_delete.parent(),
110
+ originalEvent = e;
111
+
112
+ e.preventDefault();
113
+ e.stopPropagation();
114
+
115
+ var before_remove = jQuery.Event('cocoon:before-remove');
116
+ trigger_node.trigger(before_remove, [node_to_delete, originalEvent]);
117
+
118
+ if (!before_remove.isDefaultPrevented()) {
119
+ var timeout = trigger_node.data('remove-timeout') || 0;
120
+
121
+ setTimeout(function() {
122
+ if ($this.hasClass('dynamic')) {
123
+ node_to_delete.detach();
124
+ } else {
125
+ $this.prev("input[type=hidden]").val("1");
126
+ node_to_delete.hide();
127
+ }
128
+ trigger_node.trigger('cocoon:after-remove', [node_to_delete,
129
+ originalEvent]);
130
+ }, timeout);
131
+ }
132
+ });
133
+
134
+
135
+ $(document).on("ready page:load turbolinks:load", function() {
136
+ $('.remove_fields.existing.destroyed').each(function(i, obj) {
137
+ var $this = $(this),
138
+ wrapper_class = $this.data('wrapper-class') || 'nested-fields';
139
+
140
+ $this.closest('.' + wrapper_class).hide();
141
+ });
142
+ });
143
+
144
+ })(jQuery);
145
+
146
+
@@ -0,0 +1,149 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+ # stub: cocoon 1.2.15 ruby lib
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = "cocoon".freeze
9
+ s.version = "1.2.15"
10
+
11
+ s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
12
+ s.require_paths = ["lib".freeze]
13
+ s.authors = ["Nathan Van der Auwera".freeze]
14
+ s.date = "2020-09-08"
15
+ s.description = "Unobtrusive nested forms handling, using jQuery. Use this and discover cocoon-heaven.".freeze
16
+ s.email = "nathan@dixis.com".freeze
17
+ s.extra_rdoc_files = [
18
+ "LICENSE",
19
+ "README.markdown"
20
+ ]
21
+ s.files = [
22
+ ".travis.yml",
23
+ "Gemfile",
24
+ "History.md",
25
+ "LICENSE",
26
+ "README.markdown",
27
+ "Rakefile",
28
+ "VERSION",
29
+ "app/assets/javascripts/cocoon.js",
30
+ "cocoon.gemspec",
31
+ "gemfiles/Gemfile.default",
32
+ "gemfiles/Gemfile.rails-3.2.13",
33
+ "gemfiles/Gemfile.rails-4-r2",
34
+ "lib/cocoon.rb",
35
+ "lib/cocoon/view_helpers.rb",
36
+ "lib/generators/cocoon/install/install_generator.rb",
37
+ "npm/README.md",
38
+ "npm/package.json.erb",
39
+ "spec/cocoon_spec.rb",
40
+ "spec/dummy/Rakefile",
41
+ "spec/dummy/app/controllers/application_controller.rb",
42
+ "spec/dummy/app/decorators/comment_decorator.rb",
43
+ "spec/dummy/app/helpers/application_helper.rb",
44
+ "spec/dummy/app/models/comment.rb",
45
+ "spec/dummy/app/models/person.rb",
46
+ "spec/dummy/app/models/post.rb",
47
+ "spec/dummy/app/views/layouts/application.html.erb",
48
+ "spec/dummy/config.ru",
49
+ "spec/dummy/config/application.rb",
50
+ "spec/dummy/config/boot.rb",
51
+ "spec/dummy/config/database.yml",
52
+ "spec/dummy/config/environment.rb",
53
+ "spec/dummy/config/environments/development.rb",
54
+ "spec/dummy/config/environments/production.rb",
55
+ "spec/dummy/config/environments/test.rb",
56
+ "spec/dummy/config/initializers/backtrace_silencers.rb",
57
+ "spec/dummy/config/initializers/inflections.rb",
58
+ "spec/dummy/config/initializers/mime_types.rb",
59
+ "spec/dummy/config/initializers/rails_version_helper.rb",
60
+ "spec/dummy/config/initializers/secret_token.rb",
61
+ "spec/dummy/config/initializers/session_store.rb",
62
+ "spec/dummy/config/locales/en.yml",
63
+ "spec/dummy/config/routes.rb",
64
+ "spec/dummy/db/migrate/20110306212208_create_posts.rb",
65
+ "spec/dummy/db/migrate/20110306212250_create_comments.rb",
66
+ "spec/dummy/db/migrate/20110420222224_create_people.rb",
67
+ "spec/dummy/db/schema.rb",
68
+ "spec/dummy/public/404.html",
69
+ "spec/dummy/public/422.html",
70
+ "spec/dummy/public/500.html",
71
+ "spec/dummy/public/favicon.ico",
72
+ "spec/dummy/public/javascripts/application.js",
73
+ "spec/dummy/public/javascripts/controls.js",
74
+ "spec/dummy/public/javascripts/dragdrop.js",
75
+ "spec/dummy/public/javascripts/effects.js",
76
+ "spec/dummy/public/javascripts/prototype.js",
77
+ "spec/dummy/public/javascripts/rails.js",
78
+ "spec/dummy/public/stylesheets/.gitkeep",
79
+ "spec/dummy/script/rails",
80
+ "spec/generators/install_generator_spec.rb",
81
+ "spec/integration/navigation_spec.rb",
82
+ "spec/spec_helper.rb",
83
+ "spec/support/i18n.rb",
84
+ "spec/support/rails_version_helper.rb",
85
+ "spec/support/shared_examples.rb"
86
+ ]
87
+ s.homepage = "http://github.com/nathanvda/cocoon".freeze
88
+ s.licenses = ["MIT".freeze]
89
+ s.rubygems_version = "3.0.8".freeze
90
+ s.summary = "gem that enables easier nested forms with standard forms, formtastic and simple-form".freeze
91
+
92
+ if s.respond_to? :specification_version then
93
+ s.specification_version = 4
94
+
95
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
96
+ s.add_development_dependency(%q<rails>.freeze, ["~> 4.2"])
97
+ s.add_development_dependency(%q<sqlite3>.freeze, ["= 1.3.13"])
98
+ s.add_development_dependency(%q<json_pure>.freeze, [">= 0"])
99
+ s.add_development_dependency(%q<jeweler>.freeze, [">= 0"])
100
+ s.add_development_dependency(%q<rspec-rails>.freeze, ["~> 3.0.0"])
101
+ s.add_development_dependency(%q<rspec>.freeze, ["~> 3.0.0"])
102
+ s.add_development_dependency(%q<actionpack>.freeze, [">= 4.0.0"])
103
+ s.add_development_dependency(%q<simplecov>.freeze, [">= 0"])
104
+ s.add_development_dependency(%q<rake>.freeze, ["~> 10.1"])
105
+ s.add_development_dependency(%q<nokogiri>.freeze, [">= 0"])
106
+ s.add_development_dependency(%q<generator_spec>.freeze, [">= 0"])
107
+ s.add_development_dependency(%q<rubysl>.freeze, [">= 0"])
108
+ s.add_development_dependency(%q<rubysl-test-unit>.freeze, [">= 0"])
109
+ s.add_development_dependency(%q<psych>.freeze, ["~> 2.2"])
110
+ s.add_development_dependency(%q<racc>.freeze, [">= 0"])
111
+ s.add_development_dependency(%q<rubinius-developer_tools>.freeze, [">= 0"])
112
+ else
113
+ s.add_dependency(%q<rails>.freeze, ["~> 4.2"])
114
+ s.add_dependency(%q<sqlite3>.freeze, ["= 1.3.13"])
115
+ s.add_dependency(%q<json_pure>.freeze, [">= 0"])
116
+ s.add_dependency(%q<jeweler>.freeze, [">= 0"])
117
+ s.add_dependency(%q<rspec-rails>.freeze, ["~> 3.0.0"])
118
+ s.add_dependency(%q<rspec>.freeze, ["~> 3.0.0"])
119
+ s.add_dependency(%q<actionpack>.freeze, [">= 4.0.0"])
120
+ s.add_dependency(%q<simplecov>.freeze, [">= 0"])
121
+ s.add_dependency(%q<rake>.freeze, ["~> 10.1"])
122
+ s.add_dependency(%q<nokogiri>.freeze, [">= 0"])
123
+ s.add_dependency(%q<generator_spec>.freeze, [">= 0"])
124
+ s.add_dependency(%q<rubysl>.freeze, [">= 0"])
125
+ s.add_dependency(%q<rubysl-test-unit>.freeze, [">= 0"])
126
+ s.add_dependency(%q<psych>.freeze, ["~> 2.2"])
127
+ s.add_dependency(%q<racc>.freeze, [">= 0"])
128
+ s.add_dependency(%q<rubinius-developer_tools>.freeze, [">= 0"])
129
+ end
130
+ else
131
+ s.add_dependency(%q<rails>.freeze, ["~> 4.2"])
132
+ s.add_dependency(%q<sqlite3>.freeze, ["= 1.3.13"])
133
+ s.add_dependency(%q<json_pure>.freeze, [">= 0"])
134
+ s.add_dependency(%q<jeweler>.freeze, [">= 0"])
135
+ s.add_dependency(%q<rspec-rails>.freeze, ["~> 3.0.0"])
136
+ s.add_dependency(%q<rspec>.freeze, ["~> 3.0.0"])
137
+ s.add_dependency(%q<actionpack>.freeze, [">= 4.0.0"])
138
+ s.add_dependency(%q<simplecov>.freeze, [">= 0"])
139
+ s.add_dependency(%q<rake>.freeze, ["~> 10.1"])
140
+ s.add_dependency(%q<nokogiri>.freeze, [">= 0"])
141
+ s.add_dependency(%q<generator_spec>.freeze, [">= 0"])
142
+ s.add_dependency(%q<rubysl>.freeze, [">= 0"])
143
+ s.add_dependency(%q<rubysl-test-unit>.freeze, [">= 0"])
144
+ s.add_dependency(%q<psych>.freeze, ["~> 2.2"])
145
+ s.add_dependency(%q<racc>.freeze, [">= 0"])
146
+ s.add_dependency(%q<rubinius-developer_tools>.freeze, [">= 0"])
147
+ end
148
+ end
149
+
@@ -0,0 +1,31 @@
1
+ source "http://rubygems.org"
2
+
3
+
4
+ group :development, :test do
5
+ gem "rails", "~> 4.2"
6
+ gem "sqlite3", '~> 1.3.13'
7
+ gem "json_pure"
8
+ gem "jeweler"
9
+ gem "rspec-rails", "~> 3.0.0"
10
+ gem "rspec", "~> 3.0.0"
11
+ gem "simplecov", :require => false
12
+ gem "rake", "~> 10.1"
13
+
14
+ gem 'nokogiri'
15
+
16
+ gem "generator_spec"
17
+
18
+ platforms :rbx do
19
+ gem 'rubysl'
20
+ gem 'rubysl-test-unit'
21
+ gem 'psych'
22
+ gem 'racc'
23
+ gem 'rubinius-developer_tools'
24
+ end
25
+
26
+ end
27
+
28
+
29
+ # To use debugger (ruby-debug for Ruby 1.8.7+, ruby-debug19 for Ruby 1.9.2+)
30
+ # gem 'ruby-debug'
31
+ # gem 'ruby-debug19'
@@ -0,0 +1,30 @@
1
+ source "http://rubygems.org"
2
+
3
+
4
+ group :development, :test do
5
+ gem "rails", "~> 3.2.0"
6
+ gem "sqlite3", "1.3.8"
7
+ gem "json_pure"
8
+ gem "jeweler", "~> 1.8"
9
+ gem "rspec-rails", "~> 3.0.0"
10
+ gem "rspec", "~> 3.0.0"
11
+ gem "actionpack", "~> 3.2.0"
12
+ gem "simplecov", :require => false
13
+ gem "rake", "~> 10.1"
14
+
15
+ gem 'nokogiri'
16
+
17
+ gem "generator_spec"
18
+ end
19
+
20
+ platforms :rbx do
21
+ gem 'rubysl'
22
+ gem 'rubysl-test-unit'
23
+ gem 'psych'
24
+ gem 'racc'
25
+ gem 'rubinius-developer_tools'
26
+ end
27
+
28
+ # To use debugger (ruby-debug for Ruby 1.8.7+, ruby-debug19 for Ruby 1.9.2+)
29
+ # gem 'ruby-debug'
30
+ # gem 'ruby-debug19'
@@ -0,0 +1,27 @@
1
+ source "http://rubygems.org"
2
+
3
+
4
+ group :development, :test do
5
+ gem "rails", "~> 4.0.2"
6
+ gem "sqlite3", "1.3.8"
7
+ gem "json_pure"
8
+ gem "jeweler", "~> 1.8"
9
+ gem "rspec-rails", "~> 3.0.0"
10
+ gem "rspec", "~> 3.0.0"
11
+ gem "actionpack", "~> 4.0.2"
12
+ gem "simplecov", :require => false
13
+ gem "rake", "~> 10.1"
14
+
15
+ gem 'nokogiri'
16
+
17
+ gem "generator_spec"
18
+
19
+ platforms :rbx do
20
+ gem 'rubysl'
21
+ gem 'rubysl-test-unit'
22
+ gem 'psych'
23
+ gem 'racc'
24
+ gem 'rubinius-developer_tools'
25
+ end
26
+
27
+ end
@@ -0,0 +1,160 @@
1
+ module Cocoon
2
+ module ViewHelpers
3
+
4
+
5
+ # this will show a link to remove the current association. This should be placed inside the partial.
6
+ # either you give
7
+ # - *name* : the text of the link
8
+ # - *f* : the form this link should be placed in
9
+ # - *html_options*: html options to be passed to link_to (see <tt>link_to</tt>)
10
+ #
11
+ # or you use the form without *name* with a *&block*
12
+ # - *f* : the form this link should be placed in
13
+ # - *html_options*: html options to be passed to link_to (see <tt>link_to</tt>)
14
+ # - *&block*: the output of the block will be show in the link, see <tt>link_to</tt>
15
+
16
+ def link_to_remove_association(*args, &block)
17
+ if block_given?
18
+ link_to_remove_association(capture(&block), *args)
19
+ elsif args.first.respond_to?(:object)
20
+ form = args.first
21
+ association = form.object.class.to_s.tableize
22
+ name = I18n.translate("cocoon.#{association}.remove", default: I18n.translate('cocoon.defaults.remove'))
23
+
24
+ link_to_remove_association(name, *args)
25
+ else
26
+ name, f, html_options = *args
27
+ html_options ||= {}
28
+
29
+ is_dynamic = f.object.new_record?
30
+
31
+ classes = []
32
+ classes << "remove_fields"
33
+ classes << (is_dynamic ? 'dynamic' : 'existing')
34
+ classes << 'destroyed' if f.object.marked_for_destruction?
35
+ html_options[:class] = [html_options[:class], classes.join(' ')].compact.join(' ')
36
+
37
+ wrapper_class = html_options.delete(:wrapper_class)
38
+ html_options[:'data-wrapper-class'] = wrapper_class if wrapper_class.present?
39
+
40
+ f.hidden_field(:_destroy, value: f.object._destroy) + link_to(name, '#', html_options)
41
+ end
42
+ end
43
+
44
+ # :nodoc:
45
+ def render_association(association, f, new_object, form_name, received_render_options={}, custom_partial=nil)
46
+ partial = get_partial_path(custom_partial, association)
47
+ render_options = received_render_options.dup
48
+ locals = render_options.delete(:locals) || {}
49
+ ancestors = f.class.ancestors.map{|c| c.to_s}
50
+ method_name = ancestors.include?('SimpleForm::FormBuilder') ? :simple_fields_for : (ancestors.include?('Formtastic::FormBuilder') ? :semantic_fields_for : :fields_for)
51
+ f.send(method_name, association, new_object, {:child_index => "new_#{association}"}.merge(render_options)) do |builder|
52
+ partial_options = {form_name.to_sym => builder, :dynamic => true}.merge(locals)
53
+ render(partial, partial_options)
54
+ end
55
+ end
56
+
57
+ # shows a link that will allow to dynamically add a new associated object.
58
+ #
59
+ # - *name* : the text to show in the link
60
+ # - *f* : the form this should come in (the formtastic form)
61
+ # - *association* : the associated objects, e.g. :tasks, this should be the name of the <tt>has_many</tt> relation.
62
+ # - *html_options*: html options to be passed to <tt>link_to</tt> (see <tt>link_to</tt>)
63
+ # - *:render_options* : options passed to `simple_fields_for, semantic_fields_for or fields_for`
64
+ # - *:locals* : the locals hash in the :render_options is handed to the partial
65
+ # - *:partial* : explicitly override the default partial name
66
+ # - *:wrap_object* : a proc that will allow to wrap your object, especially suited when using
67
+ # decorators, or if you want special initialisation
68
+ # - *:form_name* : the parameter for the form in the nested form partial. Default `f`.
69
+ # - *:count* : Count of how many objects will be added on a single click. Default `1`.
70
+ # - *&block*: see <tt>link_to</tt>
71
+
72
+ def link_to_add_association(*args, &block)
73
+ if block_given?
74
+ link_to_add_association(capture(&block), *args)
75
+ elsif args.first.respond_to?(:object)
76
+ association = args.second
77
+ name = I18n.translate("cocoon.#{association}.add", default: I18n.translate('cocoon.defaults.add'))
78
+
79
+ link_to_add_association(name, *args)
80
+ else
81
+ name, f, association, html_options = *args
82
+ html_options ||= {}
83
+
84
+ render_options = html_options.delete(:render_options)
85
+ render_options ||= {}
86
+ override_partial = html_options.delete(:partial)
87
+ wrap_object = html_options.delete(:wrap_object)
88
+ force_non_association_create = html_options.delete(:force_non_association_create) || false
89
+ form_parameter_name = html_options.delete(:form_name) || 'f'
90
+ count = html_options.delete(:count).to_i
91
+
92
+ html_options[:class] = [html_options[:class], "add_fields"].compact.join(' ')
93
+ html_options[:'data-association'] = association.to_s.singularize
94
+ html_options[:'data-associations'] = association.to_s.pluralize
95
+
96
+ new_object = create_object(f, association, force_non_association_create)
97
+ new_object = wrap_object.call(new_object) if wrap_object.respond_to?(:call)
98
+
99
+ html_options[:'data-association-insertion-template'] = CGI.escapeHTML(render_association(association, f, new_object, form_parameter_name, render_options, override_partial).to_str).html_safe
100
+
101
+ html_options[:'data-count'] = count if count > 0
102
+
103
+ link_to(name, '#', html_options)
104
+ end
105
+ end
106
+
107
+ # creates new association object with its conditions, like
108
+ # `` has_many :admin_comments, class_name: "Comment", conditions: { author: "Admin" }
109
+ # will create new Comment with author "Admin"
110
+
111
+ def create_object(f, association, force_non_association_create=false)
112
+ assoc = f.object.class.reflect_on_association(association)
113
+
114
+ assoc ? create_object_on_association(f, association, assoc, force_non_association_create) : create_object_on_non_association(f, association)
115
+ end
116
+
117
+ def get_partial_path(partial, association)
118
+ partial ? partial : association.to_s.singularize + "_fields"
119
+ end
120
+
121
+ private
122
+
123
+ def create_object_on_non_association(f, association)
124
+ builder_method = %W{build_#{association} build_#{association.to_s.singularize}}.select { |m| f.object.respond_to?(m) }.first
125
+ return f.object.send(builder_method) if builder_method
126
+ raise "Association #{association} doesn't exist on #{f.object.class}"
127
+ end
128
+
129
+ def create_object_on_association(f, association, instance, force_non_association_create)
130
+ if instance.class.name.starts_with?('Mongoid::') || force_non_association_create
131
+ create_object_with_conditions(instance)
132
+ else
133
+ assoc_obj = nil
134
+
135
+ # assume ActiveRecord or compatible
136
+ if instance.collection?
137
+ assoc_obj = f.object.send(association).build
138
+ f.object.send(association).delete(assoc_obj)
139
+ else
140
+ assoc_obj = f.object.send("build_#{association}")
141
+ f.object.send(association).delete
142
+ end
143
+
144
+ assoc_obj = assoc_obj.dup if assoc_obj.frozen?
145
+
146
+ assoc_obj
147
+ end
148
+ end
149
+
150
+ def create_object_with_conditions(instance)
151
+ # in rails 4, an association is defined with a proc
152
+ # and I did not find how to extract the conditions from a scope
153
+ # except building from the scope, but then why not just build from the
154
+ # association???
155
+ conditions = instance.respond_to?(:conditions) ? instance.conditions.flatten : []
156
+ instance.klass.new(*conditions)
157
+ end
158
+
159
+ end
160
+ end
@@ -0,0 +1,20 @@
1
+ require 'cocoon/view_helpers'
2
+
3
+ module Cocoon
4
+ class Engine < ::Rails::Engine
5
+
6
+ config.before_initialize do
7
+ if config.action_view.javascript_expansions
8
+ config.action_view.javascript_expansions[:cocoon] = %w(cocoon)
9
+ end
10
+ end
11
+
12
+ # configure our plugin on boot
13
+ initializer "cocoon.initialize" do |app|
14
+ ActiveSupport.on_load :action_view do
15
+ ActionView::Base.send :include, Cocoon::ViewHelpers
16
+ end
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,17 @@
1
+ module Cocoon
2
+ module Generators
3
+ class InstallGenerator < ::Rails::Generators::Base
4
+ source_root File.expand_path('../templates', __FILE__)
5
+
6
+ desc "This generator installs the javascript needed for cocoon"
7
+ def copy_the_javascript
8
+ if ::Rails.version[0..2].to_f >= 3.1
9
+ puts "Installing is no longer required since Rails 3.1"
10
+ else
11
+ copy_file "../../../../../app/assets/javascripts/cocoon.js", "public/javascripts/cocoon.js"
12
+ end
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,8 @@
1
+ # cocoon
2
+
3
+ Cocoon is the companion javascript code for the
4
+ rails gem [cocoon](https://github.com/nathanvda/cocoon)
5
+ which offers "dynamic" nested forms.
6
+
7
+ This code depends on jQuery.
8
+
@@ -0,0 +1,21 @@
1
+ {
2
+ "name": "@nathanvda/cocoon",
3
+ "version": "<%= spec.version %>",
4
+ "license": "MIT",
5
+
6
+ "description": "Companion package for the cocoon Ruby gem.",
7
+ "homepage": "https://github.com/nathanvda/cocoon",
8
+ "repository": "https://github.com/nathanvda/cocoon",
9
+ "bugs": {
10
+ "url": "https://github.com/nathanvda/cocoon/issues"
11
+ },
12
+
13
+ "author": "nathan@dixis.com",
14
+
15
+ "main": "cocoon.js",
16
+ "files": ["cocoon.js", "README", "LICENSE", "package.json"],
17
+
18
+ "dependencies": {
19
+ "jquery": "^3.3.1"
20
+ }
21
+ }