Module: ActsAsHistorical::ClassMethods
- Defined in:
- lib/acts_as_historical.rb
Instance Method Summary collapse
-
#acts_as_historical(opts = {}) ⇒ Object
acts_as_historical.
Instance Method Details
#acts_as_historical(opts = {}) ⇒ Object
acts_as_historical
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/acts_as_historical.rb', line 16 def acts_as_historical(opts = {}) configuration = { :date_column => "snapshot_date", :scope => nil } configuration.update(opts) if opts.is_a?(Hash) send :include, InstanceMethods send :extend, DynamicClassMethods self.cattr_accessor :historical_date_col, :historical_scope, :only_weekdays self.historical_date_col = configuration[:date_column] self.historical_scope = configuration[:scope] order_desc = "#{self.historical_date_col_sql} DESC" order_asc = "#{self.historical_date_col_sql} ASC" # named_scopes - sortings named_scope :asc, :order => order_asc named_scope :desc, :order => order_desc named_scope :oldest, :limit => 1, :order => order_asc named_scope :newest, :limit => 1, :order => order_desc named_scope :newest_two, :limit => 2, :order => order_desc # one snapshot per week (every wednesday) named_scope :weekly, :conditions => "DAYOFWEEK(#{self.historical_date_col_sql}) = 2" %w[sundays mondays tuesdays wednesdays thursdays fridays saturdays].each_with_index do |name, day_of_week| named_scope name, :conditions => "DAYOFWEEK(#{self.historical_date_col_sql}) = #{day_of_week+1}" end named_scope :within_month, lambda {{ :conditions => ["#{self.historical_date_col_sql} > ?", Date.today - 30] }} named_scope :within_year, lambda {{ :conditions => ["#{self.historical_date_col_sql} > ?", Date.today - 364] }} named_scope :same_scope, lambda {|record| if self.historical_scope.nil? {} else {:conditions => {self.historical_scope => record[self.historical_scope]} } end } named_scope :on_date, lambda {|date| { :conditions => { :snapshot_date => date.to_date }, :limit => 1 }} # between(older_date, newer_date) # named_scope :between, lambda {|*args| from, to = args range = (from.to_date..to.to_date) { :conditions => {self.historical_date_col => range } } } # nearest(date, 1) # nearest(date, (date_from..date_to)) # named_scope :nearest, lambda {|*args| date, tolerance = args range = self.tolerance_to_range(date, tolerance) { :conditions => {self.historical_date_col => range}, :order => ["ABS(DATEDIFF(#{self.historical_date_col_sql}, '#{date.to_date.to_s(:db)}')) ASC"] } } # Does not include date # named_scope :upto, lambda {|date| raise "passed parameter does not respond_to? to_date" unless date.respond_to?(:to_date) { :conditions => ["#{self.historical_date_col_sql} < ?", date.to_date] } } # Includes date # named_scope :upto_including, lambda {|date| raise "passed parameter does not respond_to? to_date" unless date.respond_to?(:to_date) { :conditions => ["#{self.historical_date_col_sql} <= ?", date.to_date] } } named_scope :from, lambda {|date| raise "passed parameter does not respond_to? to_date" unless date.respond_to?(:to_date) { :conditions => ["#{self.historical_date_col_sql} > ?", date.to_date] } } named_scope :from_including, lambda {|date| raise "passed parameter does not respond_to? to_date" unless date.respond_to?(:to_date) { :conditions => ["#{self.historical_date_col_sql} >= ?", date.to_date] } } named_scope :opt, lambda {|attributes_for_select| {:select => [:snapshot_date, attributes_for_select].flatten.uniq.join(', ') } } # validations validate :valid_date?, :on => :save nil end |