Module: Datagrid::Columns::InstanceMethods

Defined in:
lib/datagrid/columns.rb

Overview

ClassMethods

Instance Method Summary collapse

Instance Method Details

#column_by_name(name) ⇒ Object

Finds a column definition by name



296
297
298
# File 'lib/datagrid/columns.rb', line 296

def column_by_name(name)
  self.class.column_by_name(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


275
276
277
# File 'lib/datagrid/columns.rb', line 275

def columns(*args)
  self.class.columns(*args)
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


211
212
213
# File 'lib/datagrid/columns.rb', line 211

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



280
281
282
283
284
285
# File 'lib/datagrid/columns.rb', line 280

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


231
232
233
234
235
# File 'lib/datagrid/columns.rb', line 231

def data_hash
  map_with_batches do |asset|
    hash_for(asset)
  end
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


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

def format(value, &block)
  if block_given?
    self.class.format(value, &block)
  else
    super
  end
end

#hash_for(asset) ⇒ Object

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



187
188
189
190
191
192
193
# File 'lib/datagrid/columns.rb', line 187

def hash_for(asset)
  result = {}
  self.data_columns.each do |column|
    result[column.name] = column.data_value(asset, self)
  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


171
172
173
# File 'lib/datagrid/columns.rb', line 171

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



288
289
290
291
292
293
# File 'lib/datagrid/columns.rb', line 288

def html_columns(*names)
  options = names.extract_options!
  options[:html] = true
  names << options
  self.columns(*names)
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


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

def row_for(asset, *column_names)
  data_columns(*column_names).map do |column|
    column.data_value(asset, self)
  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


200
201
202
203
204
# File 'lib/datagrid/columns.rb', line 200

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


248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/datagrid/columns.rb', line 248

def to_csv(*column_names)
  options = column_names.extract_options!
  klass = if RUBY_VERSION >= "1.9"
            require 'csv'
            CSV
          else
            require "fastercsv"
            FasterCSV
          end
  klass.generate(
    {:headers => self.header(*column_names), :write_headers => true}.merge(options)
  ) do |csv|
    self.rows(*column_names).each do |row|
      csv << row
    end
  end
end