class TicketReport < ActiveRecord::Base belongs_to :project serialize :filter_options, Hash validates_presence_of :name, :project_id validates_uniqueness_of :name, :scope => :project_id def time_interval=(interval_hash) if !interval_hash.blank? && interval_hash[:count] && interval_hash[:count].to_i > 0 && ['days', 'weeks', 'months'].include?(interval_hash[:units]) seconds = eval("#{interval_hash[:count]}.#{interval_hash[:units]}") if seconds && seconds.to_i > 0 write_attribute(:time_interval, seconds) return true end end write_attribute(:time_interval, nil) end def filter_options filter_options = read_attribute(:filter_options) filter_options.is_a?(Hash) ? filter_options : {} end def since_date return nil if time_interval.blank? || time_interval.to_i < 1 Time.now - time_interval end def user_specific? filter_options.key?(:my_tickets) end protected def validate if filter_options.blank? self.errors.add(:filter_options, 'must be specified') end end end