Class: BootstrapBuilders::AttributeRows

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

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ AttributeRows

Returns a new instance of AttributeRows.



2
3
4
5
6
# File 'lib/bootstrap_builders/attribute_rows.rb', line 2

def initialize(args)
  @model = args.fetch(:model)
  @attributes = args.fetch(:attributes)
  @context = args.fetch(:context)
end

Instance Method Details

#htmlObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/bootstrap_builders/attribute_rows.rb', line 8

def html
  elements = []

  @attributes.each do |attribute|
    tr = HtmlGen::Element.new(:tr, classes: ["bb-attributes-row", "bb-attributes-row-#{attribute}"])
    tr.add_ele(:th, classes: ["bb-attributes-row-title"], str: @model.class.human_attribute_name(attribute))

    if column_type(attribute) == :boolean
      str = @model.__send__("#{attribute}?") ? @context.t("yes") : @context.t("no")
    elsif column_type(attribute) == :model
      model_value = column_value(attribute)

      unless model_value.nil?
        link_method_name = "link_to_#{StringCases.camel_to_snake(model_value.class.name)}"

        if @view.respond_to?(:link_to_model)
          str = @view.link_to_model(model_value)
        elsif @context.respond_to?(link_method_name)
          str_html = @context.__send__(link_method_name, model_value)
        else
          str = model_value.name
        end
      end
    elsif column_type(attribute) == :datetime || column_type(attribute) == :date
      str = @context.l(column_value(attribute), format: :long) if column_value(attribute)
    elsif column_type(attribute) == :money
      str = column_value(attribute).try(:format)
    else
      str = column_value(attribute).to_s
    end

    if str
      tr.add_ele(:td, classes: ["bb-attributes-row-value"], str: str)
    else
      tr.add_ele(:td, classes: ["bb-attributes-row-value"], str_html: str_html)
    end

    elements << tr.html
  end

  html = elements.join("\n")
  html = html.html_safe if html.respond_to?(:html_safe) # rubocop:disable Rails/OutputSafety
  html
end