show_for 0.2.4 → 0.2.5
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +9 -0
- data/MIT-LICENSE +1 -1
- data/lib/generators/show_for/templates/show_for.rb +1 -1
- data/lib/show_for/association.rb +2 -2
- data/lib/show_for/attribute.rb +36 -13
- data/lib/show_for/builder.rb +4 -5
- data/lib/show_for/content.rb +6 -7
- data/lib/show_for/helper.rb +1 -1
- data/lib/show_for/label.rb +1 -1
- data/lib/show_for/version.rb +1 -1
- data/lib/show_for.rb +15 -2
- data/test/association_test.rb +72 -0
- data/test/attribute_test.rb +167 -0
- data/test/builder_test.rb +8 -419
- data/test/content_test.rb +26 -0
- data/test/label_test.rb +70 -0
- data/test/support/misc_helpers.rb +38 -4
- data/test/support/models.rb +1 -1
- data/test/test_helper.rb +1 -4
- data/test/value_test.rb +89 -0
- metadata +59 -49
- data/.gitignore +0 -1
- data/Gemfile +0 -9
- data/Gemfile.lock +0 -79
- data/README.rdoc +0 -182
- data/Rakefile +0 -27
- data/init.rb +0 -1
- data/show_for.gemspec +0 -21
data/test/builder_test.rb
CHANGED
@@ -1,44 +1,16 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
class BuilderTest < ActionView::TestCase
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
concat(show_for(object) do |o|
|
13
|
-
concat o.value(attribute, options, &block)
|
14
|
-
end)
|
15
|
-
end
|
16
|
-
|
17
|
-
def with_association_for(object, association, options={}, &block)
|
18
|
-
concat(show_for(object) do |o|
|
19
|
-
concat o.association(association, options, &block)
|
20
|
-
end)
|
21
|
-
end
|
22
|
-
|
23
|
-
def with_label_for(object, attribute, options={})
|
24
|
-
concat(show_for(object) do |o|
|
25
|
-
concat o.label(attribute, options)
|
26
|
-
end)
|
27
|
-
end
|
28
|
-
|
29
|
-
def with_content_for(object, value, options={})
|
30
|
-
concat(show_for(object) do |o|
|
31
|
-
concat o.content(value, options)
|
32
|
-
end)
|
33
|
-
end
|
34
|
-
|
35
|
-
def with_attributes_for(object, *attributes)
|
36
|
-
concat(show_for(object) do |o|
|
37
|
-
concat o.attributes(*attributes)
|
38
|
-
end)
|
4
|
+
# WRAPPER
|
5
|
+
test "show_for allows wrapper to be configured globally" do
|
6
|
+
swap ShowFor, :wrapper_tag => "li", :wrapper_class => "my_wrapper" do
|
7
|
+
with_attribute_for @user, :name
|
8
|
+
assert_select "div.show_for li.user_name.my_wrapper"
|
9
|
+
assert_select "div.show_for li.my_wrapper strong.label"
|
10
|
+
assert_select "div.show_for li.my_wrapper"
|
11
|
+
end
|
39
12
|
end
|
40
13
|
|
41
|
-
# WRAPPER
|
42
14
|
test "show_for attribute wraps each attribute with a label and content" do
|
43
15
|
with_attribute_for @user, :name
|
44
16
|
assert_select "div.show_for p.user_name.wrapper"
|
@@ -68,387 +40,4 @@ class BuilderTest < ActionView::TestCase
|
|
68
40
|
assert_select "div.show_for p.wrapper"
|
69
41
|
end
|
70
42
|
end
|
71
|
-
|
72
|
-
# LABEL
|
73
|
-
test "show_for shows a label using the humanized attribute name from model" do
|
74
|
-
with_attribute_for @user, :name
|
75
|
-
assert_select "div.show_for p.wrapper strong.label", "Super User Name!"
|
76
|
-
end
|
77
|
-
|
78
|
-
test "show_for skips label if requested" do
|
79
|
-
with_attribute_for @user, :name, :label => false
|
80
|
-
assert_no_select "div.show_for p.wrapper strong.label"
|
81
|
-
assert_no_select "div.show_for p.wrapper br"
|
82
|
-
end
|
83
|
-
|
84
|
-
test "show_for allows label to be configured globally" do
|
85
|
-
swap ShowFor, :label_tag => :span do
|
86
|
-
with_attribute_for @user, :name
|
87
|
-
assert_select "div.show_for p.wrapper span.label"
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
test "show_for allows label to be changed by attribute" do
|
92
|
-
with_attribute_for @user, :name, :label_tag => :span
|
93
|
-
assert_select "div.show_for p.wrapper span.label"
|
94
|
-
end
|
95
|
-
|
96
|
-
test "show_for allows label html to be configured by attribute" do
|
97
|
-
with_attribute_for @user, :name, :label_html => { :id => "thelabel", :class => "special" }
|
98
|
-
assert_select "div.show_for p.wrapper strong#thelabel.special.label"
|
99
|
-
end
|
100
|
-
|
101
|
-
test "show_for allows label to be set without lookup" do
|
102
|
-
with_attribute_for @user, :name, :label => "Special Label"
|
103
|
-
assert_select "div.show_for p.wrapper strong.label", "Special Label"
|
104
|
-
end
|
105
|
-
|
106
|
-
test "show_for#label accepts the text" do
|
107
|
-
with_label_for @user, "Special Label"
|
108
|
-
assert_select "div.show_for strong.label", "Special Label"
|
109
|
-
end
|
110
|
-
|
111
|
-
test "show_for#label accepts an attribute name" do
|
112
|
-
with_label_for @user, :name
|
113
|
-
assert_select "div.show_for strong.label", "Super User Name!"
|
114
|
-
end
|
115
|
-
|
116
|
-
test "show_for#label accepts html options" do
|
117
|
-
with_label_for @user, :name, :id => "thelabel", :class => "special"
|
118
|
-
assert_select "div.show_for strong#thelabel.special.label"
|
119
|
-
end
|
120
|
-
|
121
|
-
# CONTENT
|
122
|
-
test "show_for allows content tag to be configured globally" do
|
123
|
-
swap ShowFor, :content_tag => :span do
|
124
|
-
with_attribute_for @user, :name
|
125
|
-
assert_select "div.show_for p.wrapper span.content"
|
126
|
-
end
|
127
|
-
end
|
128
|
-
|
129
|
-
test "show_for allows content tag to be changed by attribute" do
|
130
|
-
with_attribute_for @user, :name, :content_tag => :span
|
131
|
-
assert_select "div.show_for p.wrapper span.content"
|
132
|
-
end
|
133
|
-
|
134
|
-
test "show_for allows content tag html to be configured by attribute" do
|
135
|
-
with_attribute_for @user, :name, :content_tag => :span, :content_html => { :id => "thecontent", :class => "special" }
|
136
|
-
assert_select "div.show_for p.wrapper span#thecontent.special.content"
|
137
|
-
end
|
138
|
-
|
139
|
-
test "show_for accepts an attribute as string" do
|
140
|
-
with_attribute_for @user, :name
|
141
|
-
assert_select "div.show_for p.wrapper", /ShowFor/
|
142
|
-
end
|
143
|
-
|
144
|
-
test "show_for accepts an attribute as time" do
|
145
|
-
with_attribute_for @user, :created_at
|
146
|
-
assert_select "div.show_for p.wrapper", /#{Regexp.escape(I18n.l(@user.created_at))}/
|
147
|
-
end
|
148
|
-
|
149
|
-
test "show_for accepts an attribute as date" do
|
150
|
-
with_attribute_for @user, :updated_at
|
151
|
-
assert_select "div.show_for p.wrapper", /#{Regexp.escape(I18n.l(@user.updated_at))}/
|
152
|
-
end
|
153
|
-
|
154
|
-
test "show_for accepts an attribute as time with format options" do
|
155
|
-
with_attribute_for @user, :created_at, :format => :long
|
156
|
-
assert_select "div.show_for p.wrapper", /#{Regexp.escape(I18n.l(@user.created_at, :format => :long))}/
|
157
|
-
end
|
158
|
-
|
159
|
-
test "show_for accepts an attribute as true" do
|
160
|
-
with_attribute_for @user, :active
|
161
|
-
assert_select "div.show_for p.wrapper", /Yes/
|
162
|
-
end
|
163
|
-
|
164
|
-
test "show_for accepts an attribute as true which can be localized" do
|
165
|
-
store_translations(:en, :show_for => { :yes => "Hell yeah!" }) do
|
166
|
-
with_attribute_for @user, :active
|
167
|
-
assert_select "div.show_for p.wrapper", /Hell yeah!/
|
168
|
-
end
|
169
|
-
end
|
170
|
-
|
171
|
-
test "should let you override the label wrapper" do
|
172
|
-
swap ShowFor, :label_proc => proc { |l| l + ":" } do
|
173
|
-
with_label_for @user, "Special Label"
|
174
|
-
assert_select "div.show_for strong.label", "Special Label:"
|
175
|
-
end
|
176
|
-
end
|
177
|
-
|
178
|
-
test "should you skip wrapping the label on a per item basis" do
|
179
|
-
swap ShowFor, :label_proc => proc { |l| l + ":" } do
|
180
|
-
with_label_for @user, "Special Label", :wrap_label => false
|
181
|
-
assert_select "div.show_for strong.label", "Special Label"
|
182
|
-
end
|
183
|
-
end
|
184
|
-
|
185
|
-
test "show_for accepts an attribute as false" do
|
186
|
-
with_attribute_for @user, :invalid
|
187
|
-
assert_select "div.show_for p.wrapper", /No/
|
188
|
-
end
|
189
|
-
|
190
|
-
test "show_for accepts an attribute as false which can be localized" do
|
191
|
-
store_translations(:en, :show_for => { :no => "Hell no!" }) do
|
192
|
-
with_attribute_for @user, :invalid
|
193
|
-
assert_select "div.show_for p.wrapper", /Hell no!/
|
194
|
-
end
|
195
|
-
end
|
196
|
-
|
197
|
-
test "show_for accepts nil and or blank attributes" do
|
198
|
-
with_attribute_for @user, :description
|
199
|
-
assert_select "div.show_for p.wrapper", /Not specified/
|
200
|
-
end
|
201
|
-
|
202
|
-
test "show_for accepts not spcified message can be localized" do
|
203
|
-
store_translations(:en, :show_for => { :blank => "OMG! It's blank!" }) do
|
204
|
-
with_attribute_for @user, :description
|
205
|
-
assert_select "div.show_for p.wrapper", /OMG! It's blank!/
|
206
|
-
end
|
207
|
-
end
|
208
|
-
|
209
|
-
test "show_for uses :if_blank if attribute is blank" do
|
210
|
-
with_attribute_for @user, :description, :if_blank => "No description provided"
|
211
|
-
assert_select "div.show_for p.wrapper", /No description provided/
|
212
|
-
end
|
213
|
-
|
214
|
-
test "show_for accepts a block to supply the content" do
|
215
|
-
with_attribute_for @user, :description do
|
216
|
-
"This description is not blank"
|
217
|
-
end
|
218
|
-
assert_select "div.show_for p.wrapper", /This description/
|
219
|
-
end
|
220
|
-
|
221
|
-
test "show_for escapes content by default" do
|
222
|
-
@user.name = "<b>hack you!</b>"
|
223
|
-
with_attribute_for @user, :name
|
224
|
-
assert_no_select "div.show_for p.wrapper b"
|
225
|
-
assert_select "div.show_for p.wrapper", /<b>/
|
226
|
-
end
|
227
|
-
|
228
|
-
test "show_for works with html_safe marked strings" do
|
229
|
-
@user.name = "<b>hack you!</b>".html_safe
|
230
|
-
with_attribute_for @user, :name
|
231
|
-
assert_select "div.show_for p.wrapper b", "hack you!"
|
232
|
-
end
|
233
|
-
|
234
|
-
test "show_for#content accepts any object" do
|
235
|
-
with_content_for @user, "Special content"
|
236
|
-
assert_select "div.show_for", "Special content"
|
237
|
-
end
|
238
|
-
|
239
|
-
test "show_for#content accepts :if_blank as option" do
|
240
|
-
with_content_for @user, "", :if_blank => "Got blank"
|
241
|
-
assert_select "div.show_for", "Got blank"
|
242
|
-
end
|
243
|
-
|
244
|
-
test "show_for#content accepts html options" do
|
245
|
-
with_content_for @user, "Special content", :content_tag => :b, :id => "thecontent", :class => "special"
|
246
|
-
assert_select "div.show_for b#thecontent.special.content", "Special content"
|
247
|
-
end
|
248
|
-
|
249
|
-
test "show_for#content with blank value has a 'no value'-class" do
|
250
|
-
swap ShowFor, :blank_content_class => "nothing" do
|
251
|
-
with_content_for @user, nil, :content_tag => :b
|
252
|
-
assert_select "div.show_for b.nothing"
|
253
|
-
end
|
254
|
-
end
|
255
|
-
|
256
|
-
# VALUE
|
257
|
-
test "show_for allows content tag to be configured globally, without label and separator" do
|
258
|
-
swap ShowFor, :content_tag => :span do
|
259
|
-
with_value_for @user, :name
|
260
|
-
assert_no_select "div.show_for p.wrapper strong.label"
|
261
|
-
assert_no_select "div.show_for p.wrapper br"
|
262
|
-
assert_select "div.show_for p.wrapper span.content"
|
263
|
-
end
|
264
|
-
end
|
265
|
-
|
266
|
-
test "show_for allows content with tag to be changed by attribute, without label and separator" do
|
267
|
-
with_value_for @user, :name, :content_tag => :p
|
268
|
-
assert_no_select "div.show_for p.wrapper strong.label"
|
269
|
-
assert_no_select "div.show_for p.wrapper br"
|
270
|
-
assert_select "div.show_for p.wrapper p.content"
|
271
|
-
end
|
272
|
-
|
273
|
-
test "show_for allows content tag html to be configured by attribute, without label and separator" do
|
274
|
-
with_value_for @user, :name, :content_tag => :span, :content_html => { :id => "thecontent", :class => "special" }
|
275
|
-
assert_no_select "div.show_for p.wrapper strong.label"
|
276
|
-
assert_no_select "div.show_for p.wrapper br"
|
277
|
-
assert_select "div.show_for p.wrapper span#thecontent.special.content"
|
278
|
-
end
|
279
|
-
|
280
|
-
test "show_for accepts an attribute as string, without label and separator" do
|
281
|
-
with_value_for @user, :name
|
282
|
-
assert_no_select "div.show_for p.wrapper strong.label"
|
283
|
-
assert_no_select "div.show_for p.wrapper br"
|
284
|
-
assert_select "div.show_for p.wrapper", /ShowFor/
|
285
|
-
end
|
286
|
-
|
287
|
-
test "show_for accepts an attribute as time, without label and separator" do
|
288
|
-
with_value_for @user, :created_at
|
289
|
-
assert_no_select "div.show_for p.wrapper strong.label"
|
290
|
-
assert_no_select "div.show_for p.wrapper br"
|
291
|
-
assert_select "div.show_for p.wrapper", /#{Regexp.escape(I18n.l(@user.created_at))}/
|
292
|
-
end
|
293
|
-
|
294
|
-
test "show_for accepts an attribute as date, without label and separator" do
|
295
|
-
with_value_for @user, :updated_at
|
296
|
-
assert_no_select "div.show_for p.wrapper strong.label"
|
297
|
-
assert_no_select "div.show_for p.wrapper br"
|
298
|
-
assert_select "div.show_for p.wrapper", /#{Regexp.escape(I18n.l(@user.updated_at))}/
|
299
|
-
end
|
300
|
-
|
301
|
-
test "show_for accepts an attribute as time with format options, without label and separator" do
|
302
|
-
with_value_for @user, :created_at, :format => :long
|
303
|
-
assert_select "div.show_for p.wrapper", /#{Regexp.escape(I18n.l(@user.created_at, :format => :long))}/
|
304
|
-
end
|
305
|
-
|
306
|
-
test "show_for accepts an attribute as nil, without label and separator" do
|
307
|
-
c = with_value_for @user, :birthday
|
308
|
-
assert_no_select "div.show_for p.wrapper strong.label"
|
309
|
-
assert_no_select "div.show_for p.wrapper br"
|
310
|
-
assert_select "div.show_for p.wrapper", /Not specified/
|
311
|
-
end
|
312
|
-
|
313
|
-
test "show_for accepts blank attributes, without label and separator" do
|
314
|
-
with_value_for @user, :description
|
315
|
-
assert_no_select "div.show_for p.wrapper strong.label"
|
316
|
-
assert_no_select "div.show_for p.wrapper br"
|
317
|
-
assert_select "div.show_for p.wrapper", /Not specified/
|
318
|
-
end
|
319
|
-
|
320
|
-
test "show_for uses :if_blank if attribute is nil, without label and separator" do
|
321
|
-
with_value_for @user, :birthday, :if_blank => "No description provided"
|
322
|
-
assert_no_select "div.show_for p.wrapper strong.label"
|
323
|
-
assert_no_select "div.show_for p.wrapper br"
|
324
|
-
assert_select "div.show_for p.wrapper", /No description provided/
|
325
|
-
end
|
326
|
-
|
327
|
-
test "show_for uses :if_blank if attribute is blank, without label and separator" do
|
328
|
-
with_value_for @user, :description, :if_blank => "No description provided"
|
329
|
-
assert_no_select "div.show_for p.wrapper strong.label"
|
330
|
-
assert_no_select "div.show_for p.wrapper br"
|
331
|
-
assert_select "div.show_for p.wrapper", /No description provided/
|
332
|
-
end
|
333
|
-
|
334
|
-
test "show_for escapes content by default, without label and separator" do
|
335
|
-
@user.name = "<b>hack you!</b>"
|
336
|
-
with_value_for @user, :name
|
337
|
-
assert_no_select "div.show_for p.wrapper strong.label"
|
338
|
-
assert_no_select "div.show_for p.wrapper br"
|
339
|
-
assert_no_select "div.show_for p.wrapper b"
|
340
|
-
assert_select "div.show_for p.wrapper", /<b>/
|
341
|
-
end
|
342
|
-
|
343
|
-
# COLLECTIONS
|
344
|
-
test "show_for accepts an attribute as a collection" do
|
345
|
-
with_attribute_for @user, :scopes
|
346
|
-
assert_select "div.show_for p.wrapper ul.collection"
|
347
|
-
assert_select "div.show_for p.wrapper ul.collection li", :count => 3
|
348
|
-
end
|
349
|
-
|
350
|
-
test "show_for accepts an attribute as a collection with a block to iterate the collection" do
|
351
|
-
with_attribute_for @user, :scopes do |scope|
|
352
|
-
content_tag :span, scope
|
353
|
-
end
|
354
|
-
assert_select "div.show_for p.wrapper ul.collection"
|
355
|
-
assert_select "div.show_for p.wrapper ul.collection span", :count => 3
|
356
|
-
end
|
357
|
-
|
358
|
-
test "show_for allows collection tag to be configured globally" do
|
359
|
-
swap ShowFor, :collection_tag => :ol do
|
360
|
-
with_attribute_for @user, :scopes
|
361
|
-
assert_select "div.show_for p.wrapper ol.collection"
|
362
|
-
end
|
363
|
-
end
|
364
|
-
|
365
|
-
test "show_for allows collection tag to be changed by attribute" do
|
366
|
-
with_attribute_for @user, :scopes, :collection_tag => :ol
|
367
|
-
assert_select "div.show_for p.wrapper ol.collection"
|
368
|
-
end
|
369
|
-
|
370
|
-
test "show_for allows collection tag html to be configured by attribute" do
|
371
|
-
with_attribute_for @user, :scopes, :collection_html => { :id => "thecollection", :class => "special" }
|
372
|
-
assert_select "div.show_for p.wrapper ul#thecollection.special.collection"
|
373
|
-
end
|
374
|
-
|
375
|
-
# ASSOCIATIONS
|
376
|
-
test "show_for works with belongs_to/has_one associations" do
|
377
|
-
with_association_for @user, :company
|
378
|
-
assert_select "div.show_for p.wrapper", /PlataformaTec/
|
379
|
-
end
|
380
|
-
|
381
|
-
test "show_for accepts :using as option to tell how to retrieve association value" do
|
382
|
-
with_association_for @user, :company, :using => :alternate_name
|
383
|
-
assert_select "div.show_for p.wrapper", /Alternate PlataformaTec/
|
384
|
-
end
|
385
|
-
|
386
|
-
test "show_for accepts :in to tell to retrieve an attribute from association" do
|
387
|
-
with_attribute_for @user, :alternate_name, :in => :company
|
388
|
-
assert_select "div.show_for p.wrapper", /Alternate PlataformaTec/
|
389
|
-
end
|
390
|
-
|
391
|
-
test "show_for forwards all options send with :in to association" do
|
392
|
-
with_attribute_for @user, :alternate_name, :in => :tags, :to_sentence => true
|
393
|
-
assert_no_select "div.show_for p.wrapper ul.collection"
|
394
|
-
assert_select "div.show_for p.wrapper", /Alternate Tag 1, Alternate Tag 2, and Alternate Tag 3/
|
395
|
-
end
|
396
|
-
|
397
|
-
test "show_for works with has_many/has_and_belongs_to_many associations" do
|
398
|
-
with_association_for @user, :tags
|
399
|
-
assert_select "div.show_for p.wrapper ul.collection"
|
400
|
-
assert_select "div.show_for p.wrapper ul.collection li", "Tag 1"
|
401
|
-
assert_select "div.show_for p.wrapper ul.collection li", "Tag 2"
|
402
|
-
assert_select "div.show_for p.wrapper ul.collection li", "Tag 3"
|
403
|
-
end
|
404
|
-
|
405
|
-
test "show_for accepts :using as option to tell how to retrieve association values" do
|
406
|
-
with_association_for @user, :tags, :using => :alternate_name
|
407
|
-
assert_select "div.show_for p.wrapper ul.collection"
|
408
|
-
assert_select "div.show_for p.wrapper ul.collection li", "Alternate Tag 1"
|
409
|
-
assert_select "div.show_for p.wrapper ul.collection li", "Alternate Tag 2"
|
410
|
-
assert_select "div.show_for p.wrapper ul.collection li", "Alternate Tag 3"
|
411
|
-
end
|
412
|
-
|
413
|
-
test "show_for accepts :to_sentence as option in collection associations" do
|
414
|
-
with_association_for @user, :tags, :to_sentence => true
|
415
|
-
assert_no_select "div.show_for p.wrapper ul.collection"
|
416
|
-
assert_select "div.show_for p.wrapper", /Tag 1, Tag 2, and Tag 3/
|
417
|
-
end
|
418
|
-
|
419
|
-
test "show_for accepts :join as option in collection associations" do
|
420
|
-
with_association_for @user, :tags, :join => ", "
|
421
|
-
assert_no_select "div.show_for p.wrapper ul.collection"
|
422
|
-
assert_select "div.show_for p.wrapper", /Tag 1, Tag 2, Tag 3/
|
423
|
-
end
|
424
|
-
|
425
|
-
test "show_for accepts a block without argument in collection associations" do
|
426
|
-
with_association_for @user, :tags do
|
427
|
-
@user.tags.map(&:name).to_sentence
|
428
|
-
end
|
429
|
-
assert_no_select "div.show_for p.wrapper ul.collection"
|
430
|
-
assert_select "div.show_for p.wrapper", /Tag 1, Tag 2, and Tag 3/
|
431
|
-
end
|
432
|
-
|
433
|
-
test "show_for accepts a block with argument in collection associations" do
|
434
|
-
with_association_for @user, :tags, :collection_tag => :p do |tag|
|
435
|
-
assert_kind_of Tag, tag
|
436
|
-
content_tag(:span, tag.name)
|
437
|
-
end
|
438
|
-
assert_no_select "div.show_for p.wrapper ul.collection"
|
439
|
-
assert_select "div.show_for p.wrapper p.collection"
|
440
|
-
assert_select "div.show_for p.wrapper p.collection span", "Tag 1"
|
441
|
-
assert_select "div.show_for p.wrapper p.collection span", "Tag 2"
|
442
|
-
assert_select "div.show_for p.wrapper p.collection span", "Tag 3"
|
443
|
-
end
|
444
|
-
|
445
|
-
# ATTRIBUTES
|
446
|
-
test "show_for attributes wraps each attribute with a label and content" do
|
447
|
-
with_attributes_for @user, :name, :email
|
448
|
-
assert_select "div.show_for p.user_name.wrapper", /ShowFor/
|
449
|
-
assert_select "p.user_name strong.label", "Super User Name!"
|
450
|
-
assert_select "div.show_for p.user_email.wrapper", /Not specified/
|
451
|
-
assert_select "p.user_email strong.label", "Email"
|
452
|
-
end
|
453
43
|
end
|
454
|
-
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ContentTest < ActionView::TestCase
|
4
|
+
test "show_for#content accepts any object" do
|
5
|
+
with_content_for @user, "Special content"
|
6
|
+
assert_select "div.show_for", "Special content"
|
7
|
+
end
|
8
|
+
|
9
|
+
test "show_for#content accepts :if_blank as option" do
|
10
|
+
with_content_for @user, "", :if_blank => "Got blank"
|
11
|
+
assert_select "div.show_for", "Got blank"
|
12
|
+
end
|
13
|
+
|
14
|
+
test "show_for#content accepts html options" do
|
15
|
+
with_content_for @user, "Special content", :content_tag => :b, :id => "thecontent", :class => "special"
|
16
|
+
assert_select "div.show_for b#thecontent.special.content", "Special content"
|
17
|
+
assert_no_select "div.show_for b[content_tag]"
|
18
|
+
end
|
19
|
+
|
20
|
+
test "show_for#content with blank value has a 'no value'-class" do
|
21
|
+
swap ShowFor, :blank_content_class => "nothing" do
|
22
|
+
with_content_for @user, nil, :content_tag => :b
|
23
|
+
assert_select "div.show_for b.nothing"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/test/label_test.rb
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class LabelTest < ActionView::TestCase
|
4
|
+
test "show_for shows a label using the humanized attribute name from model" do
|
5
|
+
with_attribute_for @user, :name
|
6
|
+
assert_select "div.show_for p.wrapper strong.label", "Super User Name!"
|
7
|
+
end
|
8
|
+
|
9
|
+
test "show_for skips label if requested" do
|
10
|
+
with_attribute_for @user, :name, :label => false
|
11
|
+
assert_no_select "div.show_for p.wrapper strong.label"
|
12
|
+
assert_no_select "div.show_for p.wrapper br"
|
13
|
+
end
|
14
|
+
|
15
|
+
test "show_for uses custom content_tag and skips label if requested" do
|
16
|
+
with_attribute_for @user, :name, :label => false, :content_tag => :h2
|
17
|
+
assert_select "div.show_for p.wrapper h2.content", "ShowFor"
|
18
|
+
end
|
19
|
+
|
20
|
+
test "show_for allows label to be configured globally" do
|
21
|
+
swap ShowFor, :label_tag => :span, :label_class => "my_label" do
|
22
|
+
with_attribute_for @user, :name
|
23
|
+
assert_select "div.show_for p.wrapper span.my_label"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
test "show_for allows label to be changed by attribute" do
|
28
|
+
with_attribute_for @user, :name, :label_tag => :span
|
29
|
+
assert_select "div.show_for p.wrapper span.label"
|
30
|
+
end
|
31
|
+
|
32
|
+
test "show_for allows label html to be configured by attribute" do
|
33
|
+
with_attribute_for @user, :name, :label_html => { :id => "thelabel", :class => "special" }
|
34
|
+
assert_select "div.show_for p.wrapper strong#thelabel.special.label"
|
35
|
+
end
|
36
|
+
|
37
|
+
test "show_for allows label to be set without lookup" do
|
38
|
+
with_attribute_for @user, :name, :label => "Special Label"
|
39
|
+
assert_select "div.show_for p.wrapper strong.label", "Special Label"
|
40
|
+
end
|
41
|
+
|
42
|
+
test "show_for#label accepts the text" do
|
43
|
+
with_label_for @user, "Special Label"
|
44
|
+
assert_select "div.show_for strong.label", "Special Label"
|
45
|
+
end
|
46
|
+
|
47
|
+
test "show_for#label accepts an attribute name" do
|
48
|
+
with_label_for @user, :name
|
49
|
+
assert_select "div.show_for strong.label", "Super User Name!"
|
50
|
+
end
|
51
|
+
|
52
|
+
test "show_for#label accepts html options" do
|
53
|
+
with_label_for @user, :name, :id => "thelabel", :class => "special"
|
54
|
+
assert_select "div.show_for strong#thelabel.special.label"
|
55
|
+
end
|
56
|
+
|
57
|
+
test "should let you override the label wrapper" do
|
58
|
+
swap ShowFor, :label_proc => proc { |l| l + ":" } do
|
59
|
+
with_label_for @user, "Special Label"
|
60
|
+
assert_select "div.show_for strong.label", "Special Label:"
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
test "should you skip wrapping the label on a per item basis" do
|
65
|
+
swap ShowFor, :label_proc => proc { |l| l + ":" } do
|
66
|
+
with_label_for @user, "Special Label", :wrap_label => false
|
67
|
+
assert_select "div.show_for strong.label", "Special Label"
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -8,10 +8,8 @@ module MiscHelpers
|
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
|
-
def assert_no_select(
|
12
|
-
|
13
|
-
assert_select(*args)
|
14
|
-
end
|
11
|
+
def assert_no_select(selector, value=nil)
|
12
|
+
assert_select(selector, :text => value, :count => 0)
|
15
13
|
end
|
16
14
|
|
17
15
|
def swap(object, new_values)
|
@@ -26,4 +24,40 @@ module MiscHelpers
|
|
26
24
|
object.send :"#{key}=", value
|
27
25
|
end
|
28
26
|
end
|
27
|
+
|
28
|
+
def with_attribute_for(object, attribute, options={}, &block)
|
29
|
+
concat(show_for(object) do |o|
|
30
|
+
concat o.attribute(attribute, options, &block)
|
31
|
+
end)
|
32
|
+
end
|
33
|
+
|
34
|
+
def with_value_for(object, attribute, options={}, &block)
|
35
|
+
concat(show_for(object) do |o|
|
36
|
+
concat o.value(attribute, options, &block)
|
37
|
+
end)
|
38
|
+
end
|
39
|
+
|
40
|
+
def with_association_for(object, association, options={}, &block)
|
41
|
+
concat(show_for(object) do |o|
|
42
|
+
concat o.association(association, options, &block)
|
43
|
+
end)
|
44
|
+
end
|
45
|
+
|
46
|
+
def with_label_for(object, attribute, options={})
|
47
|
+
concat(show_for(object) do |o|
|
48
|
+
concat o.label(attribute, options)
|
49
|
+
end)
|
50
|
+
end
|
51
|
+
|
52
|
+
def with_content_for(object, value, options={})
|
53
|
+
concat(show_for(object) do |o|
|
54
|
+
concat o.content(value, options)
|
55
|
+
end)
|
56
|
+
end
|
57
|
+
|
58
|
+
def with_attributes_for(object, *attributes)
|
59
|
+
concat(show_for(object) do |o|
|
60
|
+
concat o.attributes(*attributes)
|
61
|
+
end)
|
62
|
+
end
|
29
63
|
end
|
data/test/support/models.rb
CHANGED