Module: Attrtastic::SemanticAttributesHelper

Defined in:
lib/attrtastic.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

Instance Method Details

- (Object) semantic_attributes_for(record, options = {}, &block) {|attr| ... }

Creates attributes for given object

@param[ActiveRecord] record AR instance record for which to display attributes @param[Hash] options Opions

Examples:

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

Options Hash (options):

  • (Hash) :html — default: {} — Hash with optional :class html class name for html block

Yields:

  • (attr) — Block which is yield inside of markup

Yield Parameters:



382
383
384
385
386
387
388
389
390
# File 'lib/attrtastic.rb', line 382

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

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

  concat(tag(:div, { :class => html_class}, true))
  yield SemanticAttributesBuilder.new(record, self) if block_given?
  concat("</div>")
end