module ActiveFedora::Scoping::Default::ClassMethods
def before_remove_const # :nodoc:
def before_remove_const # :nodoc: self.current_scope = nil end
def build_default_scope(base_rel = nil) # :nodoc:
def build_default_scope(base_rel = nil) # :nodoc: return if abstract_class? self.default_scope_override = !Base.is_a?(method(:default_scope).owner) if default_scope_override.nil? if default_scope_override # The user has defined their own default scope method, so call that evaluate_default_scope { default_scope } elsif default_scopes.any? base_rel ||= relation evaluate_default_scope do default_scopes.inject(base_rel) do |default_scope, scope| scope = scope.respond_to?(:to_proc) ? scope : scope.method(:call) default_scope.merge(base_rel.instance_exec(&scope)) end end end end
def default_scope(scope = nil)
end
# Should return a scope, you can call 'super' here etc.
def self.default_scope
class Article < ActiveRecord::Base
alternatively define it as a class method:
If you need to do more complex things with a default scope, you can
class defines a second one.
parent or module defines a #default_scope and the child or including
This is also the case with inheritance and module includes where the
Article.all # => SELECT * FROM articles WHERE published = true AND rating = 'G'
end
default_scope { where(rating: 'G') }
default_scope { where(published: true) }
class Article < ActiveRecord::Base
they will be merged together:
If you use multiple #default_scope declarations in your model then
default scope.)
+default_scope+ macro, and it will be called when building the
(You can also pass any object which responds to +call+ to the
Article.create.published # => true
Article.new.published # => true
It is not applied while updating a record.
The #default_scope is also applied while creating/building a record.
Article.all # => SELECT * FROM articles WHERE published = true
end
default_scope { where(published: true) }
class Article < ActiveRecord::Base
the model.
Use this macro in your model to set a default scope for all operations on
def default_scope(scope = nil) scope = Proc.new if block_given? if scope.is_a?(Relation) || !scope.respond_to?(:call) raise ArgumentError, "Support for calling #default_scope without a block is removed. For example instead " \ "of `default_scope where(color: 'red')`, please use " \ "`default_scope { where(color: 'red') }`. (Alternatively you can just redefine " \ "self.default_scope.)" end self.default_scopes += [scope] end
def evaluate_default_scope # :nodoc:
scope which references a scope...
situation where a default scope references a scope which has a default
The ignore_default_scope flag is used to prevent an infinite recursion
def evaluate_default_scope # :nodoc: return if ignore_default_scope? begin self.ignore_default_scope = true yield ensure self.ignore_default_scope = false end end
def ignore_default_scope=(ignore) # :nodoc:
def ignore_default_scope=(ignore) # :nodoc: ScopeRegistry.set_value_for(:ignore_default_scope, base_class, ignore) end
def ignore_default_scope? # :nodoc:
def ignore_default_scope? # :nodoc: ScopeRegistry.value_for(:ignore_default_scope, base_class) end
def scope_attributes? # :nodoc:
Are there attributes associated with this scope?
def scope_attributes? # :nodoc: super || default_scopes.any? || respond_to?(:default_scope) end
def unscoped
Post.unscoped.published
are equal: the +default_scope+ is applied on both.
+published+ is a +scope+, the following two statements
chaining unscoped with +scope+ does not work. Assuming that
It is recommended that you use the block form of unscoped because
}
Post.limit(10) # Fires "SELECT * FROM posts LIMIT 10"
Post.unscoped {
not use the +default_scope+:
This method also accepts a block. All queries inside the block will
Post.unscoped.all # Fires "SELECT * FROM posts"
Post.all # Fires "SELECT * FROM posts WHERE published = true"
end
end
where published: true
def self.default_scope
class Post < ActiveRecord::Base
Returns a scope for the model without the +default_scope+.
def unscoped block_given? ? relation.scoping { yield } : relation end