dry_forms 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -10,7 +10,7 @@ begin
10
10
  gem.email = "macarui@gmail.com"
11
11
  gem.homepage = "http://github.com/maca/dry_forms"
12
12
  gem.authors = ["Macario Ortega"]
13
- gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
13
+ gem.add_development_dependency "shoulda", ">= 0"
14
14
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
15
  end
16
16
  Jeweler::GemcutterTasks.new
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.2.2
@@ -22,14 +22,15 @@ module DryForms
22
22
  @object.errors[attribute]
23
23
  end
24
24
 
25
- DryForms.field_markup label, field, [*errors].compact, html_opts
25
+ ActiveSupport::SafeBuffer.new DryForms.field_markup(label, field, [*errors].compact, html_opts)
26
26
  end
27
27
  end
28
28
 
29
29
  def fields_for_association association, *args, &block
30
30
  options = args.extract_options!
31
31
  association = association.to_s
32
- objects = args.first ? [*args.first] : @object.send(association)
32
+ objects = args.first ? [*args.first] : @object.send(association)
33
+ html = options.delete(:html)
33
34
 
34
35
  unless @object.respond_to? "#{association.pluralize}_attributes="
35
36
  raise NotImplementedError, "Please call `accepts_nested_attributes_for :#{association.pluralize}` in your `#{@object.class}` Model"
@@ -39,44 +40,42 @@ module DryForms
39
40
 
40
41
  association_class = @object.class.reflect_on_association(association.to_sym).klass
41
42
  singular_name = association.singularize
42
- fields = objects.map{ |obj| association_fields(association, obj, &block) }.join
43
+ fields = objects.map{ |obj| association_fields(association, obj, :html => html, &block) }.join
43
44
  new_object = @object.send(association).build options.delete(:default_attributes) || {}
44
- js_fields = association_fields association, new_object, :child_index => "new_#{singular_name}", &block
45
+ js_fields = association_fields association, new_object, :child_index => "new_#{singular_name}", :html => html, &block
45
46
 
46
- @template.concat <<-HTML
47
+ association_fields = <<-HTML
47
48
  <div id="#{association}">
48
49
  #{fields}
49
50
  #{@template.javascript_tag "var fields_for_#{singular_name} = '#{js_fields.strip.gsub /\n\s+|\n/, ''}';"}
50
51
  <a href="#" class="add_fields" data-association="#{singular_name}">#{I18n.t 'add', :model => association_class.human_name}</a>
51
52
  </div>
52
53
  HTML
54
+
55
+ @template.concat ActiveSupport::SafeBuffer.new association_fields
53
56
  end
54
57
 
55
58
  def custom_fields_for *args, &block
56
59
  opts = args.extract_options!
57
60
  html_opts = opts.delete(:html) || {}
58
61
  fields = @template.capture{fields_for *(args << opts), &block}
59
- @template.concat DryForms.fields_for_markup(fields, html_opts) unless fields.empty?
62
+ @template.concat ActiveSupport::SafeBuffer.new DryForms.fields_for_markup(fields, html_opts) unless fields.empty?
60
63
  end
61
64
 
62
65
  private
63
66
  def association_fields association, object, opts = {}, &block
64
- opts.merge! :html => {:class => "associated", :'data-association' => association.singularize}
67
+ html = opts.delete(:html) || {}
68
+ opts.merge! :html => html.merge(:class => "associated", :'data-association' => association.singularize)
65
69
 
66
- fields = @template.capture do
70
+ association_fields = @template.capture do
67
71
  custom_fields_for association.to_sym, object, opts do |fields|
68
72
  @template.concat fields.hidden_field :_destroy, :class => 'destroy'
69
73
  yield fields
70
- @template.concat %{<a class="remove" href="#">#{I18n.t "remove"}</a>}
74
+ @template.concat ActiveSupport::SafeBuffer.new %{<a class="remove" href="#">#{I18n.t "remove"}</a>}
71
75
  end
72
76
  end
73
- end
74
77
 
75
- # def submit *args
76
- # options = args.extract_options!
77
- # options[:class] = "submit #{ options[:class] }".strip
78
- # value = args.first || I18n.t(@object.new_record? ? 'create' : 'save', :scope => 'helpers.submit')
79
- # super value, options
80
- # end
78
+ ActiveSupport::SafeBuffer.new %{#{association_fields}}
79
+ end
81
80
  end
82
- end
81
+ end
data/test/helper.rb CHANGED
@@ -3,12 +3,10 @@ require 'test/unit'
3
3
  require 'shoulda'
4
4
 
5
5
  require 'active_support'
6
- require 'action_pack'
7
6
  require 'active_record'
8
7
  require 'action_controller'
9
8
  require 'action_view'
10
9
  require 'action_view/test_case'
11
- require 'action_controller/test_process'
12
10
 
13
11
  $LOAD_PATH.unshift "#{File.dirname __FILE__}/../lib"
14
12
  $LOAD_PATH.unshift File.dirname(__FILE__)
@@ -16,18 +14,16 @@ $LOAD_PATH.unshift File.dirname(__FILE__)
16
14
  require 'dry_forms'
17
15
  require 'support/models'
18
16
 
19
- ActionController::Base.view_paths << FIXTURES = "#{ File.dirname __FILE__ }/fixtures'"
20
-
21
- class ActionView::TestCase::TestController
22
- attr_reader :url_for_options
23
- def url_for(options)
24
- @url_for_options = options
25
- "/do"
26
- end
27
-
28
- end
29
-
30
17
  class ActionView::Base
31
18
  def protect_against_forgery?; end
32
19
  end
33
20
 
21
+ require 'ostruct'
22
+
23
+ module ActionController::UrlFor
24
+ def _routes
25
+ helpers = OpenStruct.new
26
+ helpers.url_helpers = Module.new
27
+ helpers
28
+ end
29
+ end
@@ -22,7 +22,6 @@ ActiveRecord::Schema.define :version => 1 do
22
22
  end
23
23
  end
24
24
 
25
-
26
25
  class Post < ActiveRecord::Base
27
26
  belongs_to :author
28
27
  accepts_nested_attributes_for :author
@@ -37,4 +36,4 @@ class Author < ActiveRecord::Base
37
36
  has_many :posts
38
37
  has_many :comments
39
38
  accepts_nested_attributes_for :posts
40
- end
39
+ end
@@ -12,7 +12,7 @@ class FormHelperTest < ActionView::TestCase
12
12
  context 'helper method rendering' do
13
13
  should 'render check_box with custom markup and display human_attribute_name' do
14
14
  erb = <<-ERB
15
- <% custom_form_for :post, @post do |f| %>
15
+ <% custom_form_for :post, @post, :url => '/do' do |f| %>
16
16
  <%= f.custom_text_field :title, :class => 'title' %>
17
17
  <% end %>
18
18
  ERB
@@ -31,7 +31,7 @@ class FormHelperTest < ActionView::TestCase
31
31
 
32
32
  should 'render check_box with custom markup and display human_attribute_name and html attributes' do
33
33
  erb = <<-ERB
34
- <% custom_form_for :post, @post do |f| %>
34
+ <% custom_form_for :post, @post, :url => '/do' do |f| %>
35
35
  <%= f.custom_text_field :title, :class => 'title', :html => {:id => 'id', :class => 'class'} %>
36
36
  <% end %>
37
37
  ERB
@@ -55,7 +55,7 @@ class FormHelperTest < ActionView::TestCase
55
55
  assert_equal false, @post.valid?
56
56
 
57
57
  erb = <<-ERB
58
- <% custom_form_for :post, @post do |f| %>
58
+ <% custom_form_for :post, @post, :url => '/do' do |f| %>
59
59
  <%= f.custom_text_field :title, :class => 'title' %>
60
60
  <% end %>
61
61
  ERB
@@ -82,7 +82,7 @@ class FormHelperTest < ActionView::TestCase
82
82
 
83
83
  should 'render js fields' do
84
84
  erb = <<-ERB
85
- <% form_for :author, @author do |form| %>
85
+ <% form_for :author, @author, :url => '/do' do |form| %>
86
86
  <h2>Posts</h2>
87
87
  <% form.fields_for_association :posts do |post| %>
88
88
  <%= post.text_field :title %>
@@ -91,7 +91,7 @@ class FormHelperTest < ActionView::TestCase
91
91
  ERB
92
92
 
93
93
  js_fields = <<-HTML
94
- <fieldset class="associated" data-association="post">
94
+ <fieldset data-association="post" class="associated">
95
95
  <input class="destroy" id="author_posts_attributes_new_post__destroy" name="author[posts_attributes][new_post][_destroy]" type="hidden" />
96
96
  <input id="author_posts_attributes_new_post_title" name="author[posts_attributes][new_post][title]" size="30" type="text" />
97
97
  <a class="remove" href="#">translation missing: en, remove</a>
@@ -120,7 +120,7 @@ class FormHelperTest < ActionView::TestCase
120
120
  @author.posts.create! :title => "Hello World 2", :body => "Looking good"
121
121
 
122
122
  erb = <<-ERB
123
- <% form_for :author, @author do |form| %>
123
+ <% form_for :author, @author, :url => '/do' do |form| %>
124
124
  <h2>Posts</h2>
125
125
  <% form.fields_for_association :posts do |post| %>
126
126
  <%= post.text_field :title %>
@@ -129,7 +129,7 @@ class FormHelperTest < ActionView::TestCase
129
129
  ERB
130
130
 
131
131
  js_fields = <<-HTML
132
- <fieldset class="associated" data-association="post">
132
+ <fieldset data-association="post" class="associated">
133
133
  <input class="destroy" id="author_posts_attributes_new_post__destroy" name="author[posts_attributes][new_post][_destroy]" type="hidden" />
134
134
  <input id="author_posts_attributes_new_post_title" name="author[posts_attributes][new_post][title]" size="30" type="text" />
135
135
  <a class="remove" href="#">translation missing: en, remove</a>
@@ -165,12 +165,55 @@ class FormHelperTest < ActionView::TestCase
165
165
  assert_dom_equal html, render(:inline => erb)
166
166
  end
167
167
 
168
+ should 'render field for existing associations allowing custom html' do
169
+ @author.posts.create! :title => "Hello World", :body => "Looking good"
170
+
171
+ erb = <<-ERB
172
+ <% form_for :author, @author, :url => '/do' do |form| %>
173
+ <h2>Posts</h2>
174
+ <% form.fields_for_association :posts, :html => {'data-custom' => 'data'} do |post| %>
175
+ <%= post.text_field :title %>
176
+ <% end %>
177
+ <% end %>
178
+ ERB
179
+
180
+ js_fields = <<-HTML
181
+ <fieldset data-association="post" class="associated" data-custom="data">
182
+ <input class="destroy" id="author_posts_attributes_new_post__destroy" name="author[posts_attributes][new_post][_destroy]" type="hidden" />
183
+ <input id="author_posts_attributes_new_post_title" name="author[posts_attributes][new_post][title]" size="30" type="text" />
184
+ <a class="remove" href="#">translation missing: en, remove</a>
185
+ </fieldset>
186
+ HTML
187
+
188
+ html = <<-HTML
189
+ <form method="post" action="/do">
190
+ <h2>Posts</h2>
191
+ <div id="posts">
192
+ <fieldset class="associated" data-association="post" data-custom="data">
193
+ <input class="destroy" name="author[posts_attributes][0][_destroy]" id="author_posts_attributes_0__destroy" type="hidden" />
194
+ <input name="author[posts_attributes][0][title]" size="30" id="author_posts_attributes_0_title" type="text" value="Hello World" />
195
+ <a href="#" class="remove">translation missing: en, remove</a>
196
+ <input name="author[posts_attributes][0][id]" id="author_posts_attributes_0_id" type="hidden" value="#{ @author.posts.first.id }" />
197
+ </fieldset>
198
+ <script type="text/javascript">
199
+ //<![CDATA[
200
+ var fields_for_post = '#{js_fields.gsub(/\n\s+/, '').strip}';
201
+ //]]>
202
+ </script>
203
+ <a href="#" class="add_fields" data-association="post">translation missing: en, add</a>
204
+ </div>
205
+ </form>
206
+ HTML
207
+
208
+ assert_dom_equal html, render(:inline => erb)
209
+ end
210
+
168
211
  should 'render field for existing associations passing objects' do
169
212
  @author.posts.create! :title => "Hello World", :body => "Looking good"
170
213
  @author.posts.create! :title => "Hello World 2", :body => "Looking good"
171
214
 
172
215
  erb = <<-ERB
173
- <% form_for :author, @author do |form| %>
216
+ <% form_for :author, @author, :url => '/do' do |form| %>
174
217
  <h2>Posts</h2>
175
218
  <% form.fields_for_association :posts, @author.posts[0..0] do |post| %>
176
219
  <%= post.text_field :title %>
@@ -179,7 +222,7 @@ class FormHelperTest < ActionView::TestCase
179
222
  ERB
180
223
 
181
224
  js_fields = <<-HTML
182
- <fieldset class="associated" data-association="post">
225
+ <fieldset data-association="post" class="associated">
183
226
  <input class="destroy" id="author_posts_attributes_new_post__destroy" name="author[posts_attributes][new_post][_destroy]" type="hidden" />
184
227
  <input id="author_posts_attributes_new_post_title" name="author[posts_attributes][new_post][title]" size="30" type="text" />
185
228
  <a class="remove" href="#">translation missing: en, remove</a>
@@ -211,7 +254,7 @@ class FormHelperTest < ActionView::TestCase
211
254
 
212
255
  should 'raise error if model does not accepts nested attributes for association' do
213
256
  assert_raises(NotImplementedError) do
214
- form_for :comment, @comment do |form|
257
+ form_for :comment, @comment, :url => '/do' do |form|
215
258
  form.fields_for_association(:author) {}
216
259
  end
217
260
  end
@@ -219,7 +262,7 @@ class FormHelperTest < ActionView::TestCase
219
262
 
220
263
  should 'raise error if model does not accepts nested attributes for pluralized association' do
221
264
  assert_raises(NotImplementedError) do
222
- form_for :post, @post do |form|
265
+ form_for :post, @post, :url => '/do' do |form|
223
266
  form.fields_for_association(:author) {}
224
267
  end
225
268
  end
@@ -227,7 +270,7 @@ class FormHelperTest < ActionView::TestCase
227
270
 
228
271
  should 'raise argument error if no block is passed' do
229
272
  assert_raises(ArgumentError) do
230
- form_for :author, @author do |form|
273
+ form_for :author, @author, :url => '/do' do |form|
231
274
  form.fields_for_association(:posts)
232
275
  end
233
276
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dry_forms
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 1
10
- version: 0.2.1
9
+ - 2
10
+ version: 0.2.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Macario Ortega
@@ -15,11 +15,11 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-09-11 00:00:00 -05:00
18
+ date: 2010-10-11 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
- name: thoughtbot-shoulda
22
+ name: shoulda
23
23
  prerelease: false
24
24
  requirement: &id001 !ruby/object:Gem::Requirement
25
25
  none: false
@@ -91,6 +91,6 @@ signing_key:
91
91
  specification_version: 3
92
92
  summary: Form builder additions for drier and localized forms, and association fields adding and removing with jQuery sweetness
93
93
  test_files:
94
- - test/support/models.rb
95
- - test/helper.rb
96
94
  - test/test_dry_forms.rb
95
+ - test/helper.rb
96
+ - test/support/models.rb