Class: HtmlTables::DataTable

Inherits:
Object
  • Object
show all
Defined in:
lib/html_tables/data_table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(builder, collection, options = {}) ⇒ DataTable

Returns a new instance of DataTable.



8
9
10
11
12
13
14
# File 'lib/html_tables/data_table.rb', line 8

def initialize(builder, collection, options = {})
  @builder = builder
  @collection = collection
  @options = options
  @item_url_options = {}
  @row_classes = []
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



5
6
7
# File 'lib/html_tables/data_table.rb', line 5

def collection
  @collection
end

#item_url_optionsObject

Returns the value of attribute item_url_options.



6
7
8
# File 'lib/html_tables/data_table.rb', line 6

def item_url_options
  @item_url_options
end

#nodata_messageObject

Returns the value of attribute nodata_message.



6
7
8
# File 'lib/html_tables/data_table.rb', line 6

def nodata_message
  @nodata_message
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/html_tables/data_table.rb', line 5

def options
  @options
end

#row_classesObject (readonly)

Returns the value of attribute row_classes.



5
6
7
# File 'lib/html_tables/data_table.rb', line 5

def row_classes
  @row_classes
end

Instance Method Details

#auto_generate_columns!Object



16
17
18
19
20
21
# File 'lib/html_tables/data_table.rb', line 16

def auto_generate_columns!
  model.accessible_attributes.each do |attr|
    col = model_columns[attr]
    object_to_yield.column col.name unless col.nil?
  end
end

#columnsObject



46
47
48
# File 'lib/html_tables/data_table.rb', line 46

def columns
  @columns ||= ActiveSupport::OrderedHash.new
end

#group_by(column_or_lambda, &block) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/html_tables/data_table.rb', line 87

def group_by(column_or_lambda, &block)
  options[:group] = { block: block, proc: case column_or_lambda
    when String, Symbol
      ->(obj) { obj.public_send(column_or_lambda) }
    when Proc
      column_or_lambda
    else
      raise ArgumentError.new "group_by first argument must be a String, Symbol or Proc"
  end }
  self
end

#header_for(column_id) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/html_tables/data_table.rb', line 50

def header_for(column_id)
  return nil if column_id.nil?
  return @builder.(:i, nil, class: 'fa fa-check-square-o') if columns[column_id][:checkbox]
  v ||= I18n.t(column_id, scope: [:tables, options[:name] || :default], raise: true) rescue nil
  v ||= I18n.t(column_id, scope: [:tables, :default], raise: true) rescue nil
  v ||= I18n.t(column_id, scope: [:activerecord, :attributes, model_name.i18n_key], raise: true) rescue nil
  v ||= I18n.t(column_id, scope: [:mongoid, :attributes, model_name.i18n_key], raise: true) rescue nil
  v ||= I18n.t(column_id, scope: [:attributes], raise: true) rescue nil
  v ||= model_columns[column_id].human_name rescue nil

  v || column_id.to_s.humanize
end

#modelObject



23
24
25
26
27
28
29
30
31
# File 'lib/html_tables/data_table.rb', line 23

def model
  @model ||= begin
    n = collection.model_name.constantize if collection.respond_to?(:model_name)
    n = n.name if n.is_a?(ActiveModel::Name)
    n
  end
  @model ||= collection.first.try(:class)
  @model ||= options[:name].to_s.singularize.constantize
end

#model_columnsObject



38
39
40
# File 'lib/html_tables/data_table.rb', line 38

def model_columns
  @model_columns ||= ActiveSupport::HashWithIndifferentAccess[*model.columns.map { |c| [c.name, c] }.flatten]
end

#model_nameObject



33
34
35
36
# File 'lib/html_tables/data_table.rb', line 33

def model_name
  @model_name ||= collection.model_name if collection.respond_to?(:model_name)
  @model_name ||= model.model_name
end

#object_to_yieldObject



42
43
44
# File 'lib/html_tables/data_table.rb', line 42

def object_to_yield
  @ctl ||= YieldedObject.new(self)
end

#row_options_for(item) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/html_tables/data_table.rb', line 69

def row_options_for(item)
  h = {}

  url = self.url_for(item)
  h[:data] = { url: url } if url

  classes = []
  self.row_classes.each do |cls, opts|
    next if opts[:if] && !test(item, &opts[:if])
    next if opts[:unless] && test(item, &opts[:unless])
    classes << cls
  end

  h[:class] = classes.uniq * ' ' unless classes.empty?

  h
end

#test(item) {|item| ... } ⇒ Object

Yields:

  • (item)


99
100
101
# File 'lib/html_tables/data_table.rb', line 99

def test(item)
  yield item
end

#url_for(item) ⇒ Object



63
64
65
66
67
# File 'lib/html_tables/data_table.rb', line 63

def url_for(item)
  return nil unless item_url_options.fetch(:enabled, true)
  return item_url_options[:block].call(item) if item_url_options[:block]
  @builder.url_for(item) rescue nil
end