Class: Attrtastic::SemanticAttributesBuilder

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

Instance Attribute Summary

Instance Method Summary

Constructor Details

- (SemanticAttributesBuilder) initialize(record, template)

A new instance of SemanticAttributesBuilder



14
15
16
# File 'lib/attrtastic.rb', line 14

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

Instance Attribute Details

- (Object) record (readonly)

Only for testing purposes



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

def record
  @record
end

- (Object) template (readonly)

Only for testing purposes



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

def template
  @template
end

Instance Method Details

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

Creates list entry for single record attribute

Examples:

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

  <% attr.attribute :name, :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):

    • (String) :html — default: {} — Hash with optional :class, :label_class and :value_class names of class for html
    • (String) :label N/A — Label for attribute entry, overrides default label name from symbol
    • (String) :value N/A — 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 = {}, &block) { ... }

    Creates entry for attribute given with block

    Examples:

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

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

    Parameters:

    • (Symbol) method — Attribute name of given record
    • (Hash) options — Options

    Options Hash (options):

    • (String) :html — default: {} — Hahs with optional :class, :label_class and :value_class names of class for html
    • (String) :label N/A — Label for attribute entry, overrides default label name from symbol
    • (Boolean) :display_empty — default: false — Indicates if print value of given attribute even if it is blank?

    Yields:

    • Block which is executed in place of value for attribute


162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/attrtastic.rb', line 162

def attribute(*args, &block)
  options = {}
  if args.last and args.last.kind_of? Hash
    options = args.last
    args = args[0 .. -2]
  end
  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)
  label_content = template.(:span, label, :class => html_label_class)

  unless block_given?
    value = options.key?(:value) ? options[:value] : value_of_attribute(method)
    value_content = template.(:span, value, :class => html_value_class)

    if value.present? or options[:display_empty]
      content = [ label_content, value_content ].join
      template.(:li, content, :class => html_class)
    end
  else
    template.concat(template.tag(:li, {:class => html_class}, true))
    template.concat(label_content)
    template.concat(template.tag(:span, {:class => html_value_class}, true))
    yield
    template.concat("</span>")
    template.concat("</li>")
  end
end

- (Object) attributes(options = {}, &block) { ... } - (Object) attributes(header, options = {}, &block) { ... } - (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 = {}, &block) { ... }

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

    Examples:

      <% attr.attributes do %>
        <%= attr.attribute :name %>
        <%= attr.attribute :email %>
      <% end %>

    Parameters:

    • (Hash) options — Options for formating attributes block

    Options Hash (options):

    • (String) :class — default: '' — Name of html class(-es) to add to attributes block
    • (String) :header_class — default: '' — Name of html class(-es) to add to header

    Yields:

    • Block which can call #attribute to include attribute value
  • - (Object) attributes(header, options = {}, &block) { ... }

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

    Parameters:

    • (String) header — Header of attributes section
    • (Hash) options — Options for formating attributes block

    Options Hash (options):

    • (String) :class — default: '' — Name of html class(-es) to add to attributes block
    • (String) :header_class — default: '' — Name of html class(-es) to add to header

    Yields:

    • Block which can call #attribute to include attribute value
  • - (Object) attributes(*symbols, options = {})

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

    Examples:

      <% attr.attributes :name, :email %>

    Parameters:

    • (Symbol, ...) symbols — List of attributes
    • (Hash) options — Options for formating attributes block

    Options Hash (options):

    • (String) :class — default: '' — Name of html class(-es) to add to attributes block
    • (String) :header_class — default: '' — Name of html class(-es) 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 %>

    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(-es) to add to attributes block
    • (String) :header_class — default: '' — Name of html class(-es) to add to header

See Also:



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/attrtastic.rb', line 83

def attributes(*args, &block)
  options = {}
  if args.last and args.last.kind_of? Hash
    options = args.last
    args = args[0 .. -2]
  end
  options[:html] ||= {}

  html_class = [ "attributes", options[:html].delete(:class) ].compact.join(" ")
  html_header_class = [ "legend", options[:html].delete(:header_class) ].compact.join(" ")

  template.concat(template.tag(:div, {:class => html_class}, true))

  if args.first and args.first.is_a? String
    header = args.shift
    template.concat(template.(:div, header, :class => html_header_class))
  end

  if block_given?
    template.concat(template.tag(:ol, {}, true))
    yield
    template.concat("</ol>")
  elsif args.present?
    template.concat(template.tag(:ol, {}, true))
    attrs = args.map {|method| attribute(method, options)}.compact.join
    template.concat(attrs)
    template.concat("</ol>")
  end

  template.concat("</div>")
end