Skip to content

Commit

Permalink
added pivot
Browse files Browse the repository at this point in the history
  • Loading branch information
tsonntag committed Mar 19, 2012
1 parent 0430a85 commit 580ca9c
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 8 deletions.
48 changes: 48 additions & 0 deletions lib/gitter/axis.rb
@@ -0,0 +1,48 @@
module Gitter

class Axis

attr_reader :grid, :name, :attr

def initialize grid, name, opts = {}
@grid, @name = grid, name
@attr = opts.delete(:column){name}
only = opts.delete(:only){nil}
case only
when Hash
@only_data, @titles= only.keys, only
else
@only_data, @titles = only, nil
end
@except = opts.delete(:except){[]}
end


def data
data = case attr
when Symbol,String
grid.scope.select(attr).uniq.map(&:"#{attr}").sort
else
attr
end

data = (data & @only_data) if @only_data
data = data - @except
end

def titles
if @titles
data.map{|d|@titles[d]}
else
data
end
end

def data_titles
res = {}
data.each{|d| res[d] = @titles ? @titles[d] : d}
res
end

end
end
3 changes: 1 addition & 2 deletions lib/gitter/base.rb
Expand Up @@ -25,12 +25,11 @@ def initialize *args
@decorator = Artdeco::Decorator.new *args, opts
@params = @decorator.params.fetch(key){{}}.symbolize_keys

@filters, @facets = {}, []
@filters, @values, @facets = {}, {}, []
instance_eval &self.class.grid

@scope = opts.delete(:scope){@scope}

@values = {}
@decorator.params.symbolize_keys.each do |name, value|
if (name != key) and (filter = @filters[name]) and not filter.param_scoped?
@values[name] = value
Expand Down
2 changes: 2 additions & 0 deletions lib/gitter/column.rb
Expand Up @@ -31,6 +31,7 @@ def initialize grid, name, opts = {}, &block
end

def cells model
puts "BBBBBBBBBBB cells #{name}, model=#{model.inspect}"
res = if map && Array === model
model.map{|m| cells m}
else
Expand Down Expand Up @@ -132,6 +133,7 @@ def params
end

def cell model
puts "AAAAAAAAAA #{name}, model=#{model.inspect}"
grid.decorate model
if block
content = grid.eval block, model
Expand Down
6 changes: 3 additions & 3 deletions lib/gitter/columns.rb
Expand Up @@ -74,9 +74,9 @@ def rows_for model
end.transpose
end

def rows scope = self.scope
def rows scope = nil
res = []
models(scope).each{|model| res += rows_for(model)}
models(scope||self.scope).each{|model| res += rows_for(model)}
res
end

Expand All @@ -89,7 +89,7 @@ def models scope = self.scope
end

def columns
@columns.values
(@columns||={}).values
end

def order_column
Expand Down
2 changes: 1 addition & 1 deletion lib/gitter/filters/abstract_filter.rb
Expand Up @@ -48,7 +48,7 @@ def input_tag
return '' unless input?

@input_tag ||= if col = collection
col = [''] + col if include_blank?
col = [''] + col if include_blank? && col.size > 1
select_tag col
else
text_field_tag
Expand Down
2 changes: 1 addition & 1 deletion lib/gitter/filters/select_filter.rb
Expand Up @@ -7,7 +7,7 @@ class SelectFilter < AbstractFilter
def initialize grid, name, filters, opts = {}
super grid, name, opts
@filters = filters.inject({}){|memo,filter| memo[filter.name] = filter; memo}
if @input_options
if @input_options == true
@input_options = {}
@input_options[:collection] = @filters.keys
end
Expand Down
2 changes: 1 addition & 1 deletion lib/gitter/helpers.rb
Expand Up @@ -7,7 +7,7 @@ def name

# used to scope params of requests
def key
@key ||= name.intern
@key ||= :grid#name.intern
end

def scoped_params params
Expand Down
70 changes: 70 additions & 0 deletions lib/gitter/pivot.rb
@@ -0,0 +1,70 @@
module Gitter

module Pivot

def x_axis *args
if args.present?
@x_axis = Axis.new self, *args
else
@x_axis or raise ConfigurationError, 'undefined x_axis'
end
end

def y_axis *args
if args.present?
@y_axis = Axis.new self, *args
else
@y_axis or raise ConfigurationError, 'undefined y_axis'
end
end

def cell &cell
if cell
@cell ||= cell
else
@cell or raise ConfigurationError, 'undefined cell'
end
end

def drill_down *names
if names.present?
@drill_down ||= [names].flatten.map{|name| @filters[name] or raise ConfigurationError, "unknown filter #{name}"}
else
@drill_down ||= @filters
end
end

def input_tags
[]
end

def columns
@columns ||= x_axis.titles.map{|x|Column.new self, x}
end

def header_rows
@header_rows ||= begin
row = [''] + x_axis.titles
row = row.map{|h|Gitter::Header.new self, h}
[row]
end
end

def rows scope = self.scope
y_axis.data_titles.map do |y,y_title|
row = []
row << Gitter::Cell.new(y_title)
row += x_axis.data.map do |x|
content = cell.call data_scope, x, y
Gitter::Cell.new content
end
end
end

private
def data_scope
@data_scope ||= scope.group(x_axis.attr).group(y_axis.attr)
end

end
end
23 changes: 23 additions & 0 deletions lib/gitter/pivot_grid.rb
@@ -0,0 +1,23 @@
require 'gitter/driver'
require 'gitter/base'
require 'gitter/pivot'
require 'gitter/breadcrumbs'
require 'gitter/csv'
require 'gitter/i18n'
require 'gitter/helpers'
require 'gitter/model'

module Gitter

class PivotGrid
include Gitter::Base
include Gitter::Driver
include Gitter::Pivot
include Gitter::Breadcrumbs
include Gitter::CSV
include Gitter::I18n
include Gitter::Helpers
include Gitter::Model
end

end

0 comments on commit 580ca9c

Please sign in to comment.