Sha256: 9ff3c8cd96699da7eadcff58a9231baf3e66c5171cd5d1c4f4834e1501abcf98

Contents?: true

Size: 1.49 KB

Versions: 4

Compression:

Stored size: 1.49 KB

Contents

module DomFor
  #
  # Class name: user
  #
  # ID name: user_1
  #
  # Data-attributes: data-object-id = 1
  #
  module Record
    #
    # Creates a div tag with the attributes for the instance of ActiveRecord
    #
    # @example For the new record:
    #   dom_for_record(User.new) #=> <div class="user" id="new_user" />
    #
    # @example For the saved record:
    #   dom_for_record(@user) #=> <div class="user" data-object-id="1" id="user_1" />
    #
    # @example With the additional attributes:
    #   dom_for_record(@user, admin: true) #=> <div class="user" data-admin="true" data-object-id="1" id="user_1" />
    #
    # @example 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>
    #
    # @param [ActiveRecord::Base] record Instance of ActiveRecord::Base
    # @param [Hash] attrs Additional attributes for the record
    # @param [Proc] block Block for a div tag
    #
    # @return [String] Sanitized HTML string
    #
    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?
        content_tag(:div, id: object_id, class: object_class, data: attrs, &block)
      else
        tag(:div, id: object_id, class: object_class, data: attrs)
      end

    rescue
      content_tag(:div, &block)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
dom_for-1.0.3 lib/dom_for/record.rb
dom_for-1.0.2 lib/dom_for/record.rb
dom_for-1.0.1 lib/dom_for/record.rb
dom_for-1.0.0 lib/dom_for/record.rb