Module: Attrtastic::SemanticAttributesHelper

Defined in:
lib/attrtastic/semantic_attributes_helper.rb

Overview

Helper which should be included in ActionView. Adds #semantic_attributes_for method, which helps printing attributes for given record, similar to formtastic’s sematnic_form_for

Examples:

ActionView::Base.send :include, Attrtastic::SemanticAttributesHelper

Example of useage

<%= semantic_attributes_for @user do |attr| %>
  <%= attr.attributes "User info" do %>
    <%= attr.attribute :name %>
    <%= attr.attribute :email %>
  <% end %>
  <%= attr.attributes "User details" do %>
    <%= attr.attribute :weight %>
    <%= attr.attribute :height %>
    <%= attr.attribute :age %>
  <% end %>
<% end %>

Instance Method Summary collapse

Instance Method Details

#semantic_attributes_for(record, options = {}) {|attr| ... } ⇒ Object

Creates attributes for given object

@param record AR instance record for which to display attributes @param options Opions

Examples:

<%= semantic_attributes_for @user do |attr| %>
  <%= attr.attributes do %>
    <%= attr.attribute :name %>
    <%= attr.attribute :email %>
  <% end %>
<% end %>
<%= semantic_attributes_for @user %>

Parameters:

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

    a customizable set of options

Options Hash (options):

  • :html (Hash) — default: {}

    Hash with optional :class html class name for html block

Yields:

  • (attr)

    Block which is yield inside of markup

Yield Parameters:



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/attrtastic/semantic_attributes_helper.rb', line 44

def semantic_attributes_for(record, options = {}, &block)
  options[:html] ||= {}

  html_class = [ "attrtastic", record.class.to_s.underscore, options[:html][:class] ].compact.join(" ")

  output = tag(:div, { :class => html_class}, true)
  if block_given?
    output << capture(SemanticAttributesBuilder.new(record, self), &block)
  else
    output << capture(SemanticAttributesBuilder.new(record, self)) do |attr|
      attr.attributes
    end
  end
  output.safe_concat("</div>")
end