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
272 273 274 |
# File 'lib/datagrid/columns.rb', line 272 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
251 252 253 |
# File 'lib/datagrid/columns.rb', line 251 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
187 188 189 |
# File 'lib/datagrid/columns.rb', line 187 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
256 257 258 259 260 261 |
# File 'lib/datagrid/columns.rb', line 256 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"}]
207 208 209 210 211 |
# File 'lib/datagrid/columns.rb', line 207 def data_hash self.assets.map do |asset| hash_for(asset) end end |
#format(value, &block) ⇒ Object
277 278 279 280 281 282 283 |
# File 'lib/datagrid/columns.rb', line 277 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
162 163 164 165 166 167 168 |
# File 'lib/datagrid/columns.rb', line 162 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
146 147 148 |
# File 'lib/datagrid/columns.rb', line 146 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
264 265 266 267 268 269 |
# File 'lib/datagrid/columns.rb', line 264 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
155 156 157 158 159 |
# File 'lib/datagrid/columns.rb', line 155 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
175 176 177 178 179 180 |
# File 'lib/datagrid/columns.rb', line 175 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 => ';')
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/datagrid/columns.rb', line 224 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 |