Skip to content

Instantly share code, notes, and snippets.

@Antiarchitect
Created October 15, 2010 04:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Antiarchitect/627637 to your computer and use it in GitHub Desktop.
Save Antiarchitect/627637 to your computer and use it in GitHub Desktop.
class Event < ActiveRecord::Base
extend ActiveSupport::Memoizable
cattr_accessor :year
belongs_to :program
has_many :financing_periods, :dependent => :destroy
def current_period
financing_periods.current(@@year).last(:include => [:plain_financings, { :named_financings => :district }])
end
def planned_federal_financing
current_period ? current_period.planned_federal_financing : 0.0
end
memoize :current_period,
:planned_federal_financing
end
class FinancingPeriod < ActiveRecord::Base
extend ActiveSupport::Memoizable
belongs_to :event
has_many :financings, :dependent => :destroy
named_scope :current, lambda { |year| {:order => :date, :conditions => ['YEAR(date) = ?', year || Time.now.year.to_int] } }
def planned_federal_financing
financings.sum(&:planned_federal_financing)
end
memoize :planned_federal_financing
end
class Program < ActiveRecord::Base
extend ActiveSupport::Memoizable
acts_as_tree
has_many :events
...
def planned_federal_financing
events.sum(&:planned_federal_financing) + children.sum(&:planned_federal_financing)
end
...
memoize :planned_federal_financing
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment