Guest User

Untitled

a guest
Feb 21st, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. class Post < ActiveRecord::Base
  2.  
  3.  
  4.  
  5. class << self
  6. alias_method :find_standard, :find
  7. end
  8. validates_presence_of :publish_from, :publish_to
  9. before_create :validates_time
  10.  
  11. def validates_time
  12. [:publish_from, :publish_to].each do |attr|
  13. errors.add(attr, 'should be ActiveSupport::TimeWithZone') unless self.send(attr).is_a?(ActiveSupport::TimeWithZone)
  14. end
  15. raise 'blaad' unless errors.empty?
  16.  
  17. end
  18.  
  19. def self.find(*args)
  20. now=Time.now
  21. with_scope(:find=>{ :conditions=> ["publish_from < ? and publish_to > ?", now, now] }) do
  22. self.find_standard(*args)
  23. end
  24. end
  25.  
  26. def self.between(hash, *args)
  27. with_scope(:find=>{ :conditions=> ["publish_from < ? and publish_to < ? and publish_to > ?", hash[:from].utc, hash[:to].utc, hash[:from].utc] }) do
  28. self.find_standard(*args)
  29. end
  30. end
  31.  
  32. def self.in_date(time, *args)
  33. with_scope(:find=>{ :conditions=> ["publish_from < ? and publish_to < ?", time.utc, time.utc] }) do
  34. self.find_standard(*args)
  35. end
  36. end
  37.  
  38. end
Add Comment
Please, Sign In to add comment