Browse files

refactoring

extract getting attribute method to separate method
remove retrieve_ and get_ prefix from methods
  • Loading branch information...
1 parent 582daa6 commit c4fcf3004c9665b713ea3fce378744995a7a65bc @nashby nashby committed Jan 21, 2012
Showing with 20 additions and 21 deletions.
  1. +2 −2 lib/show_for/association.rb
  2. +17 −18 lib/show_for/attribute.rb
  3. +1 −1 lib/show_for/helper.rb
View
4 lib/show_for/association.rb
@@ -21,7 +21,7 @@ def association(association_name, options={}, &block)
block
else
association = @object.send(association_name)
- values = retrieve_values_from_association(association, options)
+ values = values_from_association(association, options)
if options.delete(:to_sentence)
values.to_sentence
@@ -37,7 +37,7 @@ def association(association_name, options={}, &block)
protected
- def retrieve_values_from_association(association, options) #:nodoc:
+ def values_from_association(association, options) #:nodoc:
sample = association.is_a?(Array) ? association.first : association
if options[:method]
View
35 lib/show_for/attribute.rb
@@ -2,16 +2,10 @@ module ShowFor
module Attribute
def attribute(attribute_name, options={}, &block)
apply_default_options!(attribute_name, options)
- block = get_block_from_value_option(attribute_name, options) unless block
+ block = block_from_value_option(attribute_name, options) unless block
collection_block, block = block, nil if collection_block?(block)
- value = if block
- block
- elsif @object.respond_to?(:"human_#{attribute_name}")
- @object.send :"human_#{attribute_name}"
- else
- @object.send(attribute_name)
- end
+ value = attribute_value(attribute_name, &block)
wrap_label_and_content(attribute_name, value, options, &collection_block)
end
@@ -20,13 +14,8 @@ def value(attribute_name, options={}, &block)
apply_default_options!(attribute_name, options)
collection_block, block = block, nil if collection_block?(block)
- value = if block
- block
- elsif @object.respond_to?(:"human_#{attribute_name}")
- @object.send :"human_#{attribute_name}"
- else
- @object.send(attribute_name)
- end
+ value = attribute_value(attribute_name, &block)
+
wrap_content(attribute_name, value, options, &collection_block)
end
@@ -38,18 +27,28 @@ def attributes(*attribute_names)
private
- def get_block_from_value_option(attribute_name, options)
+ def attribute_value(attribute_name, &block)
+ if block
+ block
+ elsif @object.respond_to?(:"human_#{attribute_name}")
+ @object.send :"human_#{attribute_name}"
+ else
+ @object.send(attribute_name)
+ end
+ end
+
+ def block_from_value_option(attribute_name, options)
case options[:value]
when nil
nil
when Symbol
- get_block_from_symbol(attribute_name, options)
+ block_from_symbol(attribute_name, options)
else
lambda { options[:value] }
end
end
- def get_block_from_symbol(attribute_name, options)
+ def block_from_symbol(attribute_name, options)
attribute = @object.send(attribute_name)
case attribute
when Array, Hash
View
2 lib/show_for/helper.rb
@@ -13,7 +13,7 @@ def show_for(object, html_options={}, &block)
tag = html_options.delete(:show_for_tag) || ShowFor.show_for_tag
html_options[:id] ||= dom_id(object)
- html_options[:class] = "show_for #{dom_class(object)} #{html_options[:class]}".strip
+ html_options[:class] = "show_for #{dom_class(object)} #{html_options[:class]}".rstrip
builder = html_options.delete(:builder) || ShowFor::Builder
content = capture(builder.new(object, self), &block)

0 comments on commit c4fcf30

Please sign in to comment.