0
@@ -55,21 +55,36 @@ module Shooter #:nodoc:
0
def self.included(base)
0
base.extend ClassMethods
0
+ if base.respond_to?(:named_scope)
0
+ base.named_scope :by_date, lambda {|by_date| { :conditions => initial_conditions(by_date), :order => order }}
0
+ base.named_scope :recent, lambda {|length_of_time| length_of_time ||= 1.year; recent_options(length_of_time) }
0
+ base.named_scope :between, lambda {|start_date, end_date| { :conditions => between_conditions(start_date, end_date), :order => order }}
0
+ base.extend WithScopeMethods
0
+ module WithScopeMethods
0
def by_date(by_date, options ={})
0
with_scope(:find => {:conditions => initial_conditions(by_date), :order => order}) do
0
block_given? ? yield(options) : find(:all, options)
0
- def count_by_date(by_date, options = {})
0
- with_scope(:find => {:conditions => initial_conditions(by_date)}) do
0
+ def recent(length_of_time = 1.year, options = {})
0
+ with_scope(:find => recent_options(length_of_time)) do
0
+ block_given? ? yield(options) : find(:all, options)
0
+ def between(start_date, end_date, options = {})
0
+ with_scope(:find => {:conditions => between_conditions(start_date, end_date), :order => order}) do
0
+ block_given? ? yield(options) : find(:all, options)
0
def oldest(options = {})
0
find(:first, options.merge(:order => "#{table_name}.#{archivable_attribute} ASC"))
0
@@ -77,25 +92,19 @@ module Shooter #:nodoc:
0
def newest(options = {})
0
find(:first, options.merge(:order => "#{table_name}.#{archivable_attribute} DESC"))
0
- def recent(length_of_time = 1.year, options = {})
0
- with_scope(:find => {:conditions => ["#{table_name}.#{archivable_attribute} >= ?", Time.now.advance(:days => -length_of_time.to_days)], :order => "#{table_name}.#{archivable_attribute} DESC"}) do
0
- block_given? ? yield(options) : find(:all, options)
0
+ def count_by_date(by_date, options = {})
0
+ with_scope(:find => {:conditions => initial_conditions(by_date)}) do
0
def count_recent(length_of_time = 1.year, options = {})
0
- with_scope(:find =>
{:conditions => ["#{table_name}.#{archivable_attribute} >= ?", Time.now.advance(:days => -length_of_time.to_days)], :order => "#{table_name}.#{archivable_attribute} DESC"}) do
0
+ with_scope(:find =>
recent_options(length_of_time)) do
0
- def between(start_date, end_date, options = {})
0
- with_scope(:find => {:conditions => between_conditions(start_date, end_date), :order => order}) do
0
- block_given? ? yield(options) : find(:all, options)
0
def count_between(start_date, end_date, options = {})
0
with_scope(:find => {:conditions => between_conditions(start_date, end_date)}) do
0
@@ -118,7 +127,11 @@ module Shooter #:nodoc:
0
start_date, end_date = simple_parse(start_date), simple_parse(end_date)
0
["#{table_name}.#{archivable_attribute} BETWEEN ? AND ?", start_date, end_date]
0
+ def recent_options(length_of_time)
0
+ {:conditions => ["#{table_name}.#{archivable_attribute} >= ?", Time.now.advance(:days => -length_of_time.to_days)], :order => "#{table_name}.#{archivable_attribute} DESC"}
0
"#{table_name}.#{archivable_attribute} #{sort_order}"
0
@@ -140,8 +153,8 @@ module Shooter #:nodoc:
Comments
No one has commented yet.