Class: SimpleModelView::ResourceTableBuilder

Inherits:
Object
  • Object
show all
Includes:
BuilderHelpers, TemplateHelpers
Defined in:
lib/simple_model_view/resource_table_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BuilderHelpers

#format

Methods included from TemplateHelpers

#blank_span, #block_concat, #merge_html_attrs

Constructor Details

#initialize(template, object, *_args, formatter: SimpleModelView.formatter) ⇒ ResourceTableBuilder

Returns a new instance of ResourceTableBuilder.



8
9
10
11
12
# File 'lib/simple_model_view/resource_table_builder.rb', line 8

def initialize(template, object, *_args, formatter: SimpleModelView.formatter)
  @template = template
  @object = object
  @formatter = formatter
end

Instance Attribute Details

#formatterObject (readonly)

Returns the value of attribute formatter.



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

def formatter
  @formatter
end

#objectObject (readonly)

Returns the value of attribute object.



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

def object
  @object
end

Instance Method Details

#actions(*_args, **options) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/simple_model_view/resource_table_builder.rb', line 60

def actions(*_args, **options)
  template.(:tr) do
    template.concat template.(:th, options[:title])
    block_concat do
      template.(:td) do
        yield object if block_given?
      end
    end
  end
end

#row(attr_name, title: nil, **options, &block) ⇒ Object

Renders row for given attr_name.

Arguments

  • attr_name - Attribute to be rendered as a Symbol or String.

  • title: - String to use as attribute name in table. Using human_attribute_name or if not given.

  • as: - Force attribute value type _(:boolean, :date, :time, :integer, :float, etc…)_.

  • collection: - If true tryes to interpret value as a iterateble collection.

  • type_specific_class: - adds type specific classes to the wrapper <tr> tag. For numeric it would be ‘negative`, `zero`, `positieve`; For date and time it would be `past`, `future`, `yesterday`, etc; See Examples for more details.

  • custom_class: - Hash with values as Symbol or any callable object. symbol will be sent to the value as a method. Proc will be called and passed a value as an argument. If method or block retuns not false or nil hash key will be added as a class to the wrapper <tr> tag.

  • wrapper_html: - html attributes to add to wrapper <tr> tag.

  • label_html: - html attributes to add to attribute title <th> tag.

  • value_html: - html attributes to add to attribute value <td> tag.

  • **options - all other named arguments will be passed to the formatter.

  • &block - if block given it will render inside value cell.

Examples

TODO:



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/simple_model_view/resource_table_builder.rb', line 43

def row(attr_name, title: nil, **options, &block)
  title ||= if object.class.respond_to?(:human_attribute_name)
              object.class.human_attribute_name attr_name
            else
              attr_name.to_s.humanize
            end

  render_data = prepare_render_data(attr_name: attr_name, options: options)

  label_html = options[:label_html] || {}
  value_html = options[:value_html] || {}

  render_row title, render_data[:wrapper_html], label_html, value_html do
    render_value render_data, options, &block
  end
end