2013-07-02 08:46:44 +00:00
|
|
|
module OrbitModel
|
|
|
|
|
|
|
|
module TimeFrame
|
|
|
|
|
|
|
|
def self.included(base)
|
2013-10-16 18:18:32 +00:00
|
|
|
base.extend ClassMethods
|
2013-07-02 08:46:44 +00:00
|
|
|
base.class_eval do
|
|
|
|
field :postdate , :type => DateTime
|
|
|
|
field :deadline , :type => DateTime
|
|
|
|
before_save :check_deadline
|
2013-10-16 18:18:32 +00:00
|
|
|
query = {type: 'any_of', conditions: "{deadline: nil, :postdate.lte => Time.now} , {:deadline.gte => Time.now, :postdate.lte => Time.now}"}
|
|
|
|
if class_variable_defined?(:@@can_display)
|
|
|
|
class_variable_set(:@@can_display, (class_variable_get(:@@can_display) << query))
|
|
|
|
else
|
|
|
|
class_variable_set(:@@can_display, [query])
|
|
|
|
end
|
2013-07-02 08:46:44 +00:00
|
|
|
send :include, InstanceMethods
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module InstanceMethods
|
|
|
|
|
|
|
|
def is_expired?
|
|
|
|
Date.today > self.deadline ? true : false rescue false
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def check_deadline
|
|
|
|
if(!self.deadline.nil? and (self.deadline < self.postdate ))
|
|
|
|
self.deadline = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-10-16 18:18:32 +00:00
|
|
|
module ClassMethods
|
|
|
|
|
|
|
|
define_method(:can_display) do
|
|
|
|
final_query = class_variable_get(:@@can_display).inject('') do |result, query_hash|
|
|
|
|
result << "." if result.present?
|
|
|
|
result << "#{query_hash[:type]}(#{query_hash[:conditions]})"
|
|
|
|
result
|
|
|
|
end
|
|
|
|
eval(final_query)
|
|
|
|
end unless method_defined? :can_display
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-07-02 08:46:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|