dry_forms 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -13,9 +13,11 @@ tmtags
13
13
  ## VIM
14
14
  *.swp
15
15
 
16
+
16
17
  ## PROJECT::GENERAL
17
18
  coverage
18
19
  rdoc
19
- # pkg
20
+ pkg
21
+ dry_forms.gemspec
20
22
 
21
23
  ## PROJECT::SPECIFIC
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.1.0
@@ -0,0 +1,8 @@
1
+ class DryFormsJsGenerator < Rails::Generator::Base
2
+ def manifest
3
+ record do |m|
4
+ m.directory "public/javascripts"
5
+ m.file "jquery.dry_forms.associations.js", "public/javascripts/jquery.dry_forms.associations.js"
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,48 @@
1
+ // github.com/maca/dry_forms
2
+
3
+ (function(a){
4
+ dryForms = {
5
+ associations : {
6
+ removeAssociation : function(){
7
+ var fieldset = $(this).closest('fieldset');
8
+ fieldset.slideUp('slow').find('input.destroy').val('1');
9
+ return false;
10
+ },
11
+
12
+ addAssociation : function(){
13
+ var link = $(this);
14
+ var association = link.attr('data-association');
15
+ var new_id = new Date().getTime();
16
+ var regexp = new RegExp('new_' + association, 'g');
17
+ var fields = $(eval('fields_for_' + association).replace(regexp, association + "_" + new_id)).hide();
18
+
19
+ fields.find('a.remove').click(dryForms.associations.removeAssociation);
20
+ this.addCallback(fields);
21
+
22
+ link.before(fields);
23
+ fields.slideDown('slow');
24
+ return false;
25
+ }
26
+ }
27
+ };
28
+
29
+ $.fn.createAssociation = function(opts){
30
+ var defaults = {
31
+ addCallback : function(){},
32
+ removeCallback : function(){}
33
+ };
34
+ $.extend(defaults, opts || {});
35
+ $('fieldset[data-association] a.remove').click(dryForms.associations.removeAssociation);
36
+
37
+ return this.each(function(){
38
+ $(this).click(dryForms.associations.addAssociation);
39
+ $.extend(this, defaults);
40
+ });
41
+ };
42
+ })(jQuery);
43
+
44
+
45
+ // This may go in application.js
46
+ jQuery(function(){
47
+ $('.add_fields').createAssociation();
48
+ });
@@ -30,7 +30,7 @@ module DryForms
30
30
 
31
31
  association_class = @object.class.reflect_on_association(association.to_sym).klass
32
32
  singular_name = association.singularize
33
- fields = association_fields association, &block
33
+ fields = @object.send(association).map{ |obj| association_fields(association, obj, &block) }.join
34
34
  new_object = @object.send(association).build options.delete(:default_attributes) || {}
35
35
  js_fields = association_fields association, new_object, :child_index => "new_#{singular_name}", &block
36
36
 
@@ -51,16 +51,14 @@ module DryForms
51
51
  end
52
52
 
53
53
  private
54
- def association_fields association, object = nil, opts = {}, &block
54
+ def association_fields association, object, opts = {}, &block
55
55
  opts.merge! :html => {:class => "associated", :'data-association' => association.singularize}
56
56
 
57
57
  fields = @template.capture do
58
58
  custom_fields_for association.to_sym, object, opts do |fields|
59
- <<-HTML
60
- #{fields.hidden_field :_destroy}
61
- #{yield fields}
62
- <a class="remove" href="#">#{I18n.t "dry_forms.remove"}</a>
63
- HTML
59
+ @template.concat fields.hidden_field :_destroy, :class => 'destroy'
60
+ yield fields
61
+ @template.concat %{<a class="remove" href="#">#{I18n.t "dry_forms.remove"}</a>}
64
62
  end
65
63
  end
66
64
  end
data/test/helper.rb CHANGED
@@ -24,6 +24,7 @@ class ActionView::TestCase::TestController
24
24
  @url_for_options = options
25
25
  "/do"
26
26
  end
27
+
27
28
  end
28
29
 
29
30
  class ActionView::Base
@@ -23,8 +23,6 @@ ActiveRecord::Schema.define :version => 1 do
23
23
  end
24
24
 
25
25
 
26
-
27
-
28
26
  class Post < ActiveRecord::Base
29
27
  belongs_to :author
30
28
  accepts_nested_attributes_for :author
@@ -4,9 +4,9 @@ class FormHelperTest < ActionView::TestCase
4
4
  tests ActionView::Helpers::FormHelper
5
5
 
6
6
  def setup
7
- @post = Post.new :title => "Hello World", :body => "Has to work"
8
- @author = Author.new :name => "Macario"
9
- @comment = Comment.new :title => "Hello World", :body => "Looking good"
7
+ @post = Post.create :title => "Hello World", :body => "Has to work"
8
+ @author = Author.create :name => "Macario"
9
+ @comment = Comment.create :title => "Hello World", :body => "Looking good"
10
10
  end
11
11
 
12
12
  context 'helper method rendering' do
@@ -87,7 +87,7 @@ class FormHelperTest < ActionView::TestCase
87
87
 
88
88
  js_fields = <<-HTML
89
89
  <fieldset class="associated" data-association="post">
90
- <input id="author_posts_attributes_new_post__destroy" name="author[posts_attributes][new_post][_destroy]" type="hidden" />
90
+ <input class="destroy" id="author_posts_attributes_new_post__destroy" name="author[posts_attributes][new_post][_destroy]" type="hidden" />
91
91
  <input id="author_posts_attributes_new_post_title" name="author[posts_attributes][new_post][title]" size="30" type="text" />
92
92
  <a class="remove" href="#">translation missing: en, dry_forms, remove</a>
93
93
  </fieldset>
@@ -110,8 +110,9 @@ class FormHelperTest < ActionView::TestCase
110
110
  assert_dom_equal html, render(:inline => erb)
111
111
  end
112
112
 
113
- should 'render field for associations' do
114
- @author.posts << @post
113
+ should 'render field for existing associations' do
114
+ @author.posts.create! :title => "Hello World", :body => "Looking good"
115
+ @author.posts.create! :title => "Hello World 2", :body => "Looking good"
115
116
 
116
117
  erb = <<-ERB
117
118
  <% form_for :author, @author do |form| %>
@@ -124,7 +125,7 @@ class FormHelperTest < ActionView::TestCase
124
125
 
125
126
  js_fields = <<-HTML
126
127
  <fieldset class="associated" data-association="post">
127
- <input id="author_posts_attributes_new_post__destroy" name="author[posts_attributes][new_post][_destroy]" type="hidden" />
128
+ <input class="destroy" id="author_posts_attributes_new_post__destroy" name="author[posts_attributes][new_post][_destroy]" type="hidden" />
128
129
  <input id="author_posts_attributes_new_post_title" name="author[posts_attributes][new_post][title]" size="30" type="text" />
129
130
  <a class="remove" href="#">translation missing: en, dry_forms, remove</a>
130
131
  </fieldset>
@@ -135,9 +136,16 @@ class FormHelperTest < ActionView::TestCase
135
136
  <h2>Posts</h2>
136
137
  <div id="posts">
137
138
  <fieldset class="associated" data-association="post">
138
- <input name="author[posts_attributes][0][_destroy]" id="author_posts_attributes_0__destroy" type="hidden" />
139
- <input name="author[posts_attributes][0][title]" size="30" id="author_posts_attributes_0_title" value="Hello World" type="text" />
139
+ <input class="destroy" name="author[posts_attributes][0][_destroy]" id="author_posts_attributes_0__destroy" type="hidden" />
140
+ <input name="author[posts_attributes][0][title]" size="30" id="author_posts_attributes_0_title" type="text" value="Hello World" />
140
141
  <a href="#" class="remove">translation missing: en, dry_forms, remove</a>
142
+ <input name="author[posts_attributes][0][id]" id="author_posts_attributes_0_id" type="hidden" value="5" />
143
+ </fieldset>
144
+ <fieldset class="associated" data-association="post">
145
+ <input class="destroy" name="author[posts_attributes][1][_destroy]" id="author_posts_attributes_1__destroy" type="hidden" />
146
+ <input name="author[posts_attributes][1][title]" size="30" id="author_posts_attributes_1_title" type="text" value="Hello World 2" />
147
+ <a href="#" class="remove">translation missing: en, dry_forms, remove</a>
148
+ <input name="author[posts_attributes][1][id]" id="author_posts_attributes_1_id" type="hidden" value="6" />
141
149
  </fieldset>
142
150
  <script type="text/javascript">
143
151
  //<![CDATA[
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dry_forms
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
5
4
  prerelease: false
6
5
  segments:
7
6
  - 0
8
- - 0
9
7
  - 1
10
- version: 0.0.1
8
+ - 0
9
+ version: 0.1.0
11
10
  platform: ruby
12
11
  authors:
13
12
  - Macario Ortega
@@ -15,18 +14,16 @@ autorequire:
15
14
  bindir: bin
16
15
  cert_chain: []
17
16
 
18
- date: 2010-08-06 00:00:00 -05:00
17
+ date: 2010-08-11 00:00:00 -05:00
19
18
  default_executable:
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
22
21
  name: thoughtbot-shoulda
23
22
  prerelease: false
24
23
  requirement: &id001 !ruby/object:Gem::Requirement
25
- none: false
26
24
  requirements:
27
25
  - - ">="
28
26
  - !ruby/object:Gem::Version
29
- hash: 3
30
27
  segments:
31
28
  - 0
32
29
  version: "0"
@@ -48,11 +45,11 @@ files:
48
45
  - README.rdoc
49
46
  - Rakefile
50
47
  - VERSION
51
- - dry_forms.gemspec
48
+ - generators/dry_forms_js/dry_forms_js_generator.rb
49
+ - generators/dry_forms_js/templates/jquery.dry_forms.associations.js
52
50
  - lib/dry_forms.rb
53
51
  - lib/dry_forms/builder_additions.rb
54
52
  - lib/dry_forms/form_helper_additions.rb
55
- - pkg/dry_forms-0.0.1.gem
56
53
  - test/helper.rb
57
54
  - test/support/models.rb
58
55
  - test/test_dry_forms.rb
@@ -66,27 +63,23 @@ rdoc_options:
66
63
  require_paths:
67
64
  - lib
68
65
  required_ruby_version: !ruby/object:Gem::Requirement
69
- none: false
70
66
  requirements:
71
67
  - - ">="
72
68
  - !ruby/object:Gem::Version
73
- hash: 3
74
69
  segments:
75
70
  - 0
76
71
  version: "0"
77
72
  required_rubygems_version: !ruby/object:Gem::Requirement
78
- none: false
79
73
  requirements:
80
74
  - - ">="
81
75
  - !ruby/object:Gem::Version
82
- hash: 3
83
76
  segments:
84
77
  - 0
85
78
  version: "0"
86
79
  requirements: []
87
80
 
88
81
  rubyforge_project:
89
- rubygems_version: 1.3.7
82
+ rubygems_version: 1.3.6
90
83
  signing_key:
91
84
  specification_version: 3
92
85
  summary: Form builder additions for drier and localized forms, and association fields adding and removing with jQuery sweetness
data/dry_forms.gemspec DELETED
@@ -1,59 +0,0 @@
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{dry_forms}
8
- s.version = "0.0.1"
9
-
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["Macario Ortega"]
12
- s.date = %q{2010-08-06}
13
- s.description = %q{Form builder additions for drier and localized forms, and association fields adding and removing with jQuery sweetness inspired by Ryan Bates' Railcast #197}
14
- s.email = %q{macarui@gmail.com}
15
- s.extra_rdoc_files = [
16
- "LICENSE",
17
- "README.rdoc"
18
- ]
19
- s.files = [
20
- ".document",
21
- ".gitignore",
22
- "LICENSE",
23
- "README.rdoc",
24
- "Rakefile",
25
- "VERSION",
26
- "dry_forms.gemspec",
27
- "lib/dry_forms.rb",
28
- "lib/dry_forms/builder_additions.rb",
29
- "lib/dry_forms/form_helper_additions.rb",
30
- "pkg/dry_forms-0.0.1.gem",
31
- "test/helper.rb",
32
- "test/support/models.rb",
33
- "test/test_dry_forms.rb"
34
- ]
35
- s.homepage = %q{http://github.com/maca/dry_forms}
36
- s.rdoc_options = ["--charset=UTF-8"]
37
- s.require_paths = ["lib"]
38
- s.rubygems_version = %q{1.3.7}
39
- s.summary = %q{Form builder additions for drier and localized forms, and association fields adding and removing with jQuery sweetness}
40
- s.test_files = [
41
- "test/helper.rb",
42
- "test/support/models.rb",
43
- "test/test_dry_forms.rb"
44
- ]
45
-
46
- if s.respond_to? :specification_version then
47
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
48
- s.specification_version = 3
49
-
50
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
51
- s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
52
- else
53
- s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
54
- end
55
- else
56
- s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
57
- end
58
- end
59
-
Binary file