module TimeScopes::Rails::ActiveRecordExtension::ClassMethods

Public Instance Methods

timescoped!(opts={}) click to toggle source
# File lib/timescopes/rails/active_record_extension.rb, line 7
def timescoped!(opts={})
  default_scopes = [:minutely, :hourly, :daily, :weekly, :monthly, :yearly, :alltime]

  field  = opts[:field] || "created_at"
  scopes = if opts[:only]
            Array(opts[:only]).map(&:to_sym) & default_scopes
           else
             default_scopes
           end

  scopes.each do |scope|
    eval %%
      def #{scope}(start=nil)
        start, finish = TimeScopes.#{scope}(start)
        where('#{field} >= ? AND #{field} < ?', start.utc, finish.utc)
      end
    %
  end
end