Skip to content

Instantly share code, notes, and snippets.

@rubiety
Created December 12, 2008 19:52
Show Gist options
  • Save rubiety/35250 to your computer and use it in GitHub Desktop.
Save rubiety/35250 to your computer and use it in GitHub Desktop.
class ActivityEntry < ActiveRecord::Base
belongs_to :target, :polymorphic => true
belongs_to :user
# Target Scopes
named_scope :for, lambda {|obj| {:conditions => {:target_id => obj.id, :target_type => obj.class.name}} }
named_scope :for_listings, {:conditions => {:target_type => 'Listing'} }
named_scope :manageable_by, lambda {|user|
case
when user.can_manage_company? then {}
when user.office_access_level? && user.can_manage_all_offices? then {}
when user.office_access_level? then {:conditions => {'l.office_id' => user.manageable_offices.map(&:id)}}
else {:conditions => {'l.agent_id' => user.id}}
end.merge(:joins => 'INNER JOIN listings l ON l.id = activity_entries.target_id')
}
# Type Scopes
named_scope :view, :conditions => {:action => 'View'}
named_scope :email, :conditions => {:action => 'E-Mail'}
named_scope :print, :conditions => {:action => 'Print'}
named_scope :favorite, :conditions => {:action => 'Favorite'}
named_scope :request_information, :conditions => {:action => 'Request Information'}
named_scope :request_showing, :conditions => {:action => 'Request Showing'}
named_scope :request_callback, :conditions => {:action => 'Request Callback'}
named_scope :lead, :conditions => {:action => 'Lead'}
# Time Scopes
named_scope :in_range, lambda {|start_date, end_date| {:conditions => {:created_at => start_date.beginning_of_day..end_date.end_of_day}} }
named_scope :this_week, lambda { {:conditions => {:created_at => Time.now.beginning_of_week..Time.now}} }
named_scope :last_week, lambda { {:conditions => {:created_at => 1.week.ago.beginning_of_week..1.week.ago.end_of_week}} }
named_scope :past_week, lambda { {:conditions => {:created_at => 1.week.ago..Time.now}} }
named_scope :this_month, lambda { {:conditions => {:created_at => Time.now.beginning_of_month..Time.now}} }
named_scope :last_month, lambda { {:conditions => {:created_at => 1.month.ago.beginning_of_month..1.month.ago.end_of_month}}}
end
@listing_views_count = ActivityEntry.view.this_month.for_listings.manageable_by(current_user).count
@leads_count = ActivityEntry.lead.this_month.for_listings.manageable_by(current_user).count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment