Skip to content

Commit

Permalink
Merged Scope Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
robwilliams committed Oct 9, 2011
2 parents ce1e15d + f48e71a commit 7fbf0c0
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 29 deletions.
1 change: 1 addition & 0 deletions Guardfile
@@ -1,6 +1,7 @@
guard 'rspec', :version => 2 do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^spec/factories/.+\.rb$}) { "spec/" }
watch(%r{^spec/support/.+\.rb$}) { "spec/" }
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec/" }
watch('spec/test_schema.rb') { "spec/" }
Expand Down
7 changes: 6 additions & 1 deletion Rakefile
Expand Up @@ -4,4 +4,9 @@ require 'rspec/core/rake_task'
task :default => :spec

desc "Run specs"
RSpec::Core::RakeTask.new
RSpec::Core::RakeTask.new

desc "Open an IRB session preloaded with ActivePress"
task :console do
sh "irb -rubygems -I lib -r active_press.rb"
end
22 changes: 11 additions & 11 deletions lib/active_press/models/post.rb
Expand Up @@ -2,17 +2,17 @@ class ActivePress::Post < ActivePress::Base
set_table_name "wp_posts"
set_primary_key "ID"

has_many :comments, :foreign_key => "comment_post_ID"
has_many :postmetas, :foreign_key => "post_id"
has_many :comments, :foreign_key => "comment_post_ID"
has_many :postmetas, :foreign_key => "post_id"
has_many :term_relationships, :foreign_key => "object_id"
has_many :term_taxonomies, :through => :term_relationships
has_many :categories, :through => :term_relationships
has_many :tags, :through => :term_relationships
has_many :term_taxonomies, :through => :term_relationships
has_many :categories, :through => :term_relationships
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)) }
belongs_to :user, :foreign_key => "post_author"

scope :by_post_status, lambda {|status| where(:post_status => status) }
scope :by_post_type, lambda {|type| where(:post_type => type) }
scope :before_now, lambda { where("post_date_gmt < ?", Time.now.to_s(:db)) }
scope :published, by_post_status('publish').before_now
end
3 changes: 2 additions & 1 deletion lib/active_press/taxonomy.rb
Expand Up @@ -8,7 +8,8 @@ def self.included(base)

belongs_to :term, :foreign_key => "term_id"
has_many :term_relationships, :foreign_key => "term_taxonomy_id"

has_many :posts, :through => :term_relationships

delegate :name, :to => :term
delegate :slug, :to => :term
end
Expand Down
14 changes: 14 additions & 0 deletions spec/factories/post_factory.rb
@@ -0,0 +1,14 @@
FactoryGirl.define do
factory :post, :class => ActivePress::Post do
post_title 'Title of the post'
post_content 'Content of the post'
post_content_filtered 'Content of the post filtered'
post_excerpt 'Excerpt of the post'
to_ping false
pinged false
post_date Time.now
post_date_gmt Time.now
post_modified Time.now
post_modified_gmt Time.now
end
end
3 changes: 1 addition & 2 deletions spec/models/category_spec.rb
@@ -1,6 +1,5 @@
require 'spec_helper'

describe ActivePress::Category do
it { should belong_to(:term) }
it { should have_many(:term_relationships) }
it_should_behave_like "a Taxonomy"
end
9 changes: 8 additions & 1 deletion spec/models/post_spec.rb
Expand Up @@ -28,6 +28,13 @@
end
end

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

describe :published do
subject { ActivePress::Post.published }

Expand All @@ -36,7 +43,7 @@
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)}'")
subject.to_sql.should include("post_date_gmt < '#{Time.now.to_s(:db)}'")
end
end
end
Expand Down
3 changes: 1 addition & 2 deletions spec/models/tag_spec.rb
@@ -1,6 +1,5 @@
require 'spec_helper'

describe ActivePress::Tag do
it { should belong_to(:term) }
it { should have_many(:term_relationships) }
it_should_behave_like "a Taxonomy"
end
25 changes: 14 additions & 11 deletions spec/models/term_taxonomy_spec.rb
Expand Up @@ -2,19 +2,22 @@

describe ActivePress::TermTaxonomy do

before(:all) {@term_taxonomy = FactoryGirl.build(:term_taxonomy, :term => FactoryGirl.build(:term))}
subject {@term_taxonomy}

it { should belong_to(:term) }
it { should have_many(:term_relationships) }

describe :name do
subject {@term_taxonomy.name}
specify { should eql(@term_taxonomy.term.name) }
end

describe :slug do
subject {@term_taxonomy.slug}
specify { should eql(@term_taxonomy.term.slug) }
context "with a term" do

before(:all) {@term_taxonomy = FactoryGirl.build(:term_taxonomy, :term => FactoryGirl.build(:term))}
subject {@term_taxonomy}

describe :name do
subject {@term_taxonomy.name}
specify { should eql(@term_taxonomy.term.name) }
end

describe :slug do
subject {@term_taxonomy.slug}
specify { should eql(@term_taxonomy.term.slug) }
end
end
end
3 changes: 3 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -3,6 +3,9 @@
require 'shoulda/matchers'
require 'factory_girl'


Dir.glob(File.dirname(__FILE__) + '/support/*') {|file| require file}

#ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ":memory:")
ActiveRecord::Migration.verbose = false
Expand Down
5 changes: 5 additions & 0 deletions spec/support/taxonomy_shared_example.rb
@@ -0,0 +1,5 @@
shared_examples_for "a Taxonomy" do
it { should belong_to(:term) }
it { should have_many(:term_relationships) }
it { should have_many(:posts).through(:term_relationships) }
end

0 comments on commit 7fbf0c0

Please sign in to comment.