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
22
23
# File 'lib/html_tables/data_table.rb', line 16

def auto_generate_columns!
  raise 'Could not auto generate columns' unless model.respond_to? :accessible_attributes

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

#columnsObject



48
49
50
# File 'lib/html_tables/data_table.rb', line 48

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

#group_by(field_or_proc, &block) ⇒ Object

Groups records on the table.

Parameters:

  • field_or_proc (Symbol, Proc)

    The field name (or Proc) which will be used to group the records.

  • block (Proc)

    Specifies the block to render on each group. It will be rendered as a full-width row.



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/html_tables/data_table.rb', line 92

def group_by(field_or_proc, &block)
  proc = case field_or_proc
           when String, Symbol
             lambda { |obj| obj.public_send(field_or_proc) }
           when Proc
             field_or_proc
           else
             raise ArgumentError, 'group_by first argument must be a String, Symbol or Proc'
         end

  options[:group] = { block: block, proc: proc }
  self
end

#header_for(column_id) ⇒ Object



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

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



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

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

#model_columnsObject



40
41
42
# File 'lib/html_tables/data_table.rb', line 40

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

#model_nameObject



35
36
37
38
# File 'lib/html_tables/data_table.rb', line 35

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

#object_to_yieldObject



44
45
46
# File 'lib/html_tables/data_table.rb', line 44

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

#row_options_for(item) ⇒ Object



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

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)


106
107
108
# File 'lib/html_tables/data_table.rb', line 106

def test(item)
  yield item
end

#url_for(item) ⇒ Object



65
66
67
68
69
# File 'lib/html_tables/data_table.rb', line 65

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