show_for 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -4,6 +4,7 @@ ShowFor allows you to quickly show a model information with I18n features.
4
4
 
5
5
  <% show_for @user do |u| %>
6
6
  <%= u.attribute :name %>
7
+ <%= u.attribute :nickname, :in => :profile %>
7
8
  <%= u.attribute :confirmed? %>
8
9
  <%= u.attribute :created_at, :format => :short %>
9
10
  <%= u.attribute :last_sign_in_at, :if_blank => "User did not access yet"
@@ -109,7 +110,8 @@ show_for also supports associations.
109
110
 
110
111
  <% show_for @artwork do |a| %>
111
112
  <%= a.association :artist %>
112
- <%= a.association :artist, :method => :name_with_title %>
113
+ <%= a.association :artist, :using => :name_with_title %>
114
+ <%= a.attribute :name_with_title, :in => :artist %>
113
115
 
114
116
  <%= a.association :tags %>
115
117
  <%= a.association :tags, :to_sentence => true %>
@@ -125,7 +127,7 @@ show_for also supports associations.
125
127
  The first is a has_one or belongs_to association, which works like an attribute
126
128
  to show_for, except it will retrieve the artist association and try to find a
127
129
  proper method from ShowFor.association_methods to be used. You can pass
128
- the option :method to tell (and not guess) which method from the association
130
+ the option :using to tell (and not guess) which method from the association
129
131
  to use.
130
132
 
131
133
  :tags is a has_and_belongs_to_many association which will return a collection.
@@ -0,0 +1,52 @@
1
+ module ShowFor
2
+ module Association
3
+ def attribute(attribute_name, options={}, &block)
4
+ if association_name = options.delete(:in)
5
+ options[:using] = attribute_name
6
+ association(association_name, options, &block)
7
+ else
8
+ super
9
+ end
10
+ end
11
+
12
+ def association(association_name, options={}, &block)
13
+ apply_default_options!(association_name, options)
14
+
15
+ # If a block with an iterator was given, no need to calculate the labels
16
+ # since we want the collection to be yielded. Otherwise, calculate the values.
17
+ value = if collection_block?(block)
18
+ collection_block = block
19
+ @object.send(association_name)
20
+ elsif block
21
+ block
22
+ else
23
+ association = @object.send(association_name)
24
+ values = retrieve_values_from_association(association, options)
25
+
26
+ if options.delete(:to_sentence)
27
+ values.to_sentence
28
+ elsif joiner = options.delete(:join)
29
+ values.join(joiner)
30
+ else
31
+ values
32
+ end
33
+ end
34
+
35
+ wrap_label_and_content(association_name, value, options, &collection_block)
36
+ end
37
+
38
+ protected
39
+
40
+ def retrieve_values_from_association(association, options) #:nodoc:
41
+ sample = association.is_a?(Array) ? association.first : association
42
+
43
+ if options[:method]
44
+ options[:using] = options.delete(:method)
45
+ ActiveSupport::Deprecation.warn ":method is deprecated. Please use :using instead", caller
46
+ end
47
+
48
+ method = options.delete(:using) || ShowFor.association_methods.find { |m| sample.respond_to?(m) }
49
+ association.is_a?(Array) ? association.map(&method) : association.try(method)
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,18 @@
1
+ module ShowFor
2
+ module Attribute
3
+ def attribute(attribute_name, options={}, &block)
4
+ apply_default_options!(attribute_name, options)
5
+ collection_block, block = block, nil if collection_block?(block)
6
+
7
+ value = if block
8
+ block
9
+ elsif @object.respond_to?(:"human_#{attribute_name}")
10
+ @object.send :"human_#{attribute_name}"
11
+ else
12
+ @object.send(attribute_name)
13
+ end
14
+
15
+ wrap_label_and_content(attribute_name, value, options, &collection_block)
16
+ end
17
+ end
18
+ end
@@ -1,8 +1,12 @@
1
+ require 'show_for/attribute'
2
+ require 'show_for/association'
1
3
  require 'show_for/content'
2
4
  require 'show_for/label'
3
5
 
4
6
  module ShowFor
5
7
  class Builder
8
+ include ShowFor::Attribute
9
+ include ShowFor::Association
6
10
  include ShowFor::Content
7
11
  include ShowFor::Label
8
12
 
@@ -12,47 +16,6 @@ module ShowFor
12
16
  @object, @template = object, template
13
17
  end
14
18
 
15
- def attribute(attribute_name, options={}, &block)
16
- apply_default_options!(attribute_name, options)
17
- collection_block, block = block, nil if collection_block?(block)
18
-
19
- value = if block
20
- block
21
- elsif @object.respond_to?(:"human_#{attribute_name}")
22
- @object.send :"human_#{attribute_name}"
23
- else
24
- @object.send(attribute_name)
25
- end
26
-
27
- wrap_label_and_content(attribute_name, value, options, &collection_block)
28
- end
29
-
30
- def association(association_name, options={}, &block)
31
- apply_default_options!(association_name, options)
32
-
33
- # If a block with an iterator was given, no need to calculate the labels
34
- # since we want the collection to be yielded. Otherwise, calculate the values.
35
- value = if collection_block?(block)
36
- collection_block = block
37
- @object.send(association_name)
38
- elsif block
39
- block
40
- else
41
- association = @object.send(association_name)
42
- values = retrieve_values_from_association(association, options)
43
-
44
- if options.delete(:to_sentence)
45
- values.to_sentence
46
- elsif joiner = options.delete(:join)
47
- values.join(joiner)
48
- else
49
- values
50
- end
51
- end
52
-
53
- wrap_label_and_content(association_name, value, options, &collection_block)
54
- end
55
-
56
19
  protected
57
20
 
58
21
  def object_name #:nodoc:
@@ -89,10 +52,10 @@ module ShowFor
89
52
  concat ? @template.concat(html) : html
90
53
  end
91
54
 
92
- def retrieve_values_from_association(association, options) #:nodoc:
93
- sample = association.is_a?(Array) ? association.first : association
94
- method = options[:method] || ShowFor.association_methods.find { |m| sample.respond_to?(m) }
95
- association.is_a?(Array) ? association.map(&method) : association.try(method)
55
+ # Returns true if the block is supposed to iterate through a collection,
56
+ # i.e. it has arity equals to one.
57
+ def collection_block?(block) #:nodoc:
58
+ block && block.arity == 1
96
59
  end
97
60
  end
98
61
  end
@@ -42,11 +42,5 @@ module ShowFor
42
42
 
43
43
  wrap_with(:collection, response, options)
44
44
  end
45
-
46
- # Returns true if the block is supposed to iterate through a collection,
47
- # i.e. it has arity equals to one.
48
- def collection_block?(block) #:nodoc:
49
- block && block.arity == 1
50
- end
51
45
  end
52
46
  end
@@ -15,7 +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
- content_tag(tag, yield(ShowFor::Builder.new(object, self)), html_options)
18
+ concat content_tag(tag, capture(ShowFor::Builder.new(object, self), &block), html_options)
19
19
  end
20
20
  end
21
21
  end
@@ -1,3 +1,3 @@
1
1
  module ShowFor
2
- VERSION = "0.1.1".freeze
2
+ VERSION = "0.1.2".freeze
3
3
  end
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
- concat(show_for(object) do |o|
6
+ show_for(object) do |o|
7
7
  o.attribute(attribute, options, &block)
8
- end)
8
+ end
9
9
  end
10
10
 
11
11
  def with_association_for(object, association, options={}, &block)
12
- concat(show_for(object) do |o|
12
+ show_for(object) do |o|
13
13
  o.association(association, options, &block)
14
- end)
14
+ end
15
15
  end
16
16
 
17
17
  def with_label_for(object, attribute, options={})
18
- concat(show_for(object) do |o|
18
+ show_for(object) do |o|
19
19
  o.label attribute, options
20
- end)
20
+ end
21
21
  end
22
22
 
23
23
  def with_content_for(object, value, options={})
24
- concat(show_for(object) do |o|
24
+ show_for(object) do |o|
25
25
  o.content value, options
26
- end)
26
+ end
27
27
  end
28
28
 
29
29
  # WRAPPER
@@ -271,11 +271,22 @@ class BuilderTest < ActionView::TestCase
271
271
  assert_select "div.show_for p.wrapper", /PlataformaTec/
272
272
  end
273
273
 
274
- test "show_for accepts :method as option to tell how to retrieve association value" do
275
- with_association_for @user, :company, :method => :alternate_name
274
+ test "show_for accepts :using as option to tell how to retrieve association value" do
275
+ with_association_for @user, :company, :using => :alternate_name
276
276
  assert_select "div.show_for p.wrapper", /Alternate PlataformaTec/
277
277
  end
278
278
 
279
+ test "show_for accepts :in to tell to retrieve an attribute from association" do
280
+ with_attribute_for @user, :alternate_name, :in => :company
281
+ assert_select "div.show_for p.wrapper", /Alternate PlataformaTec/
282
+ end
283
+
284
+ test "show_for forwards all options send with :in to association" do
285
+ with_attribute_for @user, :alternate_name, :in => :tags, :to_sentence => true
286
+ assert_no_select "div.show_for p.wrapper ul.collection"
287
+ assert_select "div.show_for p.wrapper", /Alternate Tag 1, Alternate Tag 2, and Alternate Tag 3/
288
+ end
289
+
279
290
  test "show_for works with has_many/has_and_belongs_to_many associations" do
280
291
  with_association_for @user, :tags
281
292
  assert_select "div.show_for p.wrapper ul.collection"
@@ -284,8 +295,8 @@ class BuilderTest < ActionView::TestCase
284
295
  assert_select "div.show_for p.wrapper ul.collection li", "Tag 3"
285
296
  end
286
297
 
287
- test "show_for accepts :method as option to tell how to retrieve association values" do
288
- with_association_for @user, :tags, :method => :alternate_name
298
+ test "show_for accepts :using as option to tell how to retrieve association values" do
299
+ with_association_for @user, :tags, :using => :alternate_name
289
300
  assert_select "div.show_for p.wrapper ul.collection"
290
301
  assert_select "div.show_for p.wrapper ul.collection li", "Alternate Tag 1"
291
302
  assert_select "div.show_for p.wrapper ul.collection li", "Alternate Tag 2"
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
- concat(show_for(@user) do |f|
5
+ 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
- concat(show_for(@user) do |f| end)
11
+ 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
- concat(show_for(@user) do |f| end)
16
+ 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
- concat(show_for(@user, :id => "my_div", :class => "common") do |f| end)
21
+ 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
- concat(show_for(@user) do |f| end)
27
+ 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.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - "Jos\xC3\xA9 Valim"
@@ -27,6 +27,8 @@ files:
27
27
  - generators/show_for_install/templates/show_for.rb
28
28
  - init.rb
29
29
  - lib/show_for.rb
30
+ - lib/show_for/association.rb
31
+ - lib/show_for/attribute.rb
30
32
  - lib/show_for/builder.rb
31
33
  - lib/show_for/content.rb
32
34
  - lib/show_for/helper.rb