Skip to content

Commit

Permalink
Added scopes to posts
Browse files Browse the repository at this point in the history
  • Loading branch information
robwilliams committed Oct 9, 2011
1 parent 4b0c31e commit ce1e15d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
5 changes: 5 additions & 0 deletions lib/active_press/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ class ActivePress::Post < ActivePress::Base
has_many :tags, :through => :term_relationships

belongs_to :user, :foreign_key => "post_author"

scope :by_post_status, lambda {|post_status| where(:post_status => post_status) }
scope :by_post_type, lambda {|post_type| where(:post_type => post_type) }
scope :published, lambda { by_post_status('publish').before_now }
scope :before_now, lambda { where('post_date < ?', Time.now.to_s(:db)) }
end
28 changes: 21 additions & 7 deletions spec/models/post_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'spec_helper'

describe ActivePress::Post do

it { should have_many(:comments) }
it { should belong_to(:user) }
it { should have_many(:postmetas) }
Expand All @@ -10,20 +11,33 @@
it { should have_many(:tags).through(:term_relationships) }

context :scope do

describe :by_post_status do
subject { by_post_status('draft') }
it "should only return posts that have the same post_status"
subject { ActivePress::Post.by_post_status('draft') }

it "should only return posts that have the same post_status" do
subject.to_sql.should include("\"post_status\" = 'draft'")
end
end

describe :by_post_type do
subject { by_post_type('post') }
it "should only return posts that have the same post_type"
subject { ActivePress::Post.by_post_type('post') }

it "should only return posts that have the same post_type" do
subject.to_sql.should include("\"post_type\" = 'post'")
end
end

describe :published do
subject { published }
it "should only return posts that have a post_status of 'publish'"
it "should only return posts that have a post_date before now"
subject { ActivePress::Post.published }

it "should only return posts that have a post_status of 'publish'" do
subject.to_sql.should include("\"post_status\" = 'publish'")
end

it "should only return posts that have a post_date before now" do
subject.to_sql.should include("post_date < '#{Time.now.to_s(:db)}'")
end
end
end
end

0 comments on commit ce1e15d

Please sign in to comment.