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.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/datagrid/columns/column.rb', line 38

def initialize(grid_class, name, query, options = {}, &block)
  self.grid_class = grid_class
  self.name = name.to_sym
  self.options = options
  if options[:class]
    Datagrid::Utils.warn_once(
      "column[class] option is deprecated. Use {tag_options: {class: ...}} instead.",
    )
    self.options[:tag_options] = {
      **self.options.fetch(:tag_options, {}),
      class: options[:class],
    }
  end
  if options[:html] == true
    self.html_block = block
  else
    self.data_block = block

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

Instance Attribute Details

#data_blockObject

Returns the value of attribute data_block.



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

def data_block
  @data_block
end

#grid_classObject

Returns the value of attribute grid_class.



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

def grid_class
  @grid_class
end

#html_blockObject

Returns the value of attribute html_block.



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

def html_block
  @html_block
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

#queryObject

Returns the value of attribute query.



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

def query
  @query
end

Instance Method Details

#append_preload(relation) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/datagrid/columns/column.rb', line 155

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)


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

def data?
  data_block != nil
end

#data_value(model, grid) ⇒ Object



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

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

#driverObject



181
182
183
# File 'lib/datagrid/columns/column.rb', line 181

def driver
  grid_class.driver
end

#enabled?(grid) ⇒ Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/datagrid/columns/column.rb', line 135

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

#generic_value(model, grid) ⇒ Object



151
152
153
# File 'lib/datagrid/columns/column.rb', line 151

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

#headerObject



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

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)


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

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

#html_classObject



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

def html_class
  Datagrid::Utils.warn_once(
    "Column#html_class is deprecated. Use Column#tag_options instead.",
  )
  options[:class]
end

#html_value(context, asset, grid) ⇒ Object



147
148
149
# File 'lib/datagrid/columns/column.rb', line 147

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

#inspectObject



139
140
141
# File 'lib/datagrid/columns/column.rb', line 139

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

#labelObject



66
67
68
# File 'lib/datagrid/columns/column.rb', line 66

def label
  options[:label]
end

#mandatory?Boolean

Returns:

  • (Boolean)


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

def mandatory?
  !!options[:mandatory]
end

#mandatory_explicitly_set?Boolean

Returns:

  • (Boolean)


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

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

#orderObject



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

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

#order_by_value(model, grid) ⇒ Object



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

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)


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

def order_by_value?
  !!options[:order_by_value]
end

#order_descObject



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

def order_desc
  return nil unless order

  options[:order_desc]
end

#preloadObject



171
172
173
174
175
176
177
178
179
# File 'lib/datagrid/columns/column.rb', line 171

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)


86
87
88
# File 'lib/datagrid/columns/column.rb', line 86

def supports_order?
  order || order_by_value?
end

#tag_optionsObject



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

def tag_options
  options[:tag_options] || {}
end

#to_sObject



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

def to_s
  header
end