public
Description: Plugin for querying by dates on ActiveRecord models
Clone URL: git://github.com/joshuaclayton/acts_as_archivable.git
Search Repo:
Updated to work within Rails 2.1, allowing for named_scopes; also fixed 
to_days method, making it an instance method on Numeric instead of 
Integer.
joshuaclayton (author)
Thu Jul 24 06:12:28 -0700 2008
commit  dd4ee1b869606b7dafff3e54c70826610aa5ae5a
tree    a06bed5f5c4258697972934d4e1fdb9de3963f41
parent  6a43c2fe56ae324d2d654048ea62e490c6b61691
0
...
5
6
7
8
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
...
5
6
7
 
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
0
@@ -5,4 +5,29 @@ ActsAsArchivable is a collection of methods that I found myself using quite a bi
0
 to models with time-stamped columns. Initial things were blog entries in regards to archives,
0
 but I found that I was using these more and more in various ways throughout many models.
0
 
0
-This library requires ActiveRecord.
0
\ No newline at end of file
0
+This library requires ActiveRecord.
0
+
0
+ class Entry < ActiveRecord::Base
0
+ acts_as_archivable :order => 'DESC'
0
+ has_many :comments, :dependent => :destroy
0
+ end
0
+
0
+ class Comment < ActiveRecord::Base
0
+ acts_as_archivable :on => :replied_on
0
+ belongs_to :entry
0
+ end
0
+
0
+From here, you have quick access to records related to date.
0
+
0
+ Entry.by_date :year => 2007
0
+ Entry.by_date Date.today
0
+ Entry.by_date '5/1/2007'
0
+
0
+ Entry.oldest
0
+ Entry.newest
0
+
0
+ Entry.recent 2.weeks
0
+ Entry.recent 3.months
0
+ Entry.recent (3.months - 1.week)
0
+
0
+ Entry.between '5/10/2007', Date.today
0
\ No newline at end of file
...
55
56
57
 
 
 
 
 
 
 
58
59
60
 
 
61
62
63
64
65
66
67
68
69
 
 
 
70
71
72
 
 
 
 
 
 
 
 
73
74
75
...
77
78
79
80
81
82
83
 
 
 
 
84
85
86
87
88
 
89
90
91
92
93
94
95
96
97
98
99
100
101
...
118
119
120
121
 
 
 
 
 
122
123
124
...
140
141
142
143
 
144
145
 
146
147
...
55
56
57
58
59
60
61
62
63
64
65
 
 
66
67
68
69
70
71
72
73
 
 
 
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
...
92
93
94
 
 
 
 
95
96
97
98
99
100
101
102
 
103
104
105
106
107
 
 
 
 
 
 
108
109
110
...
127
128
129
 
130
131
132
133
134
135
136
137
...
153
154
155
 
156
157
 
158
159
160
0
@@ -55,21 +55,36 @@ module Shooter #:nodoc:
0
       module InstanceMethods
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
+ else
0
+ base.extend WithScopeMethods
0
+ end
0
         end
0
-
0
- module ClassMethods
0
+
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
             end
0
           end
0
 
0
- def count_by_date(by_date, options = {})
0
- with_scope(:find => {:conditions => initial_conditions(by_date)}) do
0
- count(options)
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
             end
0
           end
0
 
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
+ end
0
+ end
0
+ end
0
+
0
+ module ClassMethods
0
           def oldest(options = {})
0
             find(:first, options.merge(:order => "#{table_name}.#{archivable_attribute} ASC"))
0
           end
0
@@ -77,25 +92,19 @@ module Shooter #:nodoc:
0
           def newest(options = {})
0
             find(:first, options.merge(:order => "#{table_name}.#{archivable_attribute} DESC"))
0
           end
0
-
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
+
0
+ def count_by_date(by_date, options = {})
0
+ with_scope(:find => {:conditions => initial_conditions(by_date)}) do
0
+ count(options)
0
             end
0
           end
0
 
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
               count(options)
0
             end
0
           end
0
 
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
- end
0
- end
0
-
0
           def count_between(start_date, end_date, options = {})
0
             with_scope(:find => {:conditions => between_conditions(start_date, end_date)}) do
0
               count(options)
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
           end
0
-
0
+
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
+ end
0
+
0
           def order
0
             "#{table_name}.#{archivable_attribute} #{sort_order}"
0
           end
0
@@ -140,8 +153,8 @@ module Shooter #:nodoc:
0
   end
0
 end
0
 
0
-class Integer
0
+class Numeric
0
   def to_days
0
- self/60/60/24
0
+ self.to_i/60/60/24
0
   end
0
 end
...
4
5
6
7
8
9
10
...
4
5
6
 
7
8
9
0
@@ -4,7 +4,6 @@ require 'date'
0
 require 'test/unit'
0
 require File.expand_path(File.join(File.dirname(__FILE__), '../../../../config/environment.rb'))
0
 require 'rubygems'
0
-require 'active_support/breakpoint'
0
 require 'active_record/fixtures'
0
 
0
 config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))

Comments

    No one has commented yet.