Permalink
Browse files

Final supporting code tweaks

  • Loading branch information...
rwdaigle committed Feb 23, 2010
1 parent 0288789 commit 9f06484ad7f81bb3d18d3f0d615f029382997d71
@@ -5,22 +5,29 @@ class Post < ActiveRecord::Base
validates_presence_of :author
- scope :published_since, lambda { |ago|
- published.where(["posts.published_at >= ?", ago])
- }
scope :published, lambda {
- where(["posts.published_at IS NOT NULL AND posts.published_at <= ?", Time.zone.now])
+ where("posts.published_at IS NOT NULL AND posts.published_at <= ?", Time.zone.now)
+ }
+ scope :published_since, lambda { |ago|
+ published.where("posts.published_at >= ?", ago)
}
- scope :recent, order("posts.published_at DESC")
+ scope :recent, published.order("posts.published_at DESC")
scope :popular, order("posts.views_count DESC")
- scope :unpublished, where("posts.published_at IS NULL")
+ scope :unpublished, lambda {
+ where("posts.published_at IS NULL OR posts.published_at > ?", Time.zone.now)
+ }
+
+ # Ludacris
+ scope :titled_luda, where(:title => 'Luda')
class << self
+ # Search the title and body fields for the given string.
+ # Start with an empty scope and build on it for each attr
def search(q)
[:title, :body].inject(scoped) do |combined_scope, attr|
- combined_scope.where(["posts.#{attr} LIKE ?", "%#{sanitize_sql(q)}%"])
+ combined_scope.where("posts.#{attr} LIKE ?", "%#{sanitize_sql(q)}%")
end
end
end
@@ -5,7 +5,6 @@ class User < ActiveRecord::Base
validates_presence_of :username
- # TODO: How to reuse published logic on far side (Post)
scope :published, lambda {
joins("join posts on posts.author_id = users.id").
where(Post.published.where_values).

0 comments on commit 9f06484

Please sign in to comment.