1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #19301 from Empact/default-scopes

Isolate access to .default_scopes in ActiveRecord::Scoping::Default
This commit is contained in:
Carlos Antonio da Silva 2015-03-12 09:36:14 -03:00
commit 6b5f815cf8
6 changed files with 20 additions and 19 deletions

View file

@ -432,8 +432,7 @@ module ActiveRecord
def get_records
if reflection.scope_chain.any?(&:any?) ||
scope.eager_loading? ||
klass.current_scope ||
klass.default_scopes.any?
klass.scope_attributes?
return scope.to_a
end

View file

@ -41,8 +41,7 @@ module ActiveRecord
def get_records
if reflection.scope_chain.any?(&:any?) ||
scope.eager_loading? ||
klass.current_scope ||
klass.default_scopes.any?
klass.scope_attributes?
return scope.limit(1).to_a
end

View file

@ -123,8 +123,7 @@ module ActiveRecord
return super unless ids.length == 1
return super if block_given? ||
primary_key.nil? ||
default_scopes.any? ||
current_scope ||
scope_attributes? ||
columns_hash.include?(inheritance_column) ||
ids.first.kind_of?(Array)
@ -152,8 +151,7 @@ module ActiveRecord
end
def find_by(*args) # :nodoc:
return super if current_scope || !(Hash === args.first) || reflect_on_all_aggregations.any?
return super if default_scopes.any?
return super if scope_attributes? || !(Hash === args.first) || reflect_on_all_aggregations.any?
hash = args.first

View file

@ -17,6 +17,17 @@ module ActiveRecord
def current_scope=(scope) #:nodoc:
ScopeRegistry.set_value_for(:current_scope, self.to_s, scope)
end
# Collects attributes from scopes that should be applied when creating
# an AR instance for the particular class this is called on.
def scope_attributes # :nodoc:
all.scope_for_create
end
# Are there attributes associated with this scope?
def scope_attributes? # :nodoc:
current_scope
end
end
def populate_with_current_scope_attributes

View file

@ -33,6 +33,11 @@ module ActiveRecord
block_given? ? relation.scoping { yield } : relation
end
# Are there attributes associated with this scope?
def scope_attributes? # :nodoc:
super || default_scopes.any?
end
def before_remove_const #:nodoc:
self.current_scope = nil
end

View file

@ -39,17 +39,6 @@ module ActiveRecord
end
end
# Collects attributes from scopes that should be applied when creating
# an AR instance for the particular class this is called on.
def scope_attributes # :nodoc:
all.scope_for_create
end
# Are there default attributes associated with this scope?
def scope_attributes? # :nodoc:
current_scope || default_scopes.any?
end
# Adds a class method for retrieving and querying objects. A \scope
# represents a narrowing of a database query, such as
# <tt>where(color: :red).select('shirts.*').includes(:washing_instructions)</tt>.