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

- (SemanticAttributesBuilder) initialize(record, template)

A new instance of SemanticAttributesBuilder



9
10
11
# File 'lib/attrtastic/semantic_attributes_builder.rb', line 9

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

Instance Attribute Details

- (Object) record (readonly)

Only for testing purposes



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

def record
  @record
end

- (Object) template (readonly)

Only for testing purposes



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

def template
  @template
end

Instance Method Details

- (Object) attribute(method, options = {}) - (Object) attribute(method, options = {}) { ... } - (Object) attribute(options = {}) { ... }

Creates list entry for single record attribute

Examples:

<%= attr.attribute :name, :display_empty => true %>

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

Overloads:

  • - (Object) attribute(method, options = {})

    Creates entry for record attribute

    Examples:

    <%= attr.attribute :name %>

    <%= attr.attribute :name, :label => "Full user name" %>

    <%= attr.attribute :name, :value => @user.full_name %>

    Parameters:

    • (Symbol) method

      Attribute name of given record

    • (Hash) options

      Options

    Options Hash (options):

    • (Hash) :html — default: {}

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

    • (String) :label

      Label for attribute entry, overrides default label name from symbol

    • (String) :value

      Value of attribute entry, overrides default value from record

    • (Boolean) :display_empty — default: false

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

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

    Creates entry for attribute given with block

    Examples:

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

    Parameters:

    • (Symbol) method

      Attribute name of given record

    • (Hash) options

      Options

    Options Hash (options):

    • (Hash) :html — default: {}

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

    • (String) :label

      Label for attribute entry, overrides default label name from symbol

    Yields:

    • Block which is executed in place of value for attribute

  • - (Object) attribute(options = {}) { ... }

    Creates entry for attribute with given block, options[:label] is mandatory in this case.

    Examples:

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

    Parameters:

    • (:Hash) options

      Options

    Options Hash (options):

    • (Hash) :html — default: {}

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

    • (String) :label

      Mandatory label for attribute entry

    Yields:

    • Block which is executed in place of value for attribute



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/attrtastic/semantic_attributes_builder.rb', line 255

def attribute(*args, &block)
  options = args.extract_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 = options.key?(:value) ? options[:value] : value_of_attribute(method)

    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

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

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:

  • - (Object) attributes(options = {}) { ... }

    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 %>

    Parameters:

    • (Hash) options

      Options for formating attributes block

    Options Hash (options):

    • (String) :name — default: nil

      Optional header of attributes section

    • (String) :class — default: ''

      Name of html class to add to attributes block

    • (String) :header_class — default: ''

      Name of html class to add to header

    Yields:

    • Block which can call #attribute to include attribute value

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

    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:

    • (String) header

      Header of attributes section

    • (Hash) options

      Options for formating attributes block

    Options Hash (options):

    • (String) :class — default: ''

      Name of html class to add to attributes block

    • (String) :header_class — 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)

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

    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:

    • (Symbol, ...) symbols

      List of attributes

    • (Hash) options

      Options for formating attributes block

    Options Hash (options):

    • (String) :name — default: nil

      Optional header of attributes section

    • (String) :class — default: ''

      Name of html class to add to attributes block

    • (String) :header_class — default: ''

      Name of html class to add to header

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

    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:

    • (String) header

      Header of attributes section

    • (Symbol, ...) symbols

      Optional list of attributes

    • (Hash) options

      Options for formating attributes block

    Options Hash (options):

    • (String) :class — default: ''

      Name of html class to add to attributes block

    • (String) :header_class — default: ''

      Name of html class to add to header

See Also:



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/attrtastic/semantic_attributes_builder.rb', line 174

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
  end

end