Class: Datagrid::Columns::Column

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

Defined Under Namespace

Classes: ResponseFormat

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Column.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/datagrid/columns/column.rb', line 30

def initialize(grid_class, name, options = {}, &block)
  self.grid_class = grid_class
  self.name = name.to_sym
  self.options = options
  if options[:html] == true
    self.html_block = block
  else
    self.data_block = block

    if options[:html].is_a? Proc
      self.html_block = options[:html]
    end
  end
end

Instance Attribute Details

#data_blockObject

Returns the value of attribute data_block.



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

def data_block
  @data_block
end

#grid_classObject

Returns the value of attribute grid_class.



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

def grid_class
  @grid_class
end

#html_blockObject

Returns the value of attribute html_block.



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

def html_block
  @html_block
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#blockObject



105
106
107
108
# File 'lib/datagrid/columns/column.rb', line 105

def block
  Datagrid::Utils.warn_once("Datagrid::Columns::Column#block is deprecated. Use #html_block or #data_block instead")
  data_block
end

#data?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/datagrid/columns/column.rb', line 77

def data?
  self.data_block != nil
end

#data_value(model, grid) ⇒ Object



45
46
47
48
# File 'lib/datagrid/columns/column.rb', line 45

def data_value(model, grid)
  result = generic_value(model,grid)
  result.is_a?(ResponseFormat) ? result.data_value : result
end

#generic_value(model, grid) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/datagrid/columns/column.rb', line 110

def generic_value(model, grid)
  if self.data_block.arity >= 1
    Datagrid::Utils.apply_args(model, grid, &data_block)
  else
    model.instance_eval(&self.data_block)
  end
end

#headerObject



55
56
57
58
# File 'lib/datagrid/columns/column.rb', line 55

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

#html?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/datagrid/columns/column.rb', line 73

def html?
  options[:html] != false
end

#html_value(context, asset, grid) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/datagrid/columns/column.rb', line 81

def html_value(context, asset, grid)
  if html? && html_block
    value_from_html_block(context, asset, grid)
  else
    result = generic_value(asset,grid)
    result.is_a?(ResponseFormat) ? result.html_value(context) : result
  end
end

#labelObject



51
52
53
# File 'lib/datagrid/columns/column.rb', line 51

def label
  self.options[:label]
end

#orderObject



60
61
62
63
64
65
66
# File 'lib/datagrid/columns/column.rb', line 60

def order
  if options.has_key?(:order)
    self.options[:order]
  else
    grid_class.driver.default_order(grid_class.scope, name)
  end
end

#order_descObject



68
69
70
71
# File 'lib/datagrid/columns/column.rb', line 68

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

#value_from_html_block(context, asset, grid) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/datagrid/columns/column.rb', line 90

def value_from_html_block(context, asset, grid)
  args = []
  remaining_arity = html_block.arity

  if data?
    args << data_value(asset,grid)
    remaining_arity -= 1
  end

  args << asset if remaining_arity > 0
  args << grid if remaining_arity > 1

  return context.instance_exec(*args, &html_block)
end