dry_forms 0.1.0 → 0.2.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.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
@@ -15,12 +15,21 @@ module DryForms
15
15
  label = label attribute, label_text
16
16
  field = send name, attribute, *(args << opts)
17
17
 
18
- DryForms.field_markup label, field, @object && @object.errors[attribute], html_opts
18
+ errors =
19
+ if @object and name == 'file_field' # Not specked, for paperclip
20
+ %w(file_name content_type file_size).map { |f| @object.errors["#{attribute}_#{f}".to_sym] }.unshift @object.errors[attribute]
21
+ elsif @object
22
+ @object.errors[attribute]
23
+ end
24
+
25
+ DryForms.field_markup label, field, [*errors].compact, html_opts
19
26
  end
20
27
  end
21
28
 
22
- def fields_for_association association, options = {}, &block
29
+ def fields_for_association association, *args, &block
30
+ options = args.extract_options!
23
31
  association = association.to_s
32
+ objects = args.first ? [*args.first] : @object.send(association)
24
33
 
25
34
  unless @object.respond_to? "#{association.pluralize}_attributes="
26
35
  raise NotImplementedError, "Please call `accepts_nested_attributes_for :#{association.pluralize}` in your `#{@object.class}` Model"
@@ -30,7 +39,7 @@ module DryForms
30
39
 
31
40
  association_class = @object.class.reflect_on_association(association.to_sym).klass
32
41
  singular_name = association.singularize
33
- fields = @object.send(association).map{ |obj| association_fields(association, obj, &block) }.join
42
+ fields = objects.map{ |obj| association_fields(association, obj, &block) }.join
34
43
  new_object = @object.send(association).build options.delete(:default_attributes) || {}
35
44
  js_fields = association_fields association, new_object, :child_index => "new_#{singular_name}", &block
36
45
 
@@ -38,7 +47,7 @@ module DryForms
38
47
  <div id="#{association}">
39
48
  #{fields}
40
49
  #{@template.javascript_tag "var fields_for_#{singular_name} = '#{js_fields.strip.gsub /\n\s+|\n/, ''}';"}
41
- <a href="#" class="add_fields" data-association="#{singular_name}">#{I18n.t 'dry_forms.add', :model => association_class.human_name}</a>
50
+ <a href="#" class="add_fields" data-association="#{singular_name}">#{I18n.t 'add', :model => association_class.human_name}</a>
42
51
  </div>
43
52
  HTML
44
53
  end
@@ -58,7 +67,7 @@ module DryForms
58
67
  custom_fields_for association.to_sym, object, opts do |fields|
59
68
  @template.concat fields.hidden_field :_destroy, :class => 'destroy'
60
69
  yield fields
61
- @template.concat %{<a class="remove" href="#">#{I18n.t "dry_forms.remove"}</a>}
70
+ @template.concat %{<a class="remove" href="#">#{I18n.t "remove"}</a>}
62
71
  end
63
72
  end
64
73
  end
@@ -75,6 +75,11 @@ class FormHelperTest < ActionView::TestCase
75
75
  end
76
76
 
77
77
  context 'field for associations' do
78
+ setup do
79
+ @author.posts = []
80
+ Post.destroy_all
81
+ end
82
+
78
83
  should 'render js fields' do
79
84
  erb = <<-ERB
80
85
  <% form_for :author, @author do |form| %>
@@ -89,7 +94,7 @@ class FormHelperTest < ActionView::TestCase
89
94
  <fieldset class="associated" data-association="post">
90
95
  <input class="destroy" id="author_posts_attributes_new_post__destroy" name="author[posts_attributes][new_post][_destroy]" type="hidden" />
91
96
  <input id="author_posts_attributes_new_post_title" name="author[posts_attributes][new_post][title]" size="30" type="text" />
92
- <a class="remove" href="#">translation missing: en, dry_forms, remove</a>
97
+ <a class="remove" href="#">translation missing: en, remove</a>
93
98
  </fieldset>
94
99
  HTML
95
100
 
@@ -102,7 +107,7 @@ class FormHelperTest < ActionView::TestCase
102
107
  var fields_for_post = '#{js_fields.gsub(/\n\s+/, '').strip}';
103
108
  //]]>
104
109
  </script>
105
- <a href="#" class="add_fields" data-association="post">translation missing: en, dry_forms, add</a>
110
+ <a href="#" class="add_fields" data-association="post">translation missing: en, add</a>
106
111
  </div>
107
112
  </form>
108
113
  HTML
@@ -127,7 +132,7 @@ class FormHelperTest < ActionView::TestCase
127
132
  <fieldset class="associated" data-association="post">
128
133
  <input class="destroy" id="author_posts_attributes_new_post__destroy" name="author[posts_attributes][new_post][_destroy]" type="hidden" />
129
134
  <input id="author_posts_attributes_new_post_title" name="author[posts_attributes][new_post][title]" size="30" type="text" />
130
- <a class="remove" href="#">translation missing: en, dry_forms, remove</a>
135
+ <a class="remove" href="#">translation missing: en, remove</a>
131
136
  </fieldset>
132
137
  HTML
133
138
 
@@ -138,27 +143,71 @@ class FormHelperTest < ActionView::TestCase
138
143
  <fieldset class="associated" data-association="post">
139
144
  <input class="destroy" name="author[posts_attributes][0][_destroy]" id="author_posts_attributes_0__destroy" type="hidden" />
140
145
  <input name="author[posts_attributes][0][title]" size="30" id="author_posts_attributes_0_title" type="text" value="Hello World" />
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" />
146
+ <a href="#" class="remove">translation missing: en, remove</a>
147
+ <input name="author[posts_attributes][0][id]" id="author_posts_attributes_0_id" type="hidden" value="#{ @author.posts.first.id }" />
143
148
  </fieldset>
144
149
  <fieldset class="associated" data-association="post">
145
150
  <input class="destroy" name="author[posts_attributes][1][_destroy]" id="author_posts_attributes_1__destroy" type="hidden" />
146
151
  <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" />
152
+ <a href="#" class="remove">translation missing: en, remove</a>
153
+ <input name="author[posts_attributes][1][id]" id="author_posts_attributes_1_id" type="hidden" value="#{ @author.posts.last.id }" />
149
154
  </fieldset>
150
155
  <script type="text/javascript">
151
156
  //<![CDATA[
152
157
  var fields_for_post = '#{js_fields.gsub(/\n\s+/, '').strip}';
153
158
  //]]>
154
159
  </script>
155
- <a href="#" class="add_fields" data-association="post">translation missing: en, dry_forms, add</a>
160
+ <a href="#" class="add_fields" data-association="post">translation missing: en, add</a>
156
161
  </div>
157
162
  </form>
158
163
  HTML
159
164
 
160
165
  assert_dom_equal html, render(:inline => erb)
161
166
  end
167
+
168
+ should 'render field for existing associations passing objects' do
169
+ @author.posts.create! :title => "Hello World", :body => "Looking good"
170
+ @author.posts.create! :title => "Hello World 2", :body => "Looking good"
171
+
172
+ erb = <<-ERB
173
+ <% form_for :author, @author do |form| %>
174
+ <h2>Posts</h2>
175
+ <% form.fields_for_association :posts, @author.posts[0..0] do |post| %>
176
+ <%= post.text_field :title %>
177
+ <% end %>
178
+ <% end %>
179
+ ERB
180
+
181
+ js_fields = <<-HTML
182
+ <fieldset class="associated" data-association="post">
183
+ <input class="destroy" id="author_posts_attributes_new_post__destroy" name="author[posts_attributes][new_post][_destroy]" type="hidden" />
184
+ <input id="author_posts_attributes_new_post_title" name="author[posts_attributes][new_post][title]" size="30" type="text" />
185
+ <a class="remove" href="#">translation missing: en, remove</a>
186
+ </fieldset>
187
+ HTML
188
+
189
+ html = <<-HTML
190
+ <form method="post" action="/do">
191
+ <h2>Posts</h2>
192
+ <div id="posts">
193
+ <fieldset class="associated" data-association="post">
194
+ <input class="destroy" name="author[posts_attributes][0][_destroy]" id="author_posts_attributes_0__destroy" type="hidden" />
195
+ <input name="author[posts_attributes][0][title]" size="30" id="author_posts_attributes_0_title" type="text" value="Hello World" />
196
+ <a href="#" class="remove">translation missing: en, remove</a>
197
+ <input name="author[posts_attributes][0][id]" id="author_posts_attributes_0_id" type="hidden" value="#{ @author.posts.first.id }" />
198
+ </fieldset>
199
+ <script type="text/javascript">
200
+ //<![CDATA[
201
+ var fields_for_post = '#{js_fields.gsub(/\n\s+/, '').strip}';
202
+ //]]>
203
+ </script>
204
+ <a href="#" class="add_fields" data-association="post">translation missing: en, add</a>
205
+ </div>
206
+ </form>
207
+ HTML
208
+
209
+ assert_dom_equal html, render(:inline => erb)
210
+ end
162
211
 
163
212
  should 'raise error if model does not accepts nested attributes for association' do
164
213
  assert_raises(NotImplementedError) do
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
7
+ - 2
8
8
  - 0
9
- version: 0.1.0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Macario Ortega
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-08-11 00:00:00 -05:00
17
+ date: 2010-09-08 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency