public
Rubygem
Description: Generated scopes for ActiveRecord classes
Clone URL: git://github.com/thoughtbot/pacecar.git
add Pacecar::Ranking, which provides a scope that ranks records by 
associated record count (User.maximum_comments => Users ordered by most 
Comment records belong to each user)
mjankowski (author)
Tue Oct 07 20:14:57 -0700 2008
commit  e909de91e898edfdc288013592fa296ff5993602
tree    4b81fdb4c20c75335de1dae28a560be875b2bd3f
parent  b60531a48cab9ed1b53c3e4115e3254d26bbd243
...
4
5
6
7
 
8
9
10
...
22
23
24
 
 
 
 
 
 
25
26
27
28
 
 
29
30
 
 
31
32
33
...
39
40
41
42
 
 
 
43
44
45
...
127
128
129
 
 
 
 
 
 
 
130
131
132
...
148
149
150
 
 
 
 
151
152
153
...
4
5
6
 
7
8
9
10
...
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
...
49
50
51
 
52
53
54
55
56
57
...
139
140
141
142
143
144
145
146
147
148
149
150
151
...
167
168
169
170
171
172
173
174
175
176
0
@@ -4,7 +4,7 @@ Pacecar adds named_scope methods to ActiveRecord classes via database column int
0
 
0
 == Usage
0
 
0
-Assuming a database schema and simple model declaration...
0
+Assuming a database schema...
0
 
0
   class CreateSchema < ActiveRecord::Migration
0
     def self.up
0
@@ -22,12 +22,22 @@ Assuming a database schema and simple model declaration...
0
         t.integer :owner_id
0
         t.string :publication_state
0
         t.string :post_type
0
+ t.timestamps
0
+ end
0
+ create_table :comments, :force => true do |t|
0
+ t.integer :user_id
0
+ t.text :description
0
+ t.timestamps
0
       end
0
     end
0
   end
0
 
0
+And some basic model declarations...
0
+
0
   class User < ActiveRecord::Base
0
     has_many :posts, :as => :owner
0
+ has_many :comments
0
+ scopes_ranking :comments
0
   end
0
 
0
   class Post < ActiveRecord::Base
0
@@ -39,7 +49,9 @@ Assuming a database schema and simple model declaration...
0
     scopes_polymorph :owner
0
   end
0
 
0
-...which yields a number of named scope methods.
0
+ class Comment < ActiveRecord::Base
0
+ belongs_to :user
0
+ end
0
 
0
 = All columns
0
 
0
@@ -127,6 +139,13 @@ Records which have an owner_type of User...
0
 
0
   Post.for_owner_type(User)
0
 
0
+= Associations
0
+
0
+Records with the most and least associated records...
0
+
0
+ User.maximum_comments
0
+ User.minimum_comments
0
+
0
 = State columns
0
 
0
 Records which are in a particular state, or not in a state...
0
@@ -148,6 +167,10 @@ To get all users that have a first_name set, who are admins and approved more th
0
 
0
   User.first_name_present.admin.approved_at_before(2.weeks.ago).by_first_name
0
 
0
+To get the top 10 commenters...
0
+
0
+ User.maximim_comments.limited(10)
0
+
0
 = License
0
 
0
 Pacecar is free software, and may be redistributed under the terms specified in the MIT-LICENSE file.
...
1
2
3
4
 
5
6
7
8
 
9
10
11
...
34
35
36
 
37
38
39
...
 
1
2
3
4
5
6
7
8
9
10
11
12
...
35
36
37
38
39
40
41
0
@@ -1,11 +1,12 @@
0
-require 'pacecar/helpers'
0
 require 'pacecar/boolean'
0
 require 'pacecar/datetime'
0
 require 'pacecar/duration'
0
+require 'pacecar/helpers'
0
 require 'pacecar/limit'
0
 require 'pacecar/order'
0
 require 'pacecar/polymorph'
0
 require 'pacecar/presence'
0
+require 'pacecar/ranking'
0
 require 'pacecar/search'
0
 require 'pacecar/state'
0
 
0
@@ -34,6 +35,7 @@ module Pacecar
0
         include Pacecar::Order
0
         include Pacecar::Polymorph
0
         include Pacecar::Presence
0
+ include Pacecar::Ranking
0
         include Pacecar::Search
0
         include Pacecar::State
0
       end
...
16
17
18
 
 
 
 
 
 
19
20
21
...
24
25
26
 
 
27
28
29
...
33
34
35
 
 
 
...
16
17
18
19
20
21
22
23
24
25
26
27
...
30
31
32
33
34
35
36
37
...
41
42
43
44
45
46
0
@@ -16,6 +16,12 @@ class CreateSchema < ActiveRecord::Migration
0
       t.integer :owner_id
0
       t.string :publication_state
0
       t.string :post_type
0
+ t.timestamps
0
+ end
0
+ create_table :comments, :force => true do |t|
0
+ t.integer :user_id
0
+ t.text :description
0
+ t.timestamps
0
     end
0
   end
0
 end
0
@@ -24,6 +30,8 @@ CreateSchema.suppress_messages { CreateSchema.migrate(:up) }
0
 
0
 class User < ActiveRecord::Base
0
   has_many :posts, :as => :owner
0
+ has_many :comments
0
+ scopes_ranking :comments
0
 end
0
 class Post < ActiveRecord::Base
0
   PUBLICATION_STATES = %w(Draft Submitted Rejected Accepted)
0
@@ -33,3 +41,6 @@ class Post < ActiveRecord::Base
0
   scopes_state :post_type, :with => TYPES
0
   scopes_polymorph :owner
0
 end
0
+class Comment < ActiveRecord::Base
0
+ belongs_to :user
0
+end

Comments

    No one has commented yet.