show_for 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -10,9 +10,9 @@ ShowFor allows you to quickly show a model information with I18n features.
10
10
  <%= u.attribute :last_sign_in_at, :if_blank => "User did not access yet",
11
11
  :wrapper_html => { :id => "sign_in_timestamp" } %>
12
12
 
13
- <% u.attribute :photo do
14
- <%= image_url(@user.photo_url) %>
15
- end %>
13
+ <% u.attribute :photo do %>
14
+ <%= image_tag(@user.photo_url) %>
15
+ <% end %>
16
16
 
17
17
  <%= u.association :company %>
18
18
  <%= u.association :tags, :to_sentence => true %>
@@ -22,7 +22,7 @@ ShowFor allows you to quickly show a model information with I18n features.
22
22
 
23
23
  Install the gem:
24
24
 
25
- sudo gem install show_for --version=0.2
25
+ sudo gem install show_for
26
26
 
27
27
  Run the generator:
28
28
 
@@ -45,7 +45,7 @@ ShowFor allows you to quickly show a model information with I18n features.
45
45
  :wrapper_html => { :id => "sign_in_timestamp" } %>
46
46
 
47
47
  <% a.attribute :photo do %>
48
- <%= image_url(@admin.photo_url) %>
48
+ <%= image_tag(@admin.photo_url) %>
49
49
  <% end %>
50
50
  <% end %>
51
51
 
@@ -100,8 +100,17 @@ options. Containers can have their tags configured on demand as well through
100
100
  show_for also exposes the label method. In case you want to use the default
101
101
  human_attribute_name lookup and the default wrapping:
102
102
 
103
- a.label :name #=> <b class="label">Name</b>
104
- a.label "Name", :id => "my_name" #=> <b class="label" id="my_name">Name</b>
103
+ a.label :name #=> <strong class="label">Name</strong>
104
+ a.label "Name", :id => "my_name" #=> <strong class="label" id="my_name">Name</strong>
105
+
106
+ Optionally, if you want to wrap the inner part of the label with some text
107
+ (e.g. adding a semicolon), you can do so by specifying a proc for ShowFor.label_proc
108
+ that will be called with any label text. E.g.:
109
+
110
+ ShowFor.label_proc = lambda { |l| l + ":" }
111
+
112
+ When taking this route, you can also skip on a per label basis by passing the
113
+ :wrap_label option with a value of false.
105
114
 
106
115
  == Associations
107
116
 
@@ -30,4 +30,9 @@ ShowFor.setup do |config|
30
30
  # Whenever a association is given, the first method in association_methods
31
31
  # in which the association responds to is used to retrieve the association labels.
32
32
  # config.association_methods = [ :name, :title, :to_s ]
33
+
34
+ # If you want to wrap the text inside a label (e.g. to append a semicolon),
35
+ # specify label_proc - it will be automatically called, passing in the label text.
36
+ # config.label_proc = lambda { |l| c + ":" }
37
+
33
38
  end
@@ -24,7 +24,7 @@ module ShowFor
24
24
 
25
25
  def wrap_label_and_content(name, value, options, &block) #:nodoc:
26
26
  wrap_with(:wrapper, label(name, options, false) + ShowFor.separator.to_s.html_safe +
27
- content(value, options, false, &block), options, value.is_a?(Proc))
27
+ content(value, options, false, &block), options)
28
28
  end
29
29
 
30
30
  # Set "#{object_name}_#{attribute_name}" as in the wrapper tag.
@@ -37,7 +37,7 @@ module ShowFor
37
37
  # Gets the default tag set in ShowFor module and apply (if defined)
38
38
  # around the given content. It also check for html_options in @options
39
39
  # hash related to the current type.
40
- def wrap_with(type, content, options, concat=false) #:nodoc:
40
+ def wrap_with(type, content, options) #:nodoc:
41
41
  tag = options.delete(:"#{type}_tag") || ShowFor.send(:"#{type}_tag")
42
42
 
43
43
  html = if tag
@@ -48,7 +48,7 @@ module ShowFor
48
48
  content
49
49
  end
50
50
 
51
- concat ? @template.safe_concat(html) : html.html_safe
51
+ html.html_safe
52
52
  end
53
53
 
54
54
  # Returns true if the block is supposed to iterate through a collection,
@@ -18,7 +18,7 @@ module ShowFor
18
18
  collection_handler(value, options, &block)
19
19
  when Proc
20
20
  options[:escape] = false
21
- @template.capture(&value)
21
+ @template.capture(&value).html_safe
22
22
  when NilClass
23
23
  ""
24
24
  else
@@ -40,7 +40,7 @@ module ShowFor
40
40
  response << template.capture(item, &iterator)
41
41
  end
42
42
 
43
- wrap_with(:collection, response, options)
43
+ wrap_with(:collection, response.html_safe, options)
44
44
  end
45
45
  end
46
46
  end
@@ -16,7 +16,11 @@ module ShowFor
16
16
  html_options[:class] = "show_for #{dom_class(object)} #{html_options[:class]}".strip
17
17
  builder_class = html_options.delete(:builder) || ShowFor::Builder
18
18
 
19
- concat content_tag(tag, capture(builder_class.new(object, self), &block), html_options)
19
+ content = with_output_buffer do
20
+ yield builder_class.new(object, self)
21
+ end
22
+
23
+ content_tag(tag, content, html_options)
20
24
  end
21
25
  end
22
26
  end
@@ -11,6 +11,7 @@ module ShowFor
11
11
 
12
12
  return "" if label == false
13
13
  options[:label_html] = options.dup if apply_options
14
+ label = ShowFor.label_proc.call(label) if options.fetch(:wrap_label, true) && ShowFor.label_proc
14
15
  wrap_with :label, label, options
15
16
  end
16
17
 
@@ -1,3 +1,3 @@
1
1
  module ShowFor
2
- VERSION = "0.2.0".freeze
2
+ VERSION = "0.2.1".freeze
3
3
  end
data/lib/show_for.rb CHANGED
@@ -35,6 +35,9 @@ module ShowFor
35
35
 
36
36
  mattr_accessor :association_methods
37
37
  @@association_methods = [ :name, :title, :to_s ]
38
+
39
+ mattr_accessor :label_proc
40
+ @@label_proc = nil
38
41
 
39
42
  # Yield self for configuration block:
40
43
  #
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
- o.attribute(attribute, options, &block)
8
- end
6
+ concat(show_for(object) do |o|
7
+ concat 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
- o.association(association, options, &block)
14
- end
12
+ concat(show_for(object) do |o|
13
+ concat 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
- o.label attribute, options
20
- end
18
+ concat(show_for(object) do |o|
19
+ concat 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
- o.content value, options
26
- end
24
+ concat(show_for(object) do |o|
25
+ concat o.content(value, options)
26
+ end)
27
27
  end
28
28
 
29
29
  # WRAPPER
@@ -154,6 +154,20 @@ class BuilderTest < ActionView::TestCase
154
154
  assert_select "div.show_for p.wrapper", /Hell yeah!/
155
155
  end
156
156
  end
157
+
158
+ test "should let you override the label wrapper" do
159
+ swap ShowFor, :label_proc => proc { |l| l + ":" } do
160
+ with_label_for @user, "Special Label"
161
+ assert_select "div.show_for strong.label", "Special Label:"
162
+ end
163
+ end
164
+
165
+ test "should you skip wrapping the label on a per item basis" do
166
+ swap ShowFor, :label_proc => proc { |l| l + ":" } do
167
+ with_label_for @user, "Special Label", :wrap_label => false
168
+ assert_select "div.show_for strong.label", "Special Label"
169
+ end
170
+ end
157
171
 
158
172
  test "show_for accepts an attribute as false" do
159
173
  with_attribute_for @user, :invalid
data/test/helper_test.rb CHANGED
@@ -17,23 +17,23 @@ class HelperTest < ActionView::TestCase
17
17
  end
18
18
 
19
19
  test "show for should add default class to form" do
20
- show_for(@user) do |f| end
20
+ concat(show_for(@user) do |f| end)
21
21
  assert_select "div.show_for"
22
22
  end
23
23
 
24
24
  test "show for should add object class name as css class to form" do
25
- show_for(@user) do |f| end
25
+ concat(show_for(@user) do |f| end)
26
26
  assert_select "div.show_for.user"
27
27
  end
28
28
 
29
29
  test "show for should pass options" do
30
- show_for(@user, :id => "my_div", :class => "common") do |f| end
30
+ concat(show_for(@user, :id => "my_div", :class => "common") do |f| end)
31
31
  assert_select "div#my_div.show_for.user.common"
32
32
  end
33
33
 
34
34
  test "show for tag should be configurable" do
35
35
  swap ShowFor, :show_for_tag => :p do
36
- show_for(@user) do |f| end
36
+ concat(show_for(@user) do |f| end)
37
37
  assert_select "p.show_for"
38
38
  end
39
39
  end
data/test/test_helper.rb CHANGED
@@ -3,6 +3,8 @@ require 'test/unit'
3
3
 
4
4
  require 'active_model'
5
5
  require 'action_controller'
6
+ require 'action_view'
7
+ require 'action_view/template'
6
8
  require 'action_view/test_case'
7
9
 
8
10
  begin
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: show_for
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 2
8
+ - 1
9
+ version: 0.2.1
5
10
  platform: ruby
6
11
  authors:
7
12
  - "Jos\xC3\xA9 Valim"
@@ -9,7 +14,7 @@ autorequire:
9
14
  bindir: bin
10
15
  cert_chain: []
11
16
 
12
- date: 2010-02-07 00:00:00 +01:00
17
+ date: 2010-04-03 00:00:00 +02:00
13
18
  default_executable:
14
19
  dependencies: []
15
20
 
@@ -50,18 +55,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
50
55
  requirements:
51
56
  - - ">="
52
57
  - !ruby/object:Gem::Version
58
+ segments:
59
+ - 0
53
60
  version: "0"
54
- version:
55
61
  required_rubygems_version: !ruby/object:Gem::Requirement
56
62
  requirements:
57
63
  - - ">="
58
64
  - !ruby/object:Gem::Version
65
+ segments:
66
+ - 0
59
67
  version: "0"
60
- version:
61
68
  requirements: []
62
69
 
63
70
  rubyforge_project:
64
- rubygems_version: 1.3.5
71
+ rubygems_version: 1.3.6
65
72
  signing_key:
66
73
  specification_version: 3
67
74
  summary: Wrap your objects with a helper to easily show them