Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
Add Comment
Please, Sign In to add comment