Skip to content

Instantly share code, notes, and snippets.

@pedrodelgallego
Created December 2, 2009 00:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pedrodelgallego/246791 to your computer and use it in GitHub Desktop.
Save pedrodelgallego/246791 to your computer and use it in GitHub Desktop.
class Post < ActiveRecord::Base
# [...]
named_scope :order_by , lambda { |order| { :order => (order) } }
named_scope :limit , lambda { |*lim| { :limit => (lim.first || DEFAULT_LIMIT) } }
named_scope :published_before, lambda { |*time|
{:conditions => [ "published_at < ?", (time.first || Time.now) ]}}
named_scope :with_comments, {:include=>:comments}
named_scope :remove_duplications, :select => 'distinct posts.*'
def self.find_recent(options = {})
tag = options.delete(:tag)
if tag
published_before.limit.order_by("published_at DESC").find_tagged_with(tag, options)
else
published_before.limit.order_by("published_at DESC").find(:all, options)
end
end
# more code ...
end
class Post < ActiveRecord::Base
# [...]
def self.find_recent(options = {})
tag = options.delete(:tag)
options = {
:order => 'posts.published_at DESC',
:conditions => ['published_at < ?', Time.zone.now],
:limit => DEFAULT_LIMIT
}.merge(options)
if tag
find_tagged_with(tag, options)
else
find(:all, options)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment