Skip to content

Instantly share code, notes, and snippets.

@gisikw
Created December 10, 2010 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gisikw/736483 to your computer and use it in GitHub Desktop.
Save gisikw/736483 to your computer and use it in GitHub Desktop.
class Outlay < ActiveRecord::Base
def self.budget(scopes = {}, pivot_value = nil)
pivot_value ||= "top_10_competitors"
projection_value = {}.tap do |projection|
Outlay.pivot_values(pivot_value, scopes).each do |pivot|
projection[pivot] = {}
pivot_scopes = ({(pivot_value=="top_10_competitors" ? "contractor" : pivot_value) => pivot}.merge(scopes))
projection[pivot] = Outlay.where(pivot_scopes).sum(:outlay_amount)
end
end
end
def self.pivot_values(key, scopes)
v = case(key)
when "top_10_competitors"
scopes["contractor"] ? (scopes["contractor"] & self.contractor) : self.contractor
when "all_competitors"
raise "Not yet implemented"
when "custom_competitors"
raise "Not yet implemented"
else
raise "Unknown value to pivot on (#{key.inspect})"
end
scopes.delete(key)
v
end
def self.contractor
@@contractors ||= distinct(:contractor)
end
private
def self.distinct(column_name)
connection.select_values("SELECT DISTINCT(#{column_name}) FROM outlays")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment