public
Description: Sort HTML tables in your Rails app.
Homepage: http://thoughtbot.com
Clone URL: git://github.com/thoughtbot/sortable_table.git
sortable_table / notes.rb
100644 48 lines (32 sloc) 1.333 kb
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
47
48
# 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: ...>, <..>]