lib/pivot_table/grid.rb in pivot_table-0.3.0 vs lib/pivot_table/grid.rb in pivot_table-0.4.0
- old
+ new
@@ -1,9 +1,9 @@
module PivotTable
class Grid
- attr_accessor :source_data, :row_name, :column_name, :value_name
+ attr_accessor :source_data, :row_name, :column_name, :value_name, :field_name
attr_reader :columns, :rows, :data_grid, :configuration
DEFAULT_OPTIONS = {
:sort => true
}
@@ -75,11 +75,13 @@
def populate_grid
prepare_grid
row_headers.each_with_index do |row, row_index|
current_row = []
column_headers.each_with_index do |col, col_index|
- current_row[col_index] = @source_data.find { |item| item.send(row_name) == row && item.send(column_name) == col }
+ object = @source_data.find { |item| item.send(row_name) == row && item.send(column_name) == col }
+ has_field_name = field_name && object.respond_to?(field_name)
+ current_row[col_index] = has_field_name ? object.send(field_name) : object
end
@data_grid[row_index] = current_row
end
@data_grid
end
@@ -88,8 +90,7 @@
def headers(method)
hdrs = @source_data.collect { |c| c.send method }.uniq
configuration.sort ? hdrs.sort : hdrs
end
-
end
end