Class: RailsTables::Column

Inherits:
Object
  • Object
show all
Defined in:
lib/rails-tables/datatable/column.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table_name, model, name, order, *args) ⇒ Column

Returns a new instance of Column.



5
6
7
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
# File 'lib/rails-tables/datatable/column.rb', line 5

def initialize(table_name, model, name, order, *args)
  self.table_name = table_name
  self.model = model
  self.name = name
  self.order = order

  attributes = args.pop || {}
  self.method = attributes.fetch(:method, name).to_s
  self.column_source = attributes.fetch(:column_source, '').to_s

  virtual = (self.column_source.blank? and not self.model.column_names.include? self.method)
  self.virtual = attributes.fetch(:virtual, virtual)
  self.sortable = attributes.fetch(:sortable, !self.virtual)
  self.searchable = attributes.fetch(:searchable, !self.virtual)

  if virtual and not attributes.has_key?(:render_with)
    raise Exception,
      "Virtual columns are required to supply a render method (render_with: lambda): "\
      "Column: #{self.name}, Datatable: #{self.table_name}, Model: #{self.model.name}"
  end

  self.render_with = attributes.fetch(:render_with, :default_render)
  self.blank_value = attributes.fetch(:blank_value, '–')

  define_singleton_method :render do |view, object|
    if self.render_with.kind_of? Symbol
      content = self.send(self.render_with, view, follow_source(object))
    else
      content = self.render_with.call(view, object)
    end
    content.present? ? content.to_s.html_safe : self.blank_value
  end
end

Instance Attribute Details

#blank_valueObject

Returns the value of attribute blank_value.



3
4
5
# File 'lib/rails-tables/datatable/column.rb', line 3

def blank_value
  @blank_value
end

#column_sourceObject

Returns the value of attribute column_source.



3
4
5
# File 'lib/rails-tables/datatable/column.rb', line 3

def column_source
  @column_source
end

#methodObject

Returns the value of attribute method.



3
4
5
# File 'lib/rails-tables/datatable/column.rb', line 3

def method
  @method
end

#modelObject

Returns the value of attribute model.



3
4
5
# File 'lib/rails-tables/datatable/column.rb', line 3

def model
  @model
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/rails-tables/datatable/column.rb', line 3

def name
  @name
end

#orderObject

Returns the value of attribute order.



3
4
5
# File 'lib/rails-tables/datatable/column.rb', line 3

def order
  @order
end

#render_withObject

Returns the value of attribute render_with.



3
4
5
# File 'lib/rails-tables/datatable/column.rb', line 3

def render_with
  @render_with
end

#searchableObject

Returns the value of attribute searchable.



3
4
5
# File 'lib/rails-tables/datatable/column.rb', line 3

def searchable
  @searchable
end

#sortableObject

Returns the value of attribute sortable.



3
4
5
# File 'lib/rails-tables/datatable/column.rb', line 3

def sortable
  @sortable
end

#table_nameObject

Returns the value of attribute table_name.



3
4
5
# File 'lib/rails-tables/datatable/column.rb', line 3

def table_name
  @table_name
end

#virtualObject

Returns the value of attribute virtual.



3
4
5
# File 'lib/rails-tables/datatable/column.rb', line 3

def virtual
  @virtual
end