Sha256: ac9fecee0a6e930f25ca9c466d12ad1e6e90d1d1c7631265c5a4fbd8e82a4001

Contents?: true

Size: 1.15 KB

Versions: 16

Compression:

Stored size: 1.15 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, conditions = {}, &block)
      name = name.to_sym
      scopes[name] = Scope.new(conditions, &block)
      (class << self; self; end).class_eval <<-EOT
        def #{name}(*args)
          scope = scopes[:#{name}]
          scope.extend(criteria.fuse(scope.conditions.scoped(*args)))
        end
      EOT
    end
    alias :scope :named_scope

    # 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

16 entries across 16 versions & 2 rubygems

Version Path
mongoid-2.0.0.rc.7 lib/mongoid/named_scope.rb
stonegao-mongoid-2.0.0.rc.6 lib/mongoid/named_scope.rb
mongoid-2.0.0.rc.6 lib/mongoid/named_scope.rb
mongoid-2.0.0.rc.5 lib/mongoid/named_scope.rb
mongoid-2.0.0.rc.4 lib/mongoid/named_scope.rb
mongoid-2.0.0.rc.3 lib/mongoid/named_scope.rb
mongoid-2.0.0.rc.2 lib/mongoid/named_scope.rb
mongoid-2.0.0.rc.1 lib/mongoid/named_scope.rb
mongoid-2.0.0.beta.20 lib/mongoid/named_scope.rb
mongoid-2.0.0.beta.19 lib/mongoid/named_scope.rb
mongoid-2.0.0.beta.18 lib/mongoid/named_scope.rb
mongoid-2.0.0.beta.17 lib/mongoid/named_scope.rb
mongoid-2.0.0.beta.16 lib/mongoid/named_scope.rb
mongoid-2.0.0.beta.15 lib/mongoid/named_scope.rb
mongoid-2.0.0.beta.14 lib/mongoid/named_scope.rb
mongoid-2.0.0.beta.13 lib/mongoid/named_scope.rb