Class: Attrtastic::SemanticAttributesBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/attrtastic/semantic_attributes_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record, template) ⇒ SemanticAttributesBuilder

Returns a new instance of SemanticAttributesBuilder.



12
13
14
# File 'lib/attrtastic/semantic_attributes_builder.rb', line 12

def initialize(record, template)
  @record, @template = record, template
end

Instance Attribute Details

#recordObject (readonly) Also known as: object

Only for testing purposes



7
8
9
# File 'lib/attrtastic/semantic_attributes_builder.rb', line 7

def record
  @record
end

#templateObject (readonly)

Only for testing purposes



7
8
9
# File 'lib/attrtastic/semantic_attributes_builder.rb', line 7

def template
  @template
end

Instance Method Details

#attribute(method, options = {}) ⇒ Object #attribute(method, options = {}) { ... } ⇒ Object #attribute(options = {}) { ... } ⇒ Object

Creates list entry for single record attribute

Options can be set globally with Attrtastic.default_options

Examples:

<%= attr.attribute :name, :display_empty => true %>
<%= attr.attribute :label => "User link" do %>
  <%= link_to @user.full_name, user_path(@user) %>

Overloads:

  • #attribute(method, options = {}) ⇒ Object

    Creates entry for record attribute

    Examples:

    <%= attr.attribute :name %>
    <%= attr.attribute :name, :label => "Full user name" %>
    <%= attr.attribute :name, :value => @user.full_name %>
    <%= attr.attribute :address, :value => :street %>
    <%= attr.attribute :avatar, :value => :url, :format => :image_tag %>

    Parameters:

    • method (Symbol)

      Attribute name of given record

    • options (Hash) (defaults to: {})

      Options

    Options Hash (options):

    • :html (Hash) — default: {}

      Hash with optional :class, :label_class and :value_class names of class for html

    • :label (String)

      Label for attribute entry, overrides default label name from symbol

    • :value (Symbol, Object)

      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.

    • :display_empty (Boolean) — default: false

      Indicates if print value of given attribute even if it is blank?

    • :format (Symbol, false, nil) — default: 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.

  • #attribute(method, options = {}) { ... } ⇒ Object

    Creates entry for attribute given with block

    Examples:

    <%= attr.attribute :name do %>
      <%= link_to @user.full_name, user_path(@user) %>

    Parameters:

    • method (Symbol)

      Attribute name of given record

    • options (Hash) (defaults to: {})

      Options

    Options Hash (options):

    • :html (Hash) — default: {}

      Hash with optional :class, :label_class and :value_class names of classes for html

    • :label (String)

      Label for attribute entry, overrides default label name from symbol

    Yields:

    • Block which is executed in place of value for attribute

  • #attribute(options = {}) { ... } ⇒ Object

    Creates entry for attribute with given block, options is mandatory in this case.

    Examples:

    <%= attr.attribute :label => "User link" do %>
      <%= link_to @user.full_name, user_path(@user) %>

    Parameters:

    • options (:Hash) (defaults to: {})

      Options

    Options Hash (options):

    • :html (Hash) — default: {}

      Hash with optional :class, :label_class and :value_class names of classes for html

    • :label (String)

      Mandatory label for attribute entry

    Yields:

    • Block which is executed in place of value for attribute



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/attrtastic/semantic_attributes_builder.rb', line 281

def attribute(*args, &block)
  options = args.extract_options!
  options.reverse_merge!(Attrtastic.default_options)
  options[:html] ||= {}

  method = args.shift

  html_label_class = [ "label", options[:html][:label_class] ].compact.join(" ")
  html_value_class = [ "value", options[:html][:value_class] ].compact.join(" ")
  html_class = [ "attribute", options[:html][:class] ].compact.join(" ")

  label = options.key?(:label) ? options[:label] : label_for_attribute(method)

  unless block_given?
    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.(:span, label, :class => html_label_class)
      output << template.(:span, value, :class => html_value_class)
      output.safe_concat("</li>")
    end
  else
    output = template.tag(:li, {:class => html_class}, true)
    output << template.(:span, label, :class => html_label_class)
    output << template.tag(:span, {:class => html_value_class}, true)
    output << template.capture(&block)
    output.safe_concat("</span>")
    output.safe_concat("</li>")
  end
end

#attributes(options = {}) { ... } ⇒ Object #attributes(header, options = {}) {|builder| ... } ⇒ Object #attributes(*symbols, options = {}) ⇒ Object #attributes(header, *symbols, options = {}) ⇒ Object

Creates block of attributes with optional header. Attributes are surrounded with ordered list.

Examples:

All together

<%= attr.attributes "User info", :name, :email, :class => "user_info", :header_class => "header important" %>

With block

<%= attr.attributes "User info" :class => "user_info", :header_class => "header important" do %>
  <%= attr.attribute :name %>
  <%= attr.attribute :email %>
<% end %>

Overloads:

  • #attributes(options = {}) { ... } ⇒ Object

    Creates attributes list without header, yields block to include each attribute

    Examples:

    <%= attr.attributes do %>
      <%= attr.attribute :name %>
      <%= attr.attribute :email %>
    <% end %>
    <%= attr.attributes :name => "User" do %>
      <%= attr.attribute :name %>
      <%= attr.attribute :email %>
    <% end %>
    <%= attr.attributes :for => :user do |user| %>
      <%= user.attribute :name %>
      <%= user.attribute :email %>
      <%= user.attribute :profile do %>
        <%= link_to h(user.record.name), user_path(user.record) %>
      <% end %>
    <% end %>
    <%= attr.attributes :for => @user do |user| %>
      <%= user.attribute :name %>
      <%= user.attribute :email %>
      <%= user.attribute :profile do %>
        <%= link_to h(@user.name), user_path(@user) %>
      <% end %>
    <% end %>
    <%= attr.attributes :for => :posts do |post| %>
      <%= post.attribute :author %>
      <%= post.attribute :title %>
    <% end %>
    <%= attr.attributes :for => @posts do |post| %>
      <%= post.attribute :author %>
      <%= post.attribute :title %>
    <% end %>
    <%= attr.attributes :for => @posts do |post| %>
      <%= post.attribute :birthday, :format => false %>
    <% end %>
    <%= attr.attributes :for => @posts do |post| %>
      <%= post.attribute :birthday, :format => :my_fancy_birthday_formatter %>
    <% end %>

    Parameters:

    • options (Hash) (defaults to: {})

      Options for formating attributes block

    Options Hash (options):

    • :name (String) — default: nil

      Optional header of attributes section

    • :class (String) — default: ''

      Name of html class to add to attributes block

    • :header_class (String) — default: ''

      Name of html class to add to header

    Yields:

    • Block which can call #attribute to include attribute value

  • #attributes(header, options = {}) {|builder| ... } ⇒ Object

    Creates attributes list with header and yields block to include each attribute

    Examples:

    <%= attr.attributes "User info" do %>
      <%= attr.attribute :name" %>
      <%= attr.attribute :email %>
    <% end %>
    <%= attr.attributes "User", :for => :user do |user| %>
      <%= user.attribute :name %>
      <%= user.attribute :email %>
      <%= user.attribute :profile do %>
        <%= link_to h(user.record.name), user_path(user.record) %>
      <%  end %>
    <% end %>
    <% attr.attributes "User", :for => @user do |user| %>
      <%= user.attribute :name %>
      <%= user.attribute :email %>
      <%= user.attribute :profile do %>
        <%= link_to h(@user.name), user_path(@user) %>
      <% end %>
    <% end %>
    <%= attr.attributes "Post", :for => :posts do |post| %>
      <%= post.attribute :author %>
      <%= post.attribute :title %>
    <% end %>
    <%= attr.attributes "Post", :for => @posts do |post| %>
      <%= post.attribute :author %>
      <%= post.attribute :title %>
    <% end %>

    Parameters:

    • header (String)

      Header of attributes section

    • options (Hash) (defaults to: {})

      Options for formating attributes block

    Options Hash (options):

    • :class (String) — default: ''

      Name of html class to add to attributes block

    • :header_class (String) — default: ''

      Name of html class to add to header

    Yields:

    • Block which can call #attribute to include attribute value

    Yield Parameters:

    • builder

      Builder instance holding actual record (retivable via #record)

  • #attributes(*symbols, options = {}) ⇒ Object

    Creates attributes list without header, attributes are given as list of symbols (record properties)

    Examples:

    <%= attr.attributes :name, :email %>
    <%= attr.attributes :name, :email, :for => :author %>
    <%= attr.attributes :name, :email, :for => @user %>
    <%= attr.attributes :title, :for => :posts %>
    <%= attr.attributes :title, :for => @posts %>

    Parameters:

    • symbols (Symbol, ...)

      List of attributes

    • options (Hash) (defaults to: {})

      Options for formating attributes block

    Options Hash (options):

    • :name (String) — default: nil

      Optional header of attributes section

    • :class (String) — default: ''

      Name of html class to add to attributes block

    • :header_class (String) — default: ''

      Name of html class to add to header

  • #attributes(header, *symbols, options = {}) ⇒ Object

    Creates attributes list with header, attributes are given as list of symbols (record properties)

    Examples:

    <%= attr.attributes "User info" :name, :email %>
    <%= attr.attributes "Author", :name, :email, :for => :author %>
    <%= attr.attributes "Author", :name, :email, :for => @user %>
    <%= attr.attributes "Post", :title, :for => :posts %>
    <%= attr.attributes "Post", :title, :for => @posts %>

    Parameters:

    • header (String)

      Header of attributes section

    • symbols (Symbol, ...)

      Optional list of attributes

    • options (Hash) (defaults to: {})

      Options for formating attributes block

    Options Hash (options):

    • :class (String) — default: ''

      Name of html class to add to attributes block

    • :header_class (String) — default: ''

      Name of html class to add to header

See Also:



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/attrtastic/semantic_attributes_builder.rb', line 187

def attributes(*args, &block)
  options = args.extract_options!
  options[:html] ||= {}

  if args.first and args.first.is_a? String
    options[:name] = args.shift
  end

  if options[:for].blank?
    attributes_for(record, args, options, &block)
  else
    for_value = if options[:for].is_a? Symbol
      record.send(options[:for])
    else
      options[:for]
    end

    [*for_value].map do |value|
      value_options = options.clone
      value_options[:html][:class] = [ options[:html][:class], value.class.to_s.underscore ].compact.join(" ")

      attributes_for(value, args, options, &block)
    end.join.html_safe
  end

end