Created
August 21, 2009 20:54
-
-
Save amiel/172423 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= Group.timespan_filter(@range).count :conditions => :etc %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ Phone, Plan, Group, Message ].each do |klass| | |
klass.class_eval{ include TimespanFilter } | |
end | |
page = (params[:page] || 1).to_i - 1 | |
@range = case params[:scope] | |
when 'day' | |
e = page.days.ago.at_beginning_of_day | |
s = e - 1.day | |
s..e | |
when 'week' | |
e = page.weeks.ago.at_beginning_of_week | |
s = e - 1.week | |
s..e | |
when 'month' | |
e = page.months.ago.at_beginning_of_month | |
s = e - 1.month | |
s..e | |
else # 'all' | |
nil | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module TimespanFilter | |
def self.included(base) | |
base.named_scope :timespan_filter, lambda { |range| | |
return {} if range.nil? | |
{ :conditions => { :created_at => range } } | |
} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment