Created
December 11, 2008 12:29
-
-
Save Baael/34682 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Post < ActiveRecord::Base | |
class << self | |
alias_method :find_standard, :find | |
end | |
validates_presence_of :publish_from, :publish_to | |
before_create :validates_time | |
def validates_time | |
[:publish_from, :publish_to].each do |attr| | |
errors.add(attr, 'should be ActiveSupport::TimeWithZone') unless self.send(attr).is_a?(ActiveSupport::TimeWithZone) | |
end | |
raise 'blaad' unless errors.empty? | |
end | |
def self.find(*args) | |
now=Time.now | |
with_scope(:find=>{ :conditions=> ["publish_from < ? and publish_to > ?", now, now] }) do | |
self.find_standard(*args) | |
end | |
end | |
def self.between(hash, *args) | |
with_scope(:find=>{ :conditions=> ["publish_from < ? and publish_to < ? and publish_to > ?", hash[:from].utc, hash[:to].utc, hash[:from].utc] }) do | |
self.find_standard(*args) | |
end | |
end | |
def self.in_date(time, *args) | |
with_scope(:find=>{ :conditions=> ["publish_from < ? and publish_to < ?", time.utc, time.utc] }) do | |
self.find_standard(*args) | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') | |
describe Post do | |
fixtures :posts | |
before(:each) do | |
@valid_attributes = { | |
:title => 'test post 4', | |
:publish_from => Time.now + 2.hours, | |
:publish_to => Time.now + 3.hours, | |
} | |
@invalid_attributes_1 = { | |
:title => 'test post 4', | |
:publish_from => Time.now + 3.hours | |
} | |
@invalid_attributes_2 = { | |
:title => 'test post 4', | |
:publish_from => 2, | |
:publish_from => '11' | |
} | |
end | |
it "should create a new instance given valid attributes" do | |
Post.create!(@valid_attributes).should be_kind_of(Post) | |
end | |
it "should return validation of presence error " do | |
lambda { Post.create!(@invalid_attributes_1) }.should raise_error | |
end | |
it "should return data type mismatch error" do | |
lambda { Post.create!(@invalid_attributes_2) }.should raise_error | |
end | |
describe "functional tests of" do | |
describe "searching with :find method" do | |
it "should return post with id 1" do | |
current_time = Time.parse('2008-12-10 01:28:00 CET').utc | |
Time.stub!(:now).and_return(current_time) | |
posts=Post.find(:all) | |
posts.size.should == 1 | |
posts.first.id.should == 1 | |
end | |
it "should return post with id 2" do | |
current_time = Time.parse('2008-12-10 02:28:00 CET').utc | |
Time.stub!(:now).and_return(current_time) | |
posts=Post.find(:all) | |
posts.size.should == 1 | |
posts.first.id.should == 2 | |
end | |
it "should return no posts" do | |
current_time = Time.parse('2008-12-10 03:28:00 CET').utc | |
Time.stub!(:now).and_return(current_time) | |
posts=Post.find(:all) | |
posts.size.should == 0 | |
end | |
end | |
describe "searching with :between method" do | |
it "should return posts with id 1 and 2" do | |
end | |
end | |
end | |
describe "behavioral tests of" do | |
describe "standard lists posts in category" do | |
it "should find all published posts in current category" do | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment