Sha256: d7067a01f9a43022ea4bef40b799e467f6602ee5eb211b20a119141aae153d05

Contents?: true

Size: 866 Bytes

Versions: 3

Compression:

Stored size: 866 Bytes

Contents

module TimeScopes
  module Rails
    module ActiveRecordExtension
      extend ActiveSupport::Concern

      module ClassMethods
        def timescoped!(opts={})
          default_scopes = [:minutely, :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.7 lib/timescopes/rails/active_record_extension.rb
timescopes-0.1.6 lib/timescopes/rails/active_record_extension.rb
timescopes-0.1.5 lib/timescopes/rails/active_record_extension.rb