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.


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

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


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

def data_block
  @data_block
end

#grid_classObject

Returns the value of attribute grid_class


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

def grid_class
  @grid_class
end

#html_blockObject

Returns the value of attribute html_block


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

def html_block
  @html_block
end

#nameObject

Returns the value of attribute name


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

def name
  @name
end

#optionsObject

Returns the value of attribute options


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

def options
  @options
end

#queryObject

Returns the value of attribute query


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

def query
  @query
end

Instance Method Details

#append_preload(scope) ⇒ Object


130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/datagrid/columns/column.rb', line 130

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

#data?Boolean

Returns:

  • (Boolean)

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

def data?
  self.data_block != nil
end

#data_value(model, grid) ⇒ Object


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

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

#driverObject


155
156
157
# File 'lib/datagrid/columns/column.rb', line 155

def driver
  grid_class.driver
end

#enabled?(grid) ⇒ Boolean

Returns:

  • (Boolean)

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

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

#generic_value(model, grid) ⇒ Object


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

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

#headerObject


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

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)

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

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

#html_value(context, asset, grid) ⇒ Object


121
122
123
# File 'lib/datagrid/columns/column.rb', line 121

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

#inspectObject


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

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

#labelObject


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

def label
  self.options[:label]
end

#mandatory?Boolean

Returns:

  • (Boolean)

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

def mandatory?
  !! options[:mandatory]
end

#orderObject


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

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


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

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)

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

def order_by_value?
  !! options[:order_by_value]
end

#order_descObject


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

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

#preloadObject


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

def preload
  preload = options[:preload]

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

end

#supports_order?Boolean

Returns:

  • (Boolean)

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

def supports_order?
  order || order_by_value?
end

#to_sObject


117
118
119
# File 'lib/datagrid/columns/column.rb', line 117

def to_s
  header
end