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, query, options = {}, &block) ⇒ Column

Returns a new instance of Column.



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

def initialize(grid_class, name, query, 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
  self.query = query
  options[:if] = convert_option_to_proc(options[:if])
  options[:unless] = convert_option_to_proc(options[:unless])
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

#queryObject

Returns the value of attribute query.



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

def query
  @query
end

Instance Method Details

#blockObject



141
142
143
144
# File 'lib/datagrid/columns/column.rb', line 141

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)


97
98
99
# File 'lib/datagrid/columns/column.rb', line 97

def data?
  self.data_block != nil
end

#data_value(model, grid) ⇒ Object



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

def data_value(model, grid)
  raise "no data value for #{name} column" unless data?
  result = generic_value(model,grid)
  result.is_a?(ResponseFormat) ? result.data_value : result
end

#enabled?(grid) ⇒ Boolean

Returns:

  • (Boolean)


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

def enabled?(grid)
  (!options[:if] || (options[:if] && options[:if].call(grid))) && !options[:unless] || (options[:unless] && !options[:unless].call(grid))
end

#generic_value(model, grid) ⇒ Object



146
147
148
149
150
151
152
# File 'lib/datagrid/columns/column.rb', line 146

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

#headerObject



59
60
61
62
# File 'lib/datagrid/columns/column.rb', line 59

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)


93
94
95
# File 'lib/datagrid/columns/column.rb', line 93

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

#html_value(context, asset, grid) ⇒ Object



117
118
119
120
121
122
123
124
# File 'lib/datagrid/columns/column.rb', line 117

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

#inspectObject



109
110
111
# File 'lib/datagrid/columns/column.rb', line 109

def inspect
  "#<Datagird::Columns::Column #{grid_class}##{name} #{options.inspect}>"
end

#labelObject



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

def label
  self.options[:label]
end

#mandatory?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/datagrid/columns/column.rb', line 101

def mandatory?
  !! options[:mandatory]
end

#orderObject



64
65
66
67
68
69
70
# File 'lib/datagrid/columns/column.rb', line 64

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

#order_by_value(model, grid) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/datagrid/columns/column.rb', line 76

def order_by_value(model, grid)
  if options[:order_by_value] == true
    data_value(model, grid)
  else
    Datagrid::Utils.apply_args(model, grid, &options[:order_by_value])
  end
end

#order_by_value?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/datagrid/columns/column.rb', line 84

def order_by_value?
  !! options[:order_by_value]
end

#order_descObject



88
89
90
91
# File 'lib/datagrid/columns/column.rb', line 88

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

#supports_order?Boolean

Returns:

  • (Boolean)


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

def supports_order?
  order || order_by_value?
end

#to_sObject



113
114
115
# File 'lib/datagrid/columns/column.rb', line 113

def to_s
  header
end

#value_from_html_block(context, asset, grid) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/datagrid/columns/column.rb', line 126

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