我写了两个简单的关系助手:
with_preferred_sort_order
返回一个关系all_on_page
返回[pages_count, record_array]
with_preferred_sort_order 奇怪地对记录进行排序:
1.9.3p194 :040 > Article.with_preferred_sort_order.
all_on_page(1).last.map{|a| puts a.title[0..20]};nil
(0.5ms) SELECT COUNT(*) FROM "articles"
Article Load (157.9ms) SELECT "articles".* FROM "articles"
ORDER BY title Desc LIMIT 50 OFFSET 0
“The Bar for Success
method_missing in Jav
jQuery on Rails: A Fr
jQuery in Action (w00
jQuery in Action (w00
jQuery Selector Refca
jQuery Selector Refca
jQuery Selector Refca
jQuery 1.4 and Malfor
jQuery 1.4 and Malfor
What’s Wrong with “HT
What’s Up With All Th
What’s Up With All Th
What’s Up With All Th
What’s Up With All Th
What’s New in Bundler
Vibrant Ink Theme for
Using the New Gem Bun
Using the New Gem Bun
Using SproutCore 2.0
Using SproutCore 2.0
Using SproutCore 2.0
Using >= Considered H
Understanding “Protot
Understanding JavaScr
Tokaido: My Hopes and
Tokaido Status Update
Today’s Dispatch: Wea
Threads (in Ruby): En
Threads (in Ruby): En
Threads (in Ruby): En
The Rails 3 Router: R
The Rails 3 Router: R
The Irony of the iPad
Textmate gem
Textmate Search in a
Stop Watching Sophie’
Spinning up a new Rai
Spinning up a new Rai
Some of the Problems
Simplifying Rails Blo
Search within a folde
Ruby 2.0 Refinements
Ruby 2.0 Refinements
Ruby 2.0 Refinements
RailsConf Talk Recap
RailsConf Slides
RailsConf Slides
RailsConf Slides
RailsConf Europe Slid
=> nil
1.9.3p194 :041 >
这里是源
def self.included recipient
with recipient do
scope :with_preferred_sort_order,
lambda {
#sort_by = recipient.preferences.sort_by;
#asc_desc = recipient.preferences.asc_desc;
#recipient.order("%s %s"%[sort_by, asc_desc])
sort_by = preferences.sort_by;
asc_desc = preferences.asc_desc;
order("%s %s"%[sort_by, asc_desc])
}
end
end
还有一个
module ActiveRecord
class Relation
# Returns an array [ pages_range, items_array]
# pages_range is a range of pages, which starts from 1, e.g. 1..19
# items_array is an array which contains items of page_number page
#
def all_on_page page_number=1, items_per_page=nil
def total_pages items_qty, items_per_page
r = items_qty/items_per_page
r == 0 ? 1 : items_qty % items_per_page !=0 ? r+1 : r
end
items_per_page ||= self.klass.respond_to?(:preferences) ?
self.klass.preferences.items_per_page.to_i : 10
items_qty = self.count
pages_range = 1..total_pages(items_qty, items_per_page)
offset = (page_number - 1)*items_per_page
items_array = self.offset(offset).limit(items_per_page).all
[ pages_range, items_array ]
end
end
end