show_for 0.3.0 → 0.4.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.
@@ -1,13 +1,12 @@
1
1
  module ShowFor
2
2
  module Content
3
3
  def content(value, options={}, apply_options=true, &block)
4
-
5
4
  # cache value for apply_wrapper_options!
6
5
  sample_value = value
7
6
 
8
7
  # We need to convert value to_a because when dealing with ActiveRecord
9
8
  # Array proxies, the follow statement Array# === value return false
10
- value = value.to_a if value.is_a?(Array)
9
+ value = value.to_a if value.respond_to?(:to_ary)
11
10
 
12
11
  content = case value
13
12
  when Date, Time, DateTime
@@ -17,25 +16,23 @@ module ShowFor
17
16
  when FalseClass
18
17
  I18n.t :"show_for.no", :default => "No"
19
18
  when Array, Hash
20
- collection_handler(value, options, &block)
19
+ collection_handler(value, options, &block) unless value.empty?
21
20
  when Proc
22
21
  @template.capture(&value)
23
- when NilClass, Numeric
22
+ when Numeric
24
23
  value.to_s
25
24
  else
26
- if block
27
- template.capture(value, &block)
28
- else
29
- value
25
+ unless value.blank?
26
+ block ? template.capture(value, &block) : value
30
27
  end
31
28
  end
32
29
 
33
- if content.blank?
30
+ if content.blank? && !ShowFor.skip_blanks
34
31
  content = blank_value(options)
35
32
  end
36
33
 
37
34
  options[:content_html] = options.except(:content_tag) if apply_options
38
- wrap_with(:content, content, apply_wrapper_options!(:content, options, sample_value) )
35
+ wrap_with(:content, content, apply_wrapper_options!(:content, options, sample_value))
39
36
  end
40
37
 
41
38
  protected
@@ -1,3 +1,3 @@
1
1
  module ShowFor
2
- VERSION = "0.3.0".freeze
2
+ VERSION = "0.4.0".freeze
3
3
  end
@@ -3,31 +3,31 @@ require 'test_helper'
3
3
  class AssociationTest < ActionView::TestCase
4
4
  test "show_for works with belongs_to/has_one associations" do
5
5
  with_association_for @user, :company
6
- assert_select "div.show_for p.wrapper", /PlataformaTec/
6
+ assert_select "div.show_for div.wrapper", /Plataformatec/
7
7
  end
8
8
 
9
9
  test "show_for accepts :using as option to tell how to retrieve association value" do
10
10
  with_association_for @user, :company, :using => :alternate_name
11
- assert_select "div.show_for p.wrapper", /Alternate PlataformaTec/
11
+ assert_select "div.show_for div.wrapper", /Alternate Plataformatec/
12
12
  end
13
13
 
14
14
  test "show_for accepts :in to tell to retrieve an attribute from association" do
15
15
  with_attribute_for @user, :alternate_name, :in => :company
16
- assert_select "div.show_for p.wrapper", /Alternate PlataformaTec/
16
+ assert_select "div.show_for div.wrapper", /Alternate Plataformatec/
17
17
  end
18
18
 
19
19
  test "show_for forwards all options send with :in to association" do
20
20
  with_attribute_for @user, :alternate_name, :in => :tags, :to_sentence => true
21
- assert_no_select "div.show_for p.wrapper ul.collection"
22
- assert_select "div.show_for p.wrapper", /Alternate Tag 1, Alternate Tag 2, and Alternate Tag 3/
21
+ assert_no_select "div.show_for div.wrapper ul.collection"
22
+ assert_select "div.show_for div.wrapper", /Alternate Tag 1, Alternate Tag 2, and Alternate Tag 3/
23
23
  end
24
24
 
25
25
  test "show_for works with has_many/has_and_belongs_to_many associations" do
26
26
  with_association_for @user, :tags
27
- assert_select "div.show_for p.wrapper ul.collection"
28
- assert_select "div.show_for p.wrapper ul.collection li", "Tag 1"
29
- assert_select "div.show_for p.wrapper ul.collection li", "Tag 2"
30
- assert_select "div.show_for p.wrapper ul.collection li", "Tag 3"
27
+ assert_select "div.show_for div.wrapper ul.collection"
28
+ assert_select "div.show_for div.wrapper ul.collection li", "Tag 1"
29
+ assert_select "div.show_for div.wrapper ul.collection li", "Tag 2"
30
+ assert_select "div.show_for div.wrapper ul.collection li", "Tag 3"
31
31
  end
32
32
 
33
33
  test "show_for works with has_many/has_and_belongs_to_many blank associations" do
@@ -37,45 +37,83 @@ class AssociationTest < ActionView::TestCase
37
37
 
38
38
  swap ShowFor, :association_methods => [:name] do
39
39
  with_association_for @user, :tags
40
- assert_no_select "div.show_for p.wrapper ul.collection"
41
- assert_no_select "div.show_for p.wrapper", /Enumerator/
40
+ assert_no_select "div.show_for div.wrapper ul.collection"
41
+ assert_no_select "div.show_for div.wrapper", /Enumerator/
42
42
  end
43
43
  end
44
44
 
45
+ test "show_for accepts a block with has_many/has_and_belongs_to_many blank associations" do
46
+ def @user.tags
47
+ []
48
+ end
49
+
50
+ swap ShowFor, :association_methods => [:name] do
51
+ with_association_for @user, :tags do |tag|
52
+ tag.name
53
+ end
54
+ assert_no_select "div.show_for div.wrapper ul.collection"
55
+ assert_no_select "div.show_for div.wrapper", /Enumerator/
56
+ end
57
+ end
58
+
59
+ test "show_for uses :if_blank when has_many/has_and_belongs_to_many association is blank" do
60
+ def @user.tags
61
+ []
62
+ end
63
+
64
+ with_association_for @user, :tags, if_blank: 'No tags' do |tag|
65
+ tag.name
66
+ end
67
+ assert_select "div.show_for div.wrapper.blank", /No tags/
68
+ assert_no_select "div.show_for div.wrapper ul.collection"
69
+ assert_no_select "div.show_for div.wrapper", /Enumerator/
70
+ end
71
+
72
+ test "show_for uses :if_blank if given a block when has_many/has_and_belongs_to_many association is blank" do
73
+ def @user.tags
74
+ []
75
+ end
76
+
77
+ with_association_for @user, :tags, if_blank: 'No tags'
78
+ assert_select "div.show_for div.wrapper.blank", /No tags/
79
+ assert_no_select "div.show_for div.wrapper ul.collection"
80
+ assert_no_select "div.show_for div.wrapper", /Enumerator/
81
+ end
82
+
45
83
  test "show_for accepts a block with an argument in belongs_to associations" do
46
84
  with_association_for @user, :company do |company|
47
85
  company.name.upcase
48
86
  end
49
87
 
50
- assert_select "div.show_for p.wrapper", /PLATAFORMATEC/
88
+ assert_select "div.show_for div.wrapper", /PLATAFORMATEC/
51
89
  end
52
90
 
53
91
  test "show_for accepts :using as option to tell how to retrieve association values" do
54
92
  with_association_for @user, :tags, :using => :alternate_name
55
- assert_select "div.show_for p.wrapper ul.collection"
56
- assert_select "div.show_for p.wrapper ul.collection li", "Alternate Tag 1"
57
- assert_select "div.show_for p.wrapper ul.collection li", "Alternate Tag 2"
58
- assert_select "div.show_for p.wrapper ul.collection li", "Alternate Tag 3"
93
+ assert_select "div.show_for div.wrapper ul.collection"
94
+ assert_select "div.show_for div.wrapper ul.collection li", "Alternate Tag 1"
95
+ assert_select "div.show_for div.wrapper ul.collection li", "Alternate Tag 2"
96
+ assert_select "div.show_for div.wrapper ul.collection li", "Alternate Tag 3"
59
97
  end
60
98
 
61
99
  test "show_for accepts :to_sentence as option in collection associations" do
62
100
  with_association_for @user, :tags, :to_sentence => true
63
- assert_no_select "div.show_for p.wrapper ul.collection"
64
- assert_select "div.show_for p.wrapper", /Tag 1, Tag 2, and Tag 3/
101
+ assert_no_select "div.show_for div.wrapper ul.collection"
102
+ assert_select "div.show_for div.wrapper", /Tag 1, Tag 2, and Tag 3/
65
103
  end
66
104
 
67
105
  test "show_for accepts :join as option in collection associations" do
68
106
  with_association_for @user, :tags, :join => ", "
69
- assert_no_select "div.show_for p.wrapper ul.collection"
70
- assert_select "div.show_for p.wrapper", /Tag 1, Tag 2, Tag 3/
107
+ assert_no_select "div.show_for div.wrapper ul.collection"
108
+ assert_select "div.show_for div.wrapper", /Tag 1, Tag 2, Tag 3/
71
109
  end
72
110
 
73
111
  test "show_for accepts a block without argument in collection associations" do
74
112
  with_association_for @user, :tags do
75
113
  @user.tags.map(&:name).to_sentence
76
114
  end
77
- assert_no_select "div.show_for p.wrapper ul.collection"
78
- assert_select "div.show_for p.wrapper", /Tag 1, Tag 2, and Tag 3/
115
+ assert_no_select "div.show_for div.wrapper ul.collection"
116
+ assert_select "div.show_for div.wrapper", /Tag 1, Tag 2, and Tag 3/
79
117
  end
80
118
 
81
119
  test "show_for accepts a block with argument in collection associations" do
@@ -83,10 +121,32 @@ class AssociationTest < ActionView::TestCase
83
121
  assert_kind_of Tag, tag
84
122
  content_tag(:span, tag.name)
85
123
  end
86
- assert_no_select "div.show_for p.wrapper ul.collection"
87
- assert_select "div.show_for p.wrapper p.collection"
88
- assert_select "div.show_for p.wrapper p.collection span", "Tag 1"
89
- assert_select "div.show_for p.wrapper p.collection span", "Tag 2"
90
- assert_select "div.show_for p.wrapper p.collection span", "Tag 3"
124
+ assert_no_select "div.show_for div.wrapper ul.collection"
125
+ assert_select "div.show_for div.wrapper p.collection"
126
+ assert_select "div.show_for div.wrapper p.collection span", "Tag 1"
127
+ assert_select "div.show_for div.wrapper p.collection span", "Tag 2"
128
+ assert_select "div.show_for div.wrapper p.collection span", "Tag 3"
129
+ end
130
+
131
+ test "show_for does not display empty has_many/has_and_belongs_to_many association if skip_blanks option is passed" do
132
+ def @user.tags
133
+ []
134
+ end
135
+
136
+ swap ShowFor, :skip_blanks => true do
137
+ with_association_for @user, :tags
138
+ assert_no_select "div.show_for div.wrapper"
139
+ end
140
+ end
141
+
142
+ test "show_for does not display empty belongs_to/has_one association if skip_blanks option is passed" do
143
+ def @user.company
144
+ nil
145
+ end
146
+
147
+ swap ShowFor, :skip_blanks => true do
148
+ with_association_for @user, :company
149
+ assert_no_select "div.show_for div.wrapper"
150
+ end
91
151
  end
92
152
  end
@@ -4,191 +4,206 @@ class AttributeTest < ActionView::TestCase
4
4
  # COLLECTIONS
5
5
  test "show_for accepts an attribute as a collection" do
6
6
  with_attribute_for @user, :scopes
7
- assert_select "div.show_for p.wrapper ul.collection"
8
- assert_select "div.show_for p.wrapper ul.collection li", :count => 3
7
+ assert_select "div.show_for div.wrapper ul.collection"
8
+ assert_select "div.show_for div.wrapper ul.collection li", :count => 3
9
9
  end
10
10
 
11
11
  test "show_for accepts an attribute as a collection with a block to iterate the collection" do
12
12
  with_attribute_for @user, :scopes do |scope|
13
13
  content_tag :span, scope
14
14
  end
15
- assert_select "div.show_for p.wrapper ul.collection"
16
- assert_select "div.show_for p.wrapper ul.collection span", :count => 3
15
+ assert_select "div.show_for div.wrapper ul.collection"
16
+ assert_select "div.show_for div.wrapper ul.collection span", :count => 3
17
17
  end
18
18
 
19
19
  test "show_for treats symbol for :value as method on each element of collection" do
20
20
  with_attribute_for @user, :scopes, :value => :upcase
21
21
  @user.scopes.each do |scope|
22
- assert_select "div.show_for p.wrapper ul.collection", /#{scope.upcase}/
22
+ assert_select "div.show_for div.wrapper ul.collection", /#{scope.upcase}/
23
23
  end
24
24
  end
25
25
 
26
26
  test "show_for allows collection tag to be configured globally" do
27
27
  swap ShowFor, :collection_tag => :ol, :collection_class => "my_collection" do
28
28
  with_attribute_for @user, :scopes
29
- assert_select "div.show_for p.wrapper ol.my_collection"
29
+ assert_select "div.show_for div.wrapper ol.my_collection"
30
30
  end
31
31
  end
32
32
 
33
33
  test "show_for allows collection tag to be changed by attribute" do
34
34
  with_attribute_for @user, :scopes, :collection_tag => :ol
35
- assert_select "div.show_for p.wrapper ol.collection"
35
+ assert_select "div.show_for div.wrapper ol.collection"
36
36
  end
37
37
 
38
38
  test "show_for allows collection tag html to be configured by attribute" do
39
39
  with_attribute_for @user, :scopes, :collection_html => { :id => "thecollection", :class => "special" }
40
- assert_select "div.show_for p.wrapper ul#thecollection.special.collection"
40
+ assert_select "div.show_for div.wrapper ul#thecollection.special.collection"
41
41
  end
42
42
 
43
43
  # CONTENT
44
44
  test "show_for allows content tag to be configured globally" do
45
45
  swap ShowFor, :content_tag => :span, :content_class => :my_content do
46
46
  with_attribute_for @user, :name
47
- assert_select "div.show_for p.wrapper span.my_content"
47
+ assert_select "div.show_for div.wrapper span.my_content"
48
48
  end
49
49
  end
50
50
 
51
51
  test "show_for allows content tag to be changed by attribute" do
52
52
  with_attribute_for @user, :name, :content_tag => :span
53
- assert_select "div.show_for p.wrapper span.content"
53
+ assert_select "div.show_for div.wrapper span.content"
54
54
  end
55
55
 
56
56
  test "show_for allows content tag html to be configured by attribute" do
57
57
  with_attribute_for @user, :name, :content_tag => :span, :content_html => { :id => "thecontent", :class => "special" }
58
- assert_select "div.show_for p.wrapper span#thecontent.special.content"
58
+ assert_select "div.show_for div.wrapper span#thecontent.special.content"
59
59
  end
60
60
 
61
61
  test "show_for accepts an attribute as string" do
62
62
  with_attribute_for @user, :name
63
- assert_select "div.show_for p.wrapper", /ShowFor/
63
+ assert_select "div.show_for div.wrapper", /ShowFor/
64
64
  end
65
65
 
66
66
  test "show_for accepts an attribute as time" do
67
67
  with_attribute_for @user, :created_at
68
- assert_select "div.show_for p.wrapper", /#{Regexp.escape(I18n.l(@user.created_at))}/
68
+ assert_select "div.show_for div.wrapper", /#{Regexp.escape(I18n.l(@user.created_at))}/
69
69
  end
70
70
 
71
71
  test "show_for accepts an attribute as date" do
72
72
  with_attribute_for @user, :updated_at
73
- assert_select "div.show_for p.wrapper", /#{Regexp.escape(I18n.l(@user.updated_at))}/
73
+ assert_select "div.show_for div.wrapper", /#{Regexp.escape(I18n.l(@user.updated_at))}/
74
74
  end
75
75
 
76
76
  test "show_for accepts an attribute as time with format options" do
77
77
  with_attribute_for @user, :created_at, :format => :long
78
- assert_select "div.show_for p.wrapper", /#{Regexp.escape(I18n.l(@user.created_at, :format => :long))}/
78
+ assert_select "div.show_for div.wrapper", /#{Regexp.escape(I18n.l(@user.created_at, :format => :long))}/
79
79
  end
80
80
 
81
81
  test "show_for accepts an attribute as true" do
82
82
  with_attribute_for @user, :active
83
- assert_select "div.show_for p.wrapper", /Yes/
83
+ assert_select "div.show_for div.wrapper", /Yes/
84
84
  end
85
85
 
86
86
  test "show_for accepts an attribute as true which can be localized" do
87
87
  store_translations(:en, :show_for => { :yes => "Hell yeah!" }) do
88
88
  with_attribute_for @user, :active
89
- assert_select "div.show_for p.wrapper", /Hell yeah!/
89
+ assert_select "div.show_for div.wrapper", /Hell yeah!/
90
90
  end
91
91
  end
92
92
 
93
93
  test "show_for accepts an attribute as false" do
94
94
  with_attribute_for @user, :invalid
95
- assert_select "div.show_for p.wrapper", /No/
95
+ assert_select "div.show_for div.wrapper", /No/
96
96
  end
97
97
 
98
98
  test "show_for accepts an attribute as false which can be localized" do
99
99
  store_translations(:en, :show_for => { :no => "Hell no!" }) do
100
100
  with_attribute_for @user, :invalid
101
- assert_select "div.show_for p.wrapper", /Hell no!/
101
+ assert_select "div.show_for div.wrapper", /Hell no!/
102
102
  end
103
103
  end
104
104
 
105
105
  test "show_for accepts nil and or blank attributes" do
106
106
  with_attribute_for @user, :description
107
- assert_select "div.show_for p.wrapper", /Not specified/
107
+ assert_select "div.show_for div.wrapper", /Not specified/
108
108
  end
109
109
 
110
110
  test "show_for accepts not spcified message can be localized" do
111
111
  store_translations(:en, :show_for => { :blank => "OMG! It's blank!" }) do
112
112
  with_attribute_for @user, :description
113
- assert_select "div.show_for p.wrapper", /OMG! It's blank!/
113
+ assert_select "div.show_for div.wrapper", /OMG! It's blank!/
114
114
  end
115
115
  end
116
116
 
117
117
  test "show_for accepts not spcified message can be localized with html" do
118
118
  store_translations(:en, :show_for => { :blank_html => "<span>OMG! It's blank!</span>" }) do
119
119
  with_attribute_for @user, :description
120
- assert_select "div.show_for p.wrapper span", "OMG! It's blank!"
120
+ assert_select "div.show_for div.wrapper span", "OMG! It's blank!"
121
121
  end
122
122
  end
123
123
 
124
124
  test "show_for uses :if_blank if attribute is blank" do
125
125
  with_attribute_for @user, :description, :if_blank => "No description provided"
126
- assert_select "div.show_for p.wrapper", /No description provided/
126
+ assert_select "div.show_for div.wrapper", /No description provided/
127
127
  end
128
128
 
129
129
  test "show_for accepts a block to supply the content" do
130
130
  with_attribute_for @user, :description do
131
131
  "This description is not blank"
132
132
  end
133
- assert_select "div.show_for p.wrapper", /This description/
133
+ assert_select "div.show_for div.wrapper", /This description/
134
134
  end
135
135
 
136
136
  test "show_for uses :if_blank if the block content is blank" do
137
137
  with_attribute_for @user, :description, :if_blank => "No description provided" do
138
138
  ""
139
139
  end
140
- assert_select "div.show_for p.wrapper", /No description provided/
140
+ assert_select "div.show_for div.wrapper", /No description provided/
141
141
  end
142
142
 
143
143
  test "show_for#content given a block should be wrapped in the result" do
144
144
  with_attribute_for @user, :name do |name|
145
145
  "<div class='block'>#{name}</div>".html_safe
146
146
  end
147
- assert_select "p.wrapper.user_name div.block", /ShowFor/
147
+ assert_select "div.wrapper.user_name div.block", /ShowFor/
148
148
  end
149
149
 
150
150
  test "show_for escapes content by default" do
151
151
  @user.name = "<b>hack you!</b>"
152
152
  with_attribute_for @user, :name
153
- assert_no_select "div.show_for p.wrapper b"
154
- assert_select "div.show_for p.wrapper", /&lt;b&gt;/
153
+ assert_no_select "div.show_for div.wrapper b"
154
+ assert_select "div.show_for div.wrapper",
155
+ rails_42? ? "Super User Name!<b>hack you!</b>" : "Super User Name!&lt;b&gt;hack you!&lt;/b&gt;"
155
156
  end
156
157
 
157
158
  test "show_for works with html_safe marked strings" do
158
159
  @user.name = "<b>hack you!</b>".html_safe
159
160
  with_attribute_for @user, :name
160
- assert_select "div.show_for p.wrapper b", "hack you!"
161
+ assert_select "div.show_for div.wrapper b", "hack you!"
161
162
  end
162
163
 
163
164
  test "show_for uses :value if supplied" do
164
165
  with_attribute_for @user, :name, :value => "Calculated Value"
165
- assert_select "div.show_for p.wrapper", /Calculated Value/
166
+ assert_select "div.show_for div.wrapper", /Calculated Value/
166
167
  end
167
168
 
168
169
  test "show_for uses :value and casts to string if supplied" do
169
170
  with_attribute_for @user, :name, :value => 123
170
- assert_select "div.show_for p.wrapper", /123/
171
+ assert_select "div.show_for div.wrapper", /123/
171
172
  end
172
173
 
173
174
  test "show_for ignores :value if a block is supplied" do
174
175
  with_attribute_for @user, :name, :value => "Calculated Value" do
175
176
  @user.name.upcase
176
177
  end
177
- assert_select "div.show_for p.wrapper", /#{@user.name.upcase}/
178
+ assert_select "div.show_for div.wrapper", /#{@user.name.upcase}/
178
179
  end
179
180
 
180
181
  test "show_for treats symbol for :value as method on attribute" do
181
182
  with_attribute_for @user, :name, :value => :upcase
182
- assert_select "div.show_for p.wrapper", /#{@user.name.upcase}/
183
+ assert_select "div.show_for div.wrapper", /#{@user.name.upcase}/
184
+ end
185
+
186
+ test "show_for does not display blank attribute if skip_blanks option is passed" do
187
+ swap ShowFor, :skip_blanks => true do
188
+ with_attribute_for @user, :description
189
+ assert_no_select "div.show_for div.wrapper"
190
+ end
191
+ end
192
+
193
+ test "show_for display false attribute if skip_blanks option is passed" do
194
+ swap ShowFor, :skip_blanks => true do
195
+ with_attribute_for @user, :invalid
196
+ assert_select "div.show_for div.wrapper", /No/
197
+ end
183
198
  end
184
199
 
185
200
  # ATTRIBUTES
186
201
  test "show_for attributes wraps each attribute with a label and content" do
187
202
  with_attributes_for @user, :name, :email
188
- assert_select "div.show_for p.user_name.wrapper", /ShowFor/
189
- assert_select "p.user_name strong.label", "Super User Name!"
190
- assert_select "div.show_for p.user_email.wrapper", /Not specified/
191
- assert_select "p.user_email strong.label", "Email"
203
+ assert_select "div.show_for div.user_name.wrapper", /ShowFor/
204
+ assert_select "div.user_name strong.label", "Super User Name!"
205
+ assert_select "div.show_for div.user_email.wrapper", /Not specified/
206
+ assert_select "div.user_email strong.label", "Email"
192
207
  end
193
208
 
194
209
  test "show_for should wrap blank attributes with no_attribute" do