show_for 0.1 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -137,10 +137,14 @@ You can also pass a block which expects an argument to association. In such case
137
137
  a wrapper for the collection is still created and the block just iterates over the
138
138
  collection objects.
139
139
 
140
- == Contributors
140
+ == Maintainers
141
141
 
142
142
  * José Valim (http://github.com/josevalim)
143
143
 
144
+ == Contributors
145
+
146
+ * Jonas Grimfelt (http://github.com/grimen)
147
+
144
148
  == Bugs and Feedback
145
149
 
146
150
  If you discover any bugs or want to drop a line, feel free to create an issue on GitHub.
@@ -12,6 +12,9 @@ ShowFor.setup do |config|
12
12
  # The tag used to wrap each content (value). Default is nil.
13
13
  # config.content_tag = :dd
14
14
 
15
+ # The DOM class set for blank content tags. Default is "blank".
16
+ # config.blank_content_class = 'no_content'
17
+
15
18
  # The separator between label and content. Default is "<br />".
16
19
  # config.separator = "<br />"
17
20
 
@@ -1,7 +1,10 @@
1
1
  module ShowFor
2
2
  module Content
3
3
  def content(value, options={}, apply_options=true, &block)
4
- value = options.delete(:if_blank) if value.blank? && value != false
4
+ if value.blank? && value != false
5
+ value = options.delete(:if_blank) || I18n.t(:'show_for.blank', :default => "Not specified")
6
+ options[:class] = [options[:class], ShowFor.blank_content_class].join(' ')
7
+ end
5
8
 
6
9
  content = case value
7
10
  when Date, Time, DateTime
@@ -15,9 +15,7 @@ module ShowFor
15
15
  html_options[:id] ||= dom_id(object)
16
16
  html_options[:class] = "show_for #{dom_class(object)} #{html_options[:class]}".strip
17
17
 
18
- concat(content_tag(tag, html_options) do
19
- yield ShowFor::Builder.new(object, self)
20
- end)
18
+ content_tag(tag, yield(ShowFor::Builder.new(object, self)), html_options)
21
19
  end
22
20
  end
23
21
  end
@@ -1,4 +1,5 @@
1
1
  en:
2
2
  show_for:
3
- "yes": 'Yes'
4
- "no": 'No'
3
+ blank: "Not specified"
4
+ "yes": "Yes"
5
+ "no": "No""
@@ -1,3 +1,3 @@
1
1
  module ShowFor
2
- VERSION = "0.1".freeze
2
+ VERSION = "0.1.1".freeze
3
3
  end
data/lib/show_for.rb CHANGED
@@ -15,6 +15,12 @@ module ShowFor
15
15
  mattr_accessor :content_tag
16
16
  @@content_tag = nil
17
17
 
18
+ mattr_accessor :blank_content_class
19
+ @@blank_content_class = "blank"
20
+
21
+ mattr_accessor :blank_content
22
+ @@blank_content = ""
23
+
18
24
  mattr_accessor :wrapper_tag
19
25
  @@wrapper_tag = :p
20
26
 
data/test/builder_test.rb CHANGED
@@ -3,27 +3,27 @@ require 'test_helper'
3
3
  class BuilderTest < ActionView::TestCase
4
4
 
5
5
  def with_attribute_for(object, attribute, options={}, &block)
6
- show_for object do |o|
7
- concat(o.attribute(attribute, options, &block))
8
- end
6
+ concat(show_for(object) do |o|
7
+ o.attribute(attribute, options, &block)
8
+ end)
9
9
  end
10
10
 
11
11
  def with_association_for(object, association, options={}, &block)
12
- show_for object do |o|
13
- concat(o.association(association, options, &block))
14
- end
12
+ concat(show_for(object) do |o|
13
+ o.association(association, options, &block)
14
+ end)
15
15
  end
16
16
 
17
17
  def with_label_for(object, attribute, options={})
18
- show_for object do |o|
19
- concat o.label attribute, options
20
- end
18
+ concat(show_for(object) do |o|
19
+ o.label attribute, options
20
+ end)
21
21
  end
22
22
 
23
23
  def with_content_for(object, value, options={})
24
- show_for object do |o|
25
- concat o.content value, options
26
- end
24
+ concat(show_for(object) do |o|
25
+ o.content value, options
26
+ end)
27
27
  end
28
28
 
29
29
  # WRAPPER
@@ -169,7 +169,14 @@ class BuilderTest < ActionView::TestCase
169
169
 
170
170
  test "show_for accepts nil and or blank attributes" do
171
171
  with_attribute_for @user, :description
172
- assert_select "div.show_for p.wrapper", "Description"
172
+ assert_select "div.show_for p.wrapper", /Not specified/
173
+ end
174
+
175
+ test "show_for accepts not spcified message can be localized" do
176
+ store_translations(:en, :show_for => { :blank => "OMG! It's blank!" }) do
177
+ with_attribute_for @user, :description
178
+ assert_select "div.show_for p.wrapper", /OMG! It's blank!/
179
+ end
173
180
  end
174
181
 
175
182
  test "show_for uses :if_blank if attribute is blank" do
@@ -211,6 +218,20 @@ class BuilderTest < ActionView::TestCase
211
218
  with_content_for @user, "Special content", :content_tag => :b, :id => "thecontent", :class => "special"
212
219
  assert_select "div.show_for b#thecontent.special.content", "Special content"
213
220
  end
221
+
222
+ test "show_for#content with blank value has a 'no value'-class" do
223
+ swap ShowFor, :blank_content_class => "nothing" do
224
+ with_content_for @user, nil, :content_tag => :b
225
+ assert_select "div.show_for b.nothing"
226
+ end
227
+ end
228
+
229
+ test "show_for#content with blank value fallbacks on a default value" do
230
+ swap ShowFor, :blank_content => "Not specified" do
231
+ with_content_for @user, nil, :content_tag => :b
232
+ assert_select "div.show_for b", "Not specified"
233
+ end
234
+ end
214
235
 
215
236
  # COLLECTIONS
216
237
  test "show_for accepts an attribute as a collection" do
data/test/helper_test.rb CHANGED
@@ -2,29 +2,29 @@ require "test_helper"
2
2
 
3
3
  class HelperTest < ActionView::TestCase
4
4
  test "show for yields an instance of ShowFor::Builder" do
5
- show_for @user do |f|
5
+ concat(show_for(@user) do |f|
6
6
  assert f.instance_of?(ShowFor::Builder)
7
- end
7
+ end)
8
8
  end
9
9
 
10
10
  test "show for should add default class to form" do
11
- show_for @user do |f| end
11
+ concat(show_for(@user) do |f| end)
12
12
  assert_select "div.show_for"
13
13
  end
14
14
 
15
15
  test "show for should add object class name as css class to form" do
16
- show_for @user do |f| end
16
+ concat(show_for(@user) do |f| end)
17
17
  assert_select "div.show_for.user"
18
18
  end
19
19
 
20
20
  test "show for should pass options" do
21
- show_for @user, :id => "my_div", :class => "common" do |f| end
21
+ concat(show_for(@user, :id => "my_div", :class => "common") do |f| end)
22
22
  assert_select "div#my_div.show_for.user.common"
23
23
  end
24
24
 
25
25
  test "show for tag should be configurable" do
26
26
  swap ShowFor, :show_for_tag => :p do
27
- show_for @user do |f| end
27
+ concat(show_for(@user) do |f| end)
28
28
  assert_select "p.show_for"
29
29
  end
30
30
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: show_for
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.1"
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - "Jos\xC3\xA9 Valim"
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-08 00:00:00 +01:00
12
+ date: 2010-01-09 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies: []
15
15