Module: Datagrid::Columns::InstanceMethods
- Defined in:
- lib/datagrid/columns.rb
Overview
ClassMethods
Instance Method Summary collapse
-
#column_by_name(name) ⇒ Object
Finds a column by name.
-
#columns(*args) ⇒ Object
Returns all columns selected in grid instance.
-
#data(*column_names) ⇒ Object
Returns Array of Arrays with data for each row in datagrid assets with header.
-
#data_columns(*names) ⇒ Object
Returns all columns that can be represented in plain data(non-html) way.
-
#data_hash ⇒ Object
Return Array of Hashes where keys are column names and values are column values for each row in datagrid
#assets. - #format(value, &block) ⇒ Object
-
#hash_for(asset) ⇒ Object
Returns
Hashwhere keys are column names and values are column values for the given asset. -
#header(*column_names) ⇒ Object
Returns
Arrayof human readable column names. -
#html_columns(*names) ⇒ Object
Returns all columns that can be represented in HTML table.
-
#row_for(asset, *column_names) ⇒ Object
Returns
Arraycolumn values for given asset. -
#rows(*column_names) ⇒ Object
Returns Array of Arrays with data for each row in datagrid assets without header.
-
#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.
Instance Method Details
#column_by_name(name) ⇒ Object
Finds a column by name
238 239 240 |
# File 'lib/datagrid/columns.rb', line 238 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
217 218 219 |
# File 'lib/datagrid/columns.rb', line 217 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
153 154 155 |
# File 'lib/datagrid/columns.rb', line 153 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
222 223 224 225 226 227 |
# File 'lib/datagrid/columns.rb', line 222 def data_columns(*names) = names. [:data] = true names << self.columns(*names) end |
#data_hash ⇒ Object
Return Array of Hashes where keys are column names and values are column values for each row in datagrid #assets
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"}]
173 174 175 176 177 |
# File 'lib/datagrid/columns.rb', line 173 def data_hash self.assets.map do |asset| hash_for(asset) end end |
#format(value, &block) ⇒ Object
243 244 245 246 247 248 249 |
# File 'lib/datagrid/columns.rb', line 243 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
128 129 130 131 132 133 134 |
# File 'lib/datagrid/columns.rb', line 128 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
112 113 114 |
# File 'lib/datagrid/columns.rb', line 112 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
230 231 232 233 234 235 |
# File 'lib/datagrid/columns.rb', line 230 def html_columns(*names) = names. [:html] = true names << 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
121 122 123 124 125 |
# File 'lib/datagrid/columns.rb', line 121 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
141 142 143 144 145 146 |
# File 'lib/datagrid/columns.rb', line 141 def rows(*column_names) #TODO: find in batches self.assets.map 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 => ';')
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/datagrid/columns.rb', line 190 def to_csv(*column_names) = column_names. 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() ) do |csv| self.rows(*column_names).each do |row| csv << row end end end |