Class: Effective::TableRows::Collection

Inherits:
Effective::TableRow show all
Defined in:
app/models/effective/table_rows/collection.rb

Instance Attribute Summary

Attributes inherited from Effective::TableRow

#builder, #name, #options, #template

Instance Method Summary collapse

Methods inherited from Effective::TableRow

#controller_namespace, #hint, #label, #label_content, #to_html, #tr_class, #value

Constructor Details

#initialize(name, collection, options, builder:) ⇒ Collection

Returns a new instance of Collection.



7
8
9
10
# File 'app/models/effective/table_rows/collection.rb', line 7

def initialize(name, collection, options, builder:)
  @collection = collection
  super(name, options, builder: builder)
end

Instance Method Details

#contentObject



12
13
14
15
16
17
18
19
20
# File 'app/models/effective/table_rows/collection.rb', line 12

def content
  values = Array(value) - [nil, '']

  if values.length > 1
    values.map { |v| (:div, item_content(v)) }.join.html_safe
  elsif values.length == 1
    item_content(values.first)
  end
end

#item_content(value) ⇒ Object



22
23
24
25
26
# File 'app/models/effective/table_rows/collection.rb', line 22

def item_content(value)
  item = @collection.find { |k, v| (k && k == value) || (v && v == value) || k.try(:id) == value }
  item = item.first if item.kind_of?(Array)
  (item || value).to_s
end