Module: Datagrid::Columns::InstanceMethods

Defined in:
lib/datagrid/columns.rb

Overview

ClassMethods

Instance Method Summary collapse

Instance Method Details

#assetsObject



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

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

#available_columnsObject

Returns all columns available for current grid configuration.

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> ]


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

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



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

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

#column_by_name(name) ⇒ Object

Finds a column definition by name



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

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


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

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


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

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



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

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"}]


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

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


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

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



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

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.



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

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


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

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:



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

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



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

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


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

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



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

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



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

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:



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

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


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

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


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

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 => ';')


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

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