mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
s/scoped/scope/
This commit is contained in:
parent
0e1cafcbc4
commit
b33e7ba140
9 changed files with 33 additions and 27 deletions
|
@ -1,5 +1,6 @@
|
||||||
require 'active_support/core_ext/array/wrap'
|
require 'active_support/core_ext/array/wrap'
|
||||||
require 'active_support/core_ext/object/inclusion'
|
require 'active_support/core_ext/object/inclusion'
|
||||||
|
require 'active_support/deprecation'
|
||||||
|
|
||||||
module ActiveRecord
|
module ActiveRecord
|
||||||
module Associations
|
module Associations
|
||||||
|
@ -81,10 +82,15 @@ module ActiveRecord
|
||||||
loaded!
|
loaded!
|
||||||
end
|
end
|
||||||
|
|
||||||
def scoped
|
def scope
|
||||||
target_scope.merge(association_scope)
|
target_scope.merge(association_scope)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def scoped
|
||||||
|
ActiveSupport::Deprecation.warn("#scoped is deprecated. use #scope instead.")
|
||||||
|
scope
|
||||||
|
end
|
||||||
|
|
||||||
# The scope for this association.
|
# The scope for this association.
|
||||||
#
|
#
|
||||||
# Note that the association_scope is merged into the target_scope only when the
|
# Note that the association_scope is merged into the target_scope only when the
|
||||||
|
|
|
@ -50,7 +50,7 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
column = "#{reflection.quoted_table_name}.#{reflection.association_primary_key}"
|
column = "#{reflection.quoted_table_name}.#{reflection.association_primary_key}"
|
||||||
scoped.pluck(column)
|
scope.pluck(column)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -71,7 +71,7 @@ module ActiveRecord
|
||||||
if block_given?
|
if block_given?
|
||||||
load_target.select.each { |e| yield e }
|
load_target.select.each { |e| yield e }
|
||||||
else
|
else
|
||||||
scoped.select(select)
|
scope.select(select)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -82,7 +82,7 @@ module ActiveRecord
|
||||||
if options[:finder_sql]
|
if options[:finder_sql]
|
||||||
find_by_scan(*args)
|
find_by_scan(*args)
|
||||||
else
|
else
|
||||||
scoped.find(*args)
|
scope.find(*args)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -164,9 +164,9 @@ module ActiveRecord
|
||||||
# Calculate sum using SQL, not Enumerable.
|
# Calculate sum using SQL, not Enumerable.
|
||||||
def sum(*args)
|
def sum(*args)
|
||||||
if block_given?
|
if block_given?
|
||||||
scoped.sum(*args) { |*block_args| yield(*block_args) }
|
scope.sum(*args) { |*block_args| yield(*block_args) }
|
||||||
else
|
else
|
||||||
scoped.sum(*args)
|
scope.sum(*args)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -189,7 +189,7 @@ module ActiveRecord
|
||||||
count_options[:distinct] = true
|
count_options[:distinct] = true
|
||||||
end
|
end
|
||||||
|
|
||||||
value = scoped.count(column_name, count_options)
|
value = scope.count(column_name, count_options)
|
||||||
|
|
||||||
limit = options[:limit]
|
limit = options[:limit]
|
||||||
offset = options[:offset]
|
offset = options[:offset]
|
||||||
|
@ -324,7 +324,7 @@ module ActiveRecord
|
||||||
include_in_memory?(record)
|
include_in_memory?(record)
|
||||||
else
|
else
|
||||||
load_target if options[:finder_sql]
|
load_target if options[:finder_sql]
|
||||||
loaded? ? target.include?(record) : scoped.exists?(record)
|
loaded? ? target.include?(record) : scope.exists?(record)
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
false
|
false
|
||||||
|
@ -380,7 +380,7 @@ module ActiveRecord
|
||||||
if options[:finder_sql]
|
if options[:finder_sql]
|
||||||
reflection.klass.find_by_sql(custom_finder_sql)
|
reflection.klass.find_by_sql(custom_finder_sql)
|
||||||
else
|
else
|
||||||
scoped.to_a
|
scope.to_a
|
||||||
end
|
end
|
||||||
|
|
||||||
records.each { |record| set_inverse_instance(record) }
|
records.each { |record| set_inverse_instance(record) }
|
||||||
|
@ -440,7 +440,7 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_scope
|
def create_scope
|
||||||
scoped.scope_for_create.stringify_keys
|
scope.scope_for_create.stringify_keys
|
||||||
end
|
end
|
||||||
|
|
||||||
def delete_or_destroy(records, method)
|
def delete_or_destroy(records, method)
|
||||||
|
@ -565,7 +565,7 @@ module ActiveRecord
|
||||||
def first_or_last(type, *args)
|
def first_or_last(type, *args)
|
||||||
args.shift if args.first.is_a?(Hash) && args.first.empty?
|
args.shift if args.first.is_a?(Hash) && args.first.empty?
|
||||||
|
|
||||||
collection = fetch_first_or_last_using_find?(args) ? scoped : load_target
|
collection = fetch_first_or_last_using_find?(args) ? scope : load_target
|
||||||
collection.send(type, *args)
|
collection.send(type, *args)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -37,7 +37,7 @@ module ActiveRecord
|
||||||
def initialize(association) #:nodoc:
|
def initialize(association) #:nodoc:
|
||||||
@association = association
|
@association = association
|
||||||
super association.klass, association.klass.arel_table
|
super association.klass, association.klass.arel_table
|
||||||
merge! association.scoped
|
merge! association.scope
|
||||||
end
|
end
|
||||||
|
|
||||||
def target
|
def target
|
||||||
|
@ -852,14 +852,14 @@ module ActiveRecord
|
||||||
# method, which gets the current scope, which is this object, which
|
# method, which gets the current scope, which is this object, which
|
||||||
# delegates to @association, and so on.
|
# delegates to @association, and so on.
|
||||||
def scoping
|
def scoping
|
||||||
@association.scoped.scoping { yield }
|
@association.scope.scoping { yield }
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns a <tt>Relation</tt> object for the records in this association
|
# Returns a <tt>Relation</tt> object for the records in this association
|
||||||
def scope
|
def scope
|
||||||
association = @association
|
association = @association
|
||||||
|
|
||||||
@association.scoped.extending! do
|
@association.scope.extending! do
|
||||||
define_method(:proxy_association) { association }
|
define_method(:proxy_association) { association }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -38,7 +38,7 @@ module ActiveRecord
|
||||||
elsif options[:counter_sql] || options[:finder_sql]
|
elsif options[:counter_sql] || options[:finder_sql]
|
||||||
reflection.klass.count_by_sql(custom_counter_sql)
|
reflection.klass.count_by_sql(custom_counter_sql)
|
||||||
else
|
else
|
||||||
scoped.count
|
scope.count
|
||||||
end
|
end
|
||||||
|
|
||||||
# If there's nothing in the database and @target has no new records
|
# If there's nothing in the database and @target has no new records
|
||||||
|
@ -90,10 +90,10 @@ module ActiveRecord
|
||||||
update_counter(-records.length) unless inverse_updates_counter_cache?
|
update_counter(-records.length) unless inverse_updates_counter_cache?
|
||||||
else
|
else
|
||||||
if records == :all
|
if records == :all
|
||||||
scope = scoped
|
scope = self.scope
|
||||||
else
|
else
|
||||||
keys = records.map { |r| r[reflection.association_primary_key] }
|
keys = records.map { |r| r[reflection.association_primary_key] }
|
||||||
scope = scoped.where(reflection.association_primary_key => keys)
|
scope = self.scope.where(reflection.association_primary_key => keys)
|
||||||
end
|
end
|
||||||
|
|
||||||
if method == :delete_all
|
if method == :delete_all
|
||||||
|
|
|
@ -126,7 +126,7 @@ module ActiveRecord
|
||||||
# even when we just want to delete everything.
|
# even when we just want to delete everything.
|
||||||
records = load_target if records == :all
|
records = load_target if records == :all
|
||||||
|
|
||||||
scope = through_association.scoped
|
scope = through_association.scope
|
||||||
scope.where! construct_join_attributes(*records)
|
scope.where! construct_join_attributes(*records)
|
||||||
|
|
||||||
case method
|
case method
|
||||||
|
@ -171,7 +171,7 @@ module ActiveRecord
|
||||||
|
|
||||||
def find_target
|
def find_target
|
||||||
return [] unless target_reflection_has_associated_record?
|
return [] unless target_reflection_has_associated_record?
|
||||||
scoped.to_a
|
scope.to_a
|
||||||
end
|
end
|
||||||
|
|
||||||
# NOTE - not sure that we can actually cope with inverses here
|
# NOTE - not sure that we can actually cope with inverses here
|
||||||
|
|
|
@ -10,7 +10,7 @@ module ActiveRecord
|
||||||
@reflection = reflection
|
@reflection = reflection
|
||||||
@preload_scope = preload_scope
|
@preload_scope = preload_scope
|
||||||
@model = owners.first && owners.first.class
|
@model = owners.first && owners.first.class
|
||||||
@scoped = nil
|
@scope = nil
|
||||||
@owners_by_key = nil
|
@owners_by_key = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -24,12 +24,12 @@ module ActiveRecord
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
end
|
end
|
||||||
|
|
||||||
def scoped
|
def scope
|
||||||
@scoped ||= build_scope
|
@scope ||= build_scope
|
||||||
end
|
end
|
||||||
|
|
||||||
def records_for(ids)
|
def records_for(ids)
|
||||||
scoped.where(association_key.in(ids))
|
scope.where(association_key.in(ids))
|
||||||
end
|
end
|
||||||
|
|
||||||
def table
|
def table
|
||||||
|
|
|
@ -35,11 +35,11 @@ module ActiveRecord
|
||||||
private
|
private
|
||||||
|
|
||||||
def create_scope
|
def create_scope
|
||||||
scoped.scope_for_create.stringify_keys.except(klass.primary_key)
|
scope.scope_for_create.stringify_keys.except(klass.primary_key)
|
||||||
end
|
end
|
||||||
|
|
||||||
def find_target
|
def find_target
|
||||||
scoped.first.tap { |record| set_inverse_instance(record) }
|
scope.first.tap { |record| set_inverse_instance(record) }
|
||||||
end
|
end
|
||||||
|
|
||||||
# Implemented by subclasses
|
# Implemented by subclasses
|
||||||
|
|
|
@ -409,7 +409,7 @@ module ActiveRecord
|
||||||
association.target
|
association.target
|
||||||
else
|
else
|
||||||
attribute_ids = attributes_collection.map {|a| a['id'] || a[:id] }.compact
|
attribute_ids = attributes_collection.map {|a| a['id'] || a[:id] }.compact
|
||||||
attribute_ids.empty? ? [] : association.scoped.where(association.klass.primary_key => attribute_ids)
|
attribute_ids.empty? ? [] : association.scope.where(association.klass.primary_key => attribute_ids)
|
||||||
end
|
end
|
||||||
|
|
||||||
attributes_collection.each do |attributes|
|
attributes_collection.each do |attributes|
|
||||||
|
|
|
@ -226,7 +226,7 @@ class HasOneAssociationsTest < ActiveRecord::TestCase
|
||||||
|
|
||||||
def test_build_and_create_should_not_happen_within_scope
|
def test_build_and_create_should_not_happen_within_scope
|
||||||
pirate = pirates(:blackbeard)
|
pirate = pirates(:blackbeard)
|
||||||
scoped_count = pirate.association(:foo_bulb).scoped.where_values.count
|
scoped_count = pirate.association(:foo_bulb).scope.where_values.count
|
||||||
|
|
||||||
bulb = pirate.build_foo_bulb
|
bulb = pirate.build_foo_bulb
|
||||||
assert_not_equal scoped_count, bulb.scope_after_initialize.where_values.count
|
assert_not_equal scoped_count, bulb.scope_after_initialize.where_values.count
|
||||||
|
|
Loading…
Reference in a new issue