Module: Datagrid::Columns::InstanceMethods

Defined in:
lib/datagrid/columns.rb

Overview

ClassMethods

Instance Method Summary collapse

Instance Method Details

#assetsObject


226
227
228
229
230
231
232
# File 'lib/datagrid/columns.rb', line 226

def assets
  append_column_preload(
    driver.append_column_queries(
      super, columns.select(&:query)
    )
  )
end

#available_columnsObject

Returns all columns that are possible to be displayed for the current grid object

class MyGrid
  filter(:search) {|scope, value| scope.full_text_search(value)}
  column(:id)
  column(:name, :mandatory => true)
  column(:search_match, :if => proc {|grid| grid.search.present? }) do |model, grid|
    search_match_line(model.searchable_content, grid.search)
  end
end

grid = MyGrid.new
grid.columns # => [ #<Column:name> ]
grid.available_columns # => [ #<Column:id>, #<Column:name> ]
grid.search = "keyword"
grid.available_columns # => [ #<Column:id>, #<Column:name>, #<Column:search_match> ]

435
436
437
438
439
# File 'lib/datagrid/columns.rb', line 435

def available_columns
  columns_array.select do |column|
    column.enabled?(self)
  end
end

#column(name, options_or_query = {}, options = {}, &block) ⇒ Object

Defines a column at instance level

See Datagrid::Columns::ClassMethods#column for more info


409
410
411
# File 'lib/datagrid/columns.rb', line 409

def column(name, options_or_query = {}, options = {}, &block)
  self.class.define_column(columns_array, name, options_or_query, options, &block)
end

#column_by_name(name) ⇒ Object

Finds a column definition by name


357
358
359
# File 'lib/datagrid/columns.rb', line 357

def column_by_name(name)
  self.class.find_column_by_name(columns_array, name)
end

#columns(*args) ⇒ Object

Returns all columns selected in grid instance

Examples:

MyGrid.new.columns # => all defined columns
grid = MyGrid.new(:column_names => [:id, :name])
grid.columns # => id and name columns
grid.columns(:id, :category) # => id and category column

336
337
338
# File 'lib/datagrid/columns.rb', line 336

def columns(*args)
  self.class.filter_columns(columns_array, *args).select {|column| column.enabled?(self)}
end

#data(*column_names) ⇒ Object

Returns Array of Arrays with data for each row in datagrid assets with header.

Arguments:

* <tt>column_names</tt> - list of column names if you want to limit data only to specified columns

279
280
281
# File 'lib/datagrid/columns.rb', line 279

def data(*column_names)
  self.rows(*column_names).unshift(self.header(*column_names))
end

#data_columns(*names) ⇒ Object

Returns all columns that can be represented in plain data(non-html) way


341
342
343
344
345
346
# File 'lib/datagrid/columns.rb', line 341

def data_columns(*names)
  options = names.extract_options!
  options[:data] = true
  names << options
  self.columns(*names)
end

#data_hashObject

Return Array of Hashes where keys are column names and values are column values for each row in filtered datagrid relation.

Example:

class MyGrid
  scope { Model }
  column(:id)
  column(:name)
end

Model.create!(:name => "One")
Model.create!(:name => "Two")

MyGrid.new.data_hash # => [{:name => "One"}, {:name => "Two"}]

299
300
301
302
303
# File 'lib/datagrid/columns.rb', line 299

def data_hash
  map_with_batches do |asset|
    hash_for(asset)
  end
end

#data_row(asset) ⇒ Object

Returns an object representing a grid row. Allows to access column values

Example:

class MyGrid
  scope { User }
  column(:id)
  column(:name)
  column(:number_of_purchases) do |user|
    user.purchases.count
  end
end

row = MyGrid.new.data_row(User.last)
row.id # => user.id
row.number_of_purchases # => user.purchases.count

402
403
404
# File 'lib/datagrid/columns.rb', line 402

def data_row(asset)
  ::Datagrid::Columns::DataRow.new(self, asset)
end

#data_value(column_name, asset) ⇒ Object

Return a cell data value for given column name and asset


442
443
444
445
446
447
448
449
# File 'lib/datagrid/columns.rb', line 442

def data_value(column_name, asset)
  column = column_by_name(column_name)
  cache(column, asset, :data_value) do
    raise "no data value for #{column.name} column" unless column.data?
    result = generic_value(column, asset)
    result.is_a?(Datagrid::Columns::Column::ResponseFormat) ? result.call_data : result
  end
end

#decorate(model) ⇒ Object

Returns a decorated version of given model if decorator is specified or the model otherwise.


465
466
467
# File 'lib/datagrid/columns.rb', line 465

def decorate(model)
  self.class.decorate(model)
end

#format(value, &block) ⇒ Object

Gives ability to have a different formatting for CSV and HTML column value.

Example:

column(:name) do |model|
  format(model.name) do |value|
    (:strong, value)
  end
end

column(:company) do |model|
  format(model.company.name) do
    render :partial => "company_with_logo", :locals => {:company => model.company }
  end
end

376
377
378
379
380
381
382
383
# File 'lib/datagrid/columns.rb', line 376

def format(value, &block)
  if block_given?
    self.class.format(value, &block)
  else
    # don't override Object#format method
    super
  end
end

#generic_value(column, model) ⇒ Object

:nodoc:


469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
# File 'lib/datagrid/columns.rb', line 469

def generic_value(column, model) #:nodoc:
  cache(column, model, :generic_value) do
    presenter = decorate(model)
    unless column.enabled?(self)
      raise Datagrid::ColumnUnavailableError, "Column #{column.name} disabled for #{inspect}"
    end

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

end

#hash_for(asset) ⇒ Object

Returns Hash where keys are column names and values are column values for the given asset


255
256
257
258
259
260
261
# File 'lib/datagrid/columns.rb', line 255

def hash_for(asset)
  result = {}
  self.data_columns.each do |column|
    result[column.name] = data_value(column, asset)
  end
  result
end

#header(*column_names) ⇒ Object

Returns Array of human readable column names. See also “Localization” section

Arguments:

* <tt>column_names</tt> - list of column names if you want to limit data only to specified columns

239
240
241
# File 'lib/datagrid/columns.rb', line 239

def header(*column_names)
  data_columns(*column_names).map(&:header)
end

#html_columns(*names) ⇒ Object

Returns all columns that can be represented in HTML table


349
350
351
352
353
354
# File 'lib/datagrid/columns.rb', line 349

def html_columns(*names)
  options = names.extract_options!
  options[:html] = true
  names << options
  self.columns(*names)
end

#html_value(column_name, context, asset) ⇒ Object

Return a cell HTML value for given column name and asset and view context


452
453
454
455
456
457
458
459
460
461
462
# File 'lib/datagrid/columns.rb', line 452

def html_value(column_name, context, asset)
  column  = column_by_name(column_name)
  cache(column, asset, :html_value) do
    if column.html? && column.html_block
      value_from_html_block(context, asset, column)
    else
      result = generic_value(column, asset)
      result.is_a?(Datagrid::Columns::Column::ResponseFormat) ? result.call_html(context) : result
    end
  end
end

#initializeObject

:nodoc:


413
414
415
416
# File 'lib/datagrid/columns.rb', line 413

def initialize(*) #:nodoc:
  self.columns_array = self.class.columns_array.clone
  super
end

#row_for(asset, *column_names) ⇒ Object

Returns Array column values for given asset

Arguments:

* <tt>column_names</tt> - list of column names if you want to limit data only to specified columns

248
249
250
251
252
# File 'lib/datagrid/columns.rb', line 248

def row_for(asset, *column_names)
  data_columns(*column_names).map do |column|
    data_value(column, asset)
  end
end

#rows(*column_names) ⇒ Object

Returns Array of Arrays with data for each row in datagrid assets without header.

Arguments:

* <tt>column_names</tt> - list of column names if you want to limit data only to specified columns

268
269
270
271
272
# File 'lib/datagrid/columns.rb', line 268

def rows(*column_names)
  map_with_batches do |asset|
    self.row_for(asset, *column_names)
  end
end

#to_csv(*column_names) ⇒ Object

Returns a CSV representation of the data in the grid You are able to specify which columns you want to see in CSV. All data columns are included by default Also you can specify options hash as last argument that is proxied to Ruby CSV library.

Example:

grid.to_csv
grid.to_csv(:id, :name)
grid.to_csv(:col_sep => ';')

316
317
318
319
320
321
322
323
324
325
# File 'lib/datagrid/columns.rb', line 316

def to_csv(*column_names)
  require "csv"
  options = column_names.extract_options!
  options = {:headers => self.header(*column_names), :write_headers => true}.merge!(options)
  CSV.generate(**options) do |csv|
    each_with_batches do |asset|
      csv << row_for(asset, *column_names)
    end
  end
end