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