Sha256: 3e2b817894e0801379c1de8618e83b99cbf8dcafbf6d54d9eccc7a2b69ffc7c6

Contents?: true

Size: 1.13 KB

Versions: 17

Compression:

Stored size: 1.13 KB

Contents

# encoding: utf-8
module Mongoid #:nodoc:
  module NamedScope
    # Creates a named_scope for the +Document+, similar to ActiveRecord's
    # named_scopes. +NamedScopes+ are proxied +Criteria+ objects that can be
    # chained.
    #
    # Example:
    #
    #   class Person
    #     include Mongoid::Document
    #     field :active, :type => Boolean
    #     field :count, :type => Integer
    #
    #     named_scope :active, :where => { :active => true }
    #     named_scope :count_gt_one, :where => { :count.gt => 1 }
    #     named_scope :at_least_count, lambda { |count| { :where => { :count.gt => count } } }
    #   end
    def named_scope(name, options = {}, &block)
      name = name.to_sym
      scopes[name] = lambda do |parent, *args|
        Scope.new(parent, options.scoped(*args), &block)
      end
      (class << self; self; end).class_eval <<-EOT
        def #{name}(*args)
          scopes[:#{name}].call(self, *args)
        end
      EOT
    end

    # Return the scopes or default to an empty +Hash+.
    def scopes
      read_inheritable_attribute(:scopes) || write_inheritable_attribute(:scopes, {})
    end

  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
mongoid-1.2.6 lib/mongoid/named_scope.rb
mongoid-1.2.5 lib/mongoid/named_scope.rb
mongoid-1.2.4 lib/mongoid/named_scope.rb
mongoid-1.2.3 lib/mongoid/named_scope.rb
mongoid-1.2.2 lib/mongoid/named_scope.rb
mongoid-1.2.1 lib/mongoid/named_scope.rb
mongoid-1.2.0 lib/mongoid/named_scope.rb
mongoid-1.1.4 lib/mongoid/named_scope.rb
mongoid-1.1.3 lib/mongoid/named_scope.rb
mongoid-1.1.2 lib/mongoid/named_scope.rb
mongoid-1.1.1 lib/mongoid/named_scope.rb
mongoid-1.1.0 lib/mongoid/named_scope.rb
mongoid-1.0.6 lib/mongoid/named_scope.rb
mongoid-1.0.5 lib/mongoid/named_scope.rb
mongoid-1.0.4 lib/mongoid/named_scope.rb
mongoid-1.0.3 lib/mongoid/named_scope.rb
mongoid-1.0.2 lib/mongoid/named_scope.rb