1
0
Fork 0
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:
Jon Leighton 2012-08-01 23:55:47 +01:00
parent 0e1cafcbc4
commit b33e7ba140
9 changed files with 33 additions and 27 deletions

View file

@ -1,5 +1,6 @@
require 'active_support/core_ext/array/wrap'
require 'active_support/core_ext/object/inclusion'
require 'active_support/deprecation'
module ActiveRecord
module Associations
@ -81,10 +82,15 @@ module ActiveRecord
loaded!
end
def scoped
def scope
target_scope.merge(association_scope)
end
def scoped
ActiveSupport::Deprecation.warn("#scoped is deprecated. use #scope instead.")
scope
end
# The scope for this association.
#
# Note that the association_scope is merged into the target_scope only when the

View file

@ -50,7 +50,7 @@ module ActiveRecord
end
else
column = "#{reflection.quoted_table_name}.#{reflection.association_primary_key}"
scoped.pluck(column)
scope.pluck(column)
end
end
@ -71,7 +71,7 @@ module ActiveRecord
if block_given?
load_target.select.each { |e| yield e }
else
scoped.select(select)
scope.select(select)
end
end
@ -82,7 +82,7 @@ module ActiveRecord
if options[:finder_sql]
find_by_scan(*args)
else
scoped.find(*args)
scope.find(*args)
end
end
end
@ -164,9 +164,9 @@ module ActiveRecord
# Calculate sum using SQL, not Enumerable.
def sum(*args)
if block_given?
scoped.sum(*args) { |*block_args| yield(*block_args) }
scope.sum(*args) { |*block_args| yield(*block_args) }
else
scoped.sum(*args)
scope.sum(*args)
end
end
@ -189,7 +189,7 @@ module ActiveRecord
count_options[:distinct] = true
end
value = scoped.count(column_name, count_options)
value = scope.count(column_name, count_options)
limit = options[:limit]
offset = options[:offset]
@ -324,7 +324,7 @@ module ActiveRecord
include_in_memory?(record)
else
load_target if options[:finder_sql]
loaded? ? target.include?(record) : scoped.exists?(record)
loaded? ? target.include?(record) : scope.exists?(record)
end
else
false
@ -380,7 +380,7 @@ module ActiveRecord
if options[:finder_sql]
reflection.klass.find_by_sql(custom_finder_sql)
else
scoped.to_a
scope.to_a
end
records.each { |record| set_inverse_instance(record) }
@ -440,7 +440,7 @@ module ActiveRecord
end
def create_scope
scoped.scope_for_create.stringify_keys
scope.scope_for_create.stringify_keys
end
def delete_or_destroy(records, method)
@ -565,7 +565,7 @@ module ActiveRecord
def first_or_last(type, *args)
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)
end
end

View file

@ -37,7 +37,7 @@ module ActiveRecord
def initialize(association) #:nodoc:
@association = association
super association.klass, association.klass.arel_table
merge! association.scoped
merge! association.scope
end
def target
@ -852,14 +852,14 @@ module ActiveRecord
# method, which gets the current scope, which is this object, which
# delegates to @association, and so on.
def scoping
@association.scoped.scoping { yield }
@association.scope.scoping { yield }
end
# Returns a <tt>Relation</tt> object for the records in this association
def scope
association = @association
@association.scoped.extending! do
@association.scope.extending! do
define_method(:proxy_association) { association }
end
end

View file

@ -38,7 +38,7 @@ module ActiveRecord
elsif options[:counter_sql] || options[:finder_sql]
reflection.klass.count_by_sql(custom_counter_sql)
else
scoped.count
scope.count
end
# 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?
else
if records == :all
scope = scoped
scope = self.scope
else
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
if method == :delete_all

View file

@ -126,7 +126,7 @@ module ActiveRecord
# even when we just want to delete everything.
records = load_target if records == :all
scope = through_association.scoped
scope = through_association.scope
scope.where! construct_join_attributes(*records)
case method
@ -171,7 +171,7 @@ module ActiveRecord
def find_target
return [] unless target_reflection_has_associated_record?
scoped.to_a
scope.to_a
end
# NOTE - not sure that we can actually cope with inverses here

View file

@ -10,7 +10,7 @@ module ActiveRecord
@reflection = reflection
@preload_scope = preload_scope
@model = owners.first && owners.first.class
@scoped = nil
@scope = nil
@owners_by_key = nil
end
@ -24,12 +24,12 @@ module ActiveRecord
raise NotImplementedError
end
def scoped
@scoped ||= build_scope
def scope
@scope ||= build_scope
end
def records_for(ids)
scoped.where(association_key.in(ids))
scope.where(association_key.in(ids))
end
def table

View file

@ -35,11 +35,11 @@ module ActiveRecord
private
def create_scope
scoped.scope_for_create.stringify_keys.except(klass.primary_key)
scope.scope_for_create.stringify_keys.except(klass.primary_key)
end
def find_target
scoped.first.tap { |record| set_inverse_instance(record) }
scope.first.tap { |record| set_inverse_instance(record) }
end
# Implemented by subclasses

View file

@ -409,7 +409,7 @@ module ActiveRecord
association.target
else
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
attributes_collection.each do |attributes|

View file

@ -226,7 +226,7 @@ class HasOneAssociationsTest < ActiveRecord::TestCase
def test_build_and_create_should_not_happen_within_scope
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
assert_not_equal scoped_count, bulb.scope_after_initialize.where_values.count