Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added pivot

  • Loading branch information...
commit 580ca9c7c9af0d51417e94b2dfecfca0ed704852 1 parent 0430a85
@tracksun authored
View
48 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
View
3  lib/gitter/base.rb
@@ -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
View
2  lib/gitter/column.rb
@@ -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
@@ -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
View
6 lib/gitter/columns.rb
@@ -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
@@ -89,7 +89,7 @@ def models scope = self.scope
end
def columns
- @columns.values
+ (@columns||={}).values
end
def order_column
View
2  lib/gitter/filters/abstract_filter.rb
@@ -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
View
2  lib/gitter/filters/select_filter.rb
@@ -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
View
2  lib/gitter/helpers.rb
@@ -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
View
70 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
View
23 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
Please sign in to comment.
Something went wrong with that request. Please try again.