Class: Datagrid::Columns::Column

Inherits:
Object
  • Object
show all
Defined in:
lib/datagrid/columns/column.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(grid, name, options = {}, &block) ⇒ Column

Returns a new instance of Column.



6
7
8
9
10
11
# File 'lib/datagrid/columns/column.rb', line 6

def initialize(grid, name, options = {}, &block)
  self.grid = grid
  self.name = name.to_sym
  self.options = options
  self.block = block
end

Instance Attribute Details

#blockObject

Returns the value of attribute block.



4
5
6
# File 'lib/datagrid/columns/column.rb', line 4

def block
  @block
end

#gridObject

Returns the value of attribute grid.



4
5
6
# File 'lib/datagrid/columns/column.rb', line 4

def grid
  @grid
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/datagrid/columns/column.rb', line 4

def name
  @name
end

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/datagrid/columns/column.rb', line 4

def options
  @options
end

Instance Method Details

#formatObject



27
28
29
# File 'lib/datagrid/columns/column.rb', line 27

def format
  self.options[:format]
end

#headerObject



35
36
37
38
# File 'lib/datagrid/columns/column.rb', line 35

def header
  self.options[:header] || 
    I18n.translate(self.name, :scope => "reports.#{self.grid.param_name}.columns", :default => self.name.to_s.humanize )
end

#labelObject



31
32
33
# File 'lib/datagrid/columns/column.rb', line 31

def label
  self.options[:label]
end

#orderObject



40
41
42
43
44
45
46
# File 'lib/datagrid/columns/column.rb', line 40

def order
  if options.has_key?(:order)
    self.options[:order]
  else
    grid.scope.column_names.include?(name.to_s) ? [grid.scope.table_name, name].join(".") : nil
  end
end

#order_descObject



48
49
50
51
# File 'lib/datagrid/columns/column.rb', line 48

def order_desc
  return nil unless order
  self.options[:order_desc]  
end

#value(model, report) ⇒ Object



13
14
15
# File 'lib/datagrid/columns/column.rb', line 13

def value(model, report)
  value_for(model, report)
end

#value_for(model, report) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/datagrid/columns/column.rb', line 17

def value_for(model, report)
  if self.block.arity == 1
    self.block.call(model)
  elsif self.block.arity == 2
    self.block.call(model, report)
  else
    model.instance_eval(&self.block)
  end
end