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.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/datagrid/columns/column.rb', line 36

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
end

Instance Attribute Details

#data_blockObject

Returns the value of attribute data_block.



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

def data_block
  @data_block
end

#grid_classObject

Returns the value of attribute grid_class.



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

def grid_class
  @grid_class
end

#html_blockObject

Returns the value of attribute html_block.



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

def html_block
  @html_block
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

#queryObject

Returns the value of attribute query.



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

def query
  @query
end

Instance Method Details

#append_preload(relation) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/datagrid/columns/column.rb', line 136

def append_preload(relation)
  return relation unless preload
  if preload.respond_to?(:call)
    return relation unless preload
    if preload.arity == 1
      preload.call(relation)
    else
      relation.instance_exec(&preload)
    end
  else
    driver.default_preload(relation, preload)
  end
end

#data?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/datagrid/columns/column.rb', line 103

def data?
  self.data_block != nil
end

#data_value(model, grid) ⇒ Object



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

def data_value(model, grid)
  # backward compatibility method
  grid.data_value(name, model)
end

#driverObject



161
162
163
# File 'lib/datagrid/columns/column.rb', line 161

def driver
  grid_class.driver
end

#enabled?(grid) ⇒ Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/datagrid/columns/column.rb', line 115

def enabled?(grid)
  ::Datagrid::Utils.process_availability(grid, options[:if], options[:unless])
end

#generic_value(model, grid) ⇒ Object



132
133
134
# File 'lib/datagrid/columns/column.rb', line 132

def generic_value(model, grid)
  grid.generic_value(self, model)
end

#headerObject



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

def header
  if header = options[:header]
    Datagrid::Utils.callable(header)
  else
    Datagrid::Utils.translate_from_namespace(:columns, grid_class, name)
  end
end

#html?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/datagrid/columns/column.rb', line 99

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

#html_value(context, asset, grid) ⇒ Object



127
128
129
# File 'lib/datagrid/columns/column.rb', line 127

def html_value(context, asset, grid)
  grid.html_value(name, context, asset)
end

#inspectObject



119
120
121
# File 'lib/datagrid/columns/column.rb', line 119

def inspect
  "#<#{self.class} #{grid_class}##{name} #{options.inspect}>"
end

#labelObject



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

def label
  self.options[:label]
end

#mandatory?Boolean

Returns:

  • (Boolean)


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

def mandatory?
  !! options[:mandatory]
end

#mandatory_explicitly_set?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/datagrid/columns/column.rb', line 111

def mandatory_explicitly_set?
  options.key?(:mandatory)
end

#orderObject



70
71
72
73
74
75
76
# File 'lib/datagrid/columns/column.rb', line 70

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

#order_by_value(model, grid) ⇒ Object



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

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

#order_by_value?Boolean

Returns:

  • (Boolean)


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

def order_by_value?
  !! options[:order_by_value]
end

#order_descObject



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

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

#preloadObject



150
151
152
153
154
155
156
157
158
159
# File 'lib/datagrid/columns/column.rb', line 150

def preload
  preload = options[:preload]

  if preload == true && driver.can_preload?(grid_class.scope, name)
    name
  else
    preload
  end

end

#supports_order?Boolean

Returns:

  • (Boolean)


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

def supports_order?
  order || order_by_value?
end

#to_sObject



123
124
125
# File 'lib/datagrid/columns/column.rb', line 123

def to_s
  header
end