lib/attrtastic/semantic_attributes_builder.rb in attrtastic-0.3.3 vs lib/attrtastic/semantic_attributes_builder.rb in attrtastic-0.4.0

- old
+ new

@@ -4,10 +4,13 @@ class SemanticAttributesBuilder # Only for testing purposes attr_reader :record, :template + # For compatibility with formtastic + alias :object :record + def initialize(record, template) @record, @template = record, template end ## @@ -62,10 +65,20 @@ # <%= attr.attributes :for => @posts do |post| %> # <%= post.attribute :author %> # <%= post.attribute :title %> # <% end %> # + # @example + # <%= attr.attributes :for => @posts do |post| %> + # <%= post.attribute :birthday, :format => false %> + # <% end %> + # + # @example + # <%= attr.attributes :for => @posts do |post| %> + # <%= post.attribute :birthday, :format => :my_fancy_birthday_formatter %> + # <% end %> + # # @overload attributes(header, options = {}, &block) # Creates attributes list with header and yields block to include each attribute # # @param [String] header Header of attributes section # @param [Hash] options Options for formating attributes block @@ -206,22 +219,34 @@ # # @param [Symbol] method Attribute name of given record # @param [Hash] options Options # @option options [Hash] :html ({}) Hash with optional :class, :label_class and :value_class names of class for html # @option options [String] :label Label for attribute entry, overrides default label name from symbol - # @option options [String] :value Value of attribute entry, overrides default value from record + # @option options [Symbol,Object] :value If it's Symbol, then it's used as either name of hash key to use on attribute + # (if it's hash) or method name to call on attribute. Otherwise it's used as value to use instead of + # actual attribute's. # @option options [Boolean] :display_empty (false) Indicates if print value of given attribute even if it is blank? + # @option options [Symbol,false,nil] :format (nil) Type of formatter to use to display attribute's value. If it's false, + # then don't format at all (just call #to_s). If it's nil, then use default formatting (#l for dates, + # #number_with_precision/#number_with_delimiter for floats/integers). If it's Symbol, then use it to select + # view helper method and pass aattribute's value to it to format. # # @example # <%= attr.attribute :name %> # # @example # <%= attr.attribute :name, :label => "Full user name" %> # # @example # <%= attr.attribute :name, :value => @user.full_name %> # + # @example + # <%= attr.attribute :address, :value => :street %> + # + # @example + # <%= attr.attribute :avatar, :value => :url, :format => :image_tag %> + # # @overload attribute(method, options = {}, &block) # Creates entry for attribute given with block # # @param [Symbol] method Attribute name of given record # @param [Hash] options Options @@ -263,12 +288,36 @@ html_class = [ "attribute", options[:html][:class] ].compact.join(" ") label = options.key?(:label) ? options[:label] : label_for_attribute(method) unless block_given? - value = options.key?(:value) ? options[:value] : value_of_attribute(method) + value = if options.key?(:value) + case options[:value] + when Symbol + attribute_value = value_of_attribute(method) + case attribute_value + when Hash + attribute_value[options[:value]] + else + attribute_value.send(options[:value]) + end + else + options[:value] + end + else + value_of_attribute(method) + end + value = case options[:format] + when false + value + when nil + format_attribute_value(value) + else + template.send(options[:format], value) + end + if value.present? or options[:display_empty] output = template.tag(:li, {:class => html_class}, true) output << template.content_tag(:span, label, :class => html_label_class) output << template.content_tag(:span, value, :class => html_value_class) output.safe_concat("</li>") @@ -294,11 +343,13 @@ output = template.tag(:div, {:class => html_class}, true) header = options[:name] if header.present? - output << template.content_tag(:div, header, :class => html_header_class) + output << template.content_tag(:div, :class => html_header_class) do + template.content_tag(:span, header) + end end if block_given? output << template.tag(:ol, {}, true) output << template.capture(new_builder, &block) @@ -321,13 +372,22 @@ method.to_s.send(:humanize) end end def value_of_attribute(method) - value = record.send(method) - value_methods = [ :to_label, :display_name, :full_name, :name, :title, :username, :login, :value ] - value_method = value_methods.find { |m| value.respond_to?(m) } || :to_s - value.send(value_method) + record.send(method) end + def format_attribute_value(value) + case value + when Date, Time, DateTime + template.send(:l, value) + when Integer + template.send(:number_with_delimiter, value) + when Float, BigDecimal + template.send(:number_with_precision, value) + else + value.to_s + end + end end end