module ShowFor::Attribute
Public Instance Methods
attribute(attribute_name, options = {}, &block)
click to toggle source
# File lib/show_for/attribute.rb, line 3 def attribute(attribute_name, options = {}, &block) apply_default_options!(attribute_name, options) block = block_from_value_option(attribute_name, options) unless block collection_block, block = block, nil if collection_block?(block) value = attribute_value(attribute_name, &block) wrap_label_and_content(attribute_name, value, options, &collection_block) end
attributes(*attribute_names)
click to toggle source
# File lib/show_for/attribute.rb, line 22 def attributes(*attribute_names) attribute_names.map do |attribute_name| attribute(attribute_name) end.join.html_safe end
value(attribute_name, options = {}, &block)
click to toggle source
# File lib/show_for/attribute.rb, line 13 def value(attribute_name, options = {}, &block) apply_default_options!(attribute_name, options) collection_block, block = block, nil if collection_block?(block) value = attribute_value(attribute_name, &block) wrap_content(attribute_name, value, options, &collection_block) end
Private Instance Methods
attribute_value(attribute_name, &block)
click to toggle source
# File lib/show_for/attribute.rb, line 30 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
block_from_symbol(attribute_name, options)
click to toggle source
# File lib/show_for/attribute.rb, line 50 def block_from_symbol(attribute_name, options) attribute = @object.send(attribute_name) case attribute when Array, Hash lambda { |element| element.send(options[:value]) } else lambda { attribute.send(options[:value]) } end end
block_from_value_option(attribute_name, options)
click to toggle source
# File lib/show_for/attribute.rb, line 40 def block_from_value_option(attribute_name, options) if !options.has_key?(:value) nil elsif options[:value].is_a?(Symbol) block_from_symbol(attribute_name, options) else lambda { options[:value].to_s } end end