Module: DomFor::Record

Included in:
DomFor
Defined in:
lib/dom_for/record.rb

Overview

Class name: user

ID name: user_1

Data-attributes: data-object-id = 1

Instance Method Summary collapse

Instance Method Details

#dom_for_record(record, attrs = {}, &block) ⇒ String

Creates a div tag with the attributes for the instance of ActiveRecord

Examples:

For the new record:

dom_for_record(User.new) #=> <div class="user" id="new_user" />

For the saved record:

dom_for_record(@user) #=> <div class="user" data-object-id="1" id="user_1" />

With the additional attributes:

dom_for_record(@user, admin: true) #=> <div class="user" data-admin="true" data-object-id="1" id="user_1" />

With the block:

dom_for_record(@user, admin: true) do
  tag(:span)
end #=> <div class="user" data-admin="true" data-object-id="1" id="user_1"><span /></div>

Parameters:

  • record (ActiveRecord::Base)

    Instance of ActiveRecord::Base

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

    Additional attributes for the record

  • block (Proc)

    Block for a div tag

Returns:

  • (String)

    Sanitized HTML string



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/dom_for/record.rb', line 33

def dom_for_record(record, attrs={}, &block)
  object_id     = dom_id(record)
  object_class  = dom_class(record.class)

  attrs = attrs.merge(object_id: record.id) if record.persisted?

  if block_given?
    (:div, id: object_id, class: object_class, data: attrs, &block)
  else
    tag(:div, id: object_id, class: object_class, data: attrs)
  end

rescue
  (:div, &block)
end