Sha256: 8ac4b11d29df99bdf1b5ef807c3d7cfd6a5e2fefa54c0c42730527b2eb1d93fd

Contents?: true

Size: 986 Bytes

Versions: 6

Compression:

Stored size: 986 Bytes

Contents

module PagedScopes
  module Context
    extend ActiveSupport::Memoizable
    
    def self.included(base)
      base.extend ClassMethods
      class << base
        alias_method_chain :find, :context
      end
    end
    
    module ClassMethods
      def find_with_context(*args)
        found_scope, found_options = (scope(:find) || {}).dup, args.dup.extract_options!.dup
        returning find_without_context(*args) do |results|
          [ results ].flatten.compact.each do |result|
            result.instance_variable_set "@found_scope", found_scope.dup
            result.instance_variable_set "@found_options", found_options.dup
          end
        end
      end
    end
    
    def previous
      self.class.scoped(@found_scope).scoped(@found_options).before(self)
    end
    
    def next
      self.class.scoped(@found_scope).scoped(@found_options).after(self)
    end
    
    memoize :previous, :next
  end
end

ActiveRecord::Base.send :include, PagedScopes::Context

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
mholling-paged_scopes-0.0.3 lib/paged_scopes/context.rb
mholling-paged_scopes-0.0.4 lib/paged_scopes/context.rb
mholling-paged_scopes-0.0.5 lib/paged_scopes/context.rb
mholling-paged_scopes-0.1.0 lib/paged_scopes/context.rb
paged_scopes-0.1.1 lib/paged_scopes/context.rb
paged_scopes-0.1.0 lib/paged_scopes/context.rb