forked from ryana/sortable_table
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notes.rb
47 lines (32 loc) · 1.3 KB
/
notes.rb
1
2
3
4
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
36
37
38
39
40
41
42
43
44
45
46
# http://github.com/yfactorial/utility_scopes
Rails::Initializer.run do |config|
# ...
config.gem "yfactorial-utility_scopes", :lib => 'utility_scopes',
:source => 'http://gems.github.com/'
end
# rake gems:install GEM=yfactorial-utility_scopes
class Article < ActiveRecord::Base
# Order the results by the given argument, or 'created_at DESC'
# if no arg is given
named_scope :ordered, lambda { |*order|
{ :order => order.flatten.first || 'created_at DESC' }
}
end
# Get all articles ordered by 'created_at DESC'
Article.ordered #=> [<Article id: ...>, <..>]
# Get all articles ordered by 'updated_at DESC'
Article.ordered('updated_at DESC') #=> [<Article id: ...>, <..>]
class Article < ActiveRecord::Base
# This class's default ordering (if not specified
# defaults to 'created_at DESC'
ordered_by 'published_at DESC'
# By default, return 15 results (if not specified
# defaults to 10
default_limit 15
end
# Get the first 15 articles ordered by 'published_at DESC'
Article.ordered.limited #=> [<Article id: ...>, <..>]
# Get the first 15 articles ordered by 'popularity ASC'
Article.ordered('popularity ASC').limited #=> [<Article id: ...>, <..>]
# Get the first 20 articles ordered by 'popularity ASC'
Article.ordered('popularity ASC').limited(20) #=> [<Article id: ...>, <..>]