Sha256: d9615986c1353441ff5b9ebbac9bde43f47a669a59b1594e63f54cceae4ae30b

Contents?: true

Size: 855 Bytes

Versions: 3

Compression:

Stored size: 855 Bytes

Contents

module TimeScopes
  module Rails
    module ActiveRecordExtension
      extend ActiveSupport::Concern

      module ClassMethods
        def timescoped!(opts={})
          default_scopes = [:hourly, :daily, :weekly, :monthly]

          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
      end
    end
  end
end

ActiveRecord::Base.send(:include, TimeScopes::Rails::ActiveRecordExtension)

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
timescopes-0.1.4 lib/timescopes/rails/active_record_extension.rb
timescopes-0.1.3 lib/timescopes/rails/active_record_extension.rb
timescopes-0.1.2 lib/timescopes/rails/active_record_extension.rb