提交 96bae305 编写于 作者: P Pavel Gorbokon 提交者: Aaron Patterson

Replace rudimentary named_scope with scope. [#6052 state:resolved]

* rename method names (actually in tests)
* rename instance variable @_named_scopes_cache to @_scopes_cache
* rename references in doc comments
* don't touch CHANGELOG :)
上级 aa405430
......@@ -11,7 +11,7 @@ def render_with_has_many_association
render :partial => @topic.replies
end
def render_with_named_scope
def render_with_scope
render :partial => Reply.base
end
......@@ -62,8 +62,8 @@ def test_rendering_partial_with_has_many_association
assert_equal 'Birdman is better!', @response.body
end
def test_rendering_partial_with_named_scope
get :render_with_named_scope
def test_rendering_partial_with_scope
get :render_with_scope
assert_template 'replies/_reply'
assert_equal 'Birdman is better!Nuh uh!', @response.body
end
......
......@@ -102,7 +102,7 @@ def to_ary
def reset
reset_target!
reset_named_scopes_cache!
reset_scopes_cache!
@loaded = false
end
......@@ -160,7 +160,7 @@ def delete_all
load_target
delete(@target)
reset_target!
reset_named_scopes_cache!
reset_scopes_cache!
end
# Calculate sum using SQL, not Enumerable
......@@ -253,7 +253,7 @@ def destroy_all
load_target
destroy(@target).tap do
reset_target!
reset_named_scopes_cache!
reset_scopes_cache!
end
end
......@@ -409,9 +409,9 @@ def method_missing(method, *args)
if @target.respond_to?(method) || (!@reflection.klass.respond_to?(method) && Class.respond_to?(method))
super
elsif @reflection.klass.scopes[method]
@_named_scopes_cache ||= {}
@_named_scopes_cache[method] ||= {}
@_named_scopes_cache[method][args] ||= with_scope(@scope) { @reflection.klass.send(method, *args) }
@_scopes_cache ||= {}
@_scopes_cache[method] ||= {}
@_scopes_cache[method][args] ||= with_scope(@scope) { @reflection.klass.send(method, *args) }
else
with_scope(@scope) do
if block_given?
......@@ -442,8 +442,8 @@ def reset_target!
@target = Array.new
end
def reset_named_scopes_cache!
@_named_scopes_cache = {}
def reset_scopes_cache!
@_scopes_cache = {}
end
def find_target
......
......@@ -852,8 +852,8 @@ def arel_engine
# limit(10) # Fires "SELECT * FROM posts LIMIT 10"
# }
#
# It is recommended to use block form of unscoped because chaining unscoped with <tt>named_scope</tt>
# does not work. Assuming that <tt>published</tt> is a <tt>named_scope</tt> following two statements are same.
# It is recommended to use block form of unscoped because chaining unscoped with <tt>scope</tt>
# does not work. Assuming that <tt>published</tt> is a <tt>scope</tt> following two statements are same.
#
# Post.unscoped.published
# Post.published
......
......@@ -141,26 +141,26 @@ def test_multiple_extensions
assert_equal 1, Topic.multiple_extensions.extension_one
end
def test_has_many_associations_have_access_to_named_scopes
def test_has_many_associations_have_access_to_scopes
assert_not_equal Post.containing_the_letter_a, authors(:david).posts
assert !Post.containing_the_letter_a.empty?
assert_equal authors(:david).posts & Post.containing_the_letter_a, authors(:david).posts.containing_the_letter_a
end
def test_named_scope_with_STI
def test_scope_with_STI
assert_equal 3,Post.containing_the_letter_a.count
assert_equal 1,SpecialPost.containing_the_letter_a.count
end
def test_has_many_through_associations_have_access_to_named_scopes
def test_has_many_through_associations_have_access_to_scopes
assert_not_equal Comment.containing_the_letter_e, authors(:david).comments
assert !Comment.containing_the_letter_e.empty?
assert_equal authors(:david).comments & Comment.containing_the_letter_e, authors(:david).comments.containing_the_letter_e
end
def test_named_scopes_honor_current_scopes_from_when_defined
def test_scopes_honor_current_scopes_from_when_defined
assert !Post.ranked_by_comments.limit_by(5).empty?
assert !authors(:david).posts.ranked_by_comments.limit_by(5).empty?
assert_not_equal Post.ranked_by_comments.limit_by(5), authors(:david).posts.ranked_by_comments.limit_by(5)
......@@ -236,7 +236,7 @@ def test_any_should_call_proxy_found_if_using_a_block
end
end
def test_any_should_not_fire_query_if_named_scope_loaded
def test_any_should_not_fire_query_if_scope_loaded
topics = Topic.base
topics.collect # force load
assert_no_queries { assert topics.any? }
......@@ -259,7 +259,7 @@ def test_many_should_call_proxy_found_if_using_a_block
end
end
def test_many_should_not_fire_query_if_named_scope_loaded
def test_many_should_not_fire_query_if_scope_loaded
topics = Topic.base
topics.collect # force load
assert_no_queries { assert topics.many? }
......@@ -276,27 +276,27 @@ def test_many_should_return_true_if_more_than_one
assert Topic.base.many?
end
def test_should_build_on_top_of_named_scope
def test_should_build_on_top_of_scope
topic = Topic.approved.build({})
assert topic.approved
end
def test_should_build_new_on_top_of_named_scope
def test_should_build_new_on_top_of_scope
topic = Topic.approved.new
assert topic.approved
end
def test_should_create_on_top_of_named_scope
def test_should_create_on_top_of_scope
topic = Topic.approved.create({})
assert topic.approved
end
def test_should_create_with_bang_on_top_of_named_scope
def test_should_create_with_bang_on_top_of_scope
topic = Topic.approved.create!({})
assert topic.approved
end
def test_should_build_on_top_of_chained_named_scopes
def test_should_build_on_top_of_chained_scopes
topic = Topic.approved.by_lifo.build({})
assert topic.approved
assert_equal 'lifo', topic.author_name
......@@ -310,7 +310,7 @@ def test_rand_should_select_a_random_object_from_proxy
assert_kind_of Topic, Topic.approved.sample
end
def test_should_use_where_in_query_for_named_scope
def test_should_use_where_in_query_for_scope
assert_equal Developer.find_all_by_name('Jamis').to_set, Developer.find_all_by_id(Developer.jamises).to_set
end
......@@ -361,7 +361,7 @@ def test_chaining_should_use_latest_conditions_when_searching
assert_equal [posts(:sti_comments)], Post.with_special_comments.with_post(4).all.uniq
end
def test_named_scopes_batch_finders
def test_scopes_batch_finders
assert_equal 3, Topic.approved.count
assert_queries(4) do
......@@ -381,7 +381,7 @@ def test_table_names_for_chaining_scopes_with_and_without_table_name_included
end
end
def test_named_scopes_with_reserved_names
def test_scopes_with_reserved_names
class << Topic
def public_method; end
public :public_method
......@@ -400,7 +400,7 @@ def private_method; end
end
end
def test_named_scopes_on_relations
def test_scopes_on_relations
# Topic.replied
approved_topics = Topic.scoped.approved.order('id DESC')
assert_equal topics(:fourth), approved_topics.first
......@@ -409,19 +409,19 @@ def test_named_scopes_on_relations
assert_equal topics(:third), replied_approved_topics.first
end
def test_index_on_named_scope
def test_index_on_scope
approved = Topic.approved.order('id ASC')
assert_equal topics(:second), approved[0]
assert approved.loaded?
end
def test_nested_named_scopes_queries_size
def test_nested_scopes_queries_size
assert_queries(1) do
Topic.approved.by_lifo.replied.written_before(Time.now).all
end
end
def test_named_scopes_are_cached_on_associations
def test_scopes_are_cached_on_associations
post = posts(:welcome)
assert_equal post.comments.containing_the_letter_e.object_id, post.comments.containing_the_letter_e.object_id
......@@ -430,7 +430,7 @@ def test_named_scopes_are_cached_on_associations
assert_no_queries { post.comments.containing_the_letter_e.all }
end
def test_named_scopes_with_arguments_are_cached_on_associations
def test_scopes_with_arguments_are_cached_on_associations
post = posts(:welcome)
one = post.comments.limit_by(1).all
......@@ -443,7 +443,7 @@ def test_named_scopes_with_arguments_are_cached_on_associations
assert_no_queries { post.comments.limit_by(2).all }
end
def test_named_scopes_are_reset_on_association_reload
def test_scopes_are_reset_on_association_reload
post = posts(:welcome)
[:destroy_all, :reset, :delete_all].each do |method|
......@@ -453,7 +453,7 @@ def test_named_scopes_are_reset_on_association_reload
end
end
def test_named_scoped_are_lazy_loaded_if_table_still_does_not_exist
def test_scoped_are_lazy_loaded_if_table_still_does_not_exist
assert_nothing_raised do
require "models/without_table"
end
......
......@@ -422,7 +422,7 @@ def test_nested_scope
assert_equal expected, received
end
def test_named_scope_overwrites_default
def test_scope_overwrites_default
expected = Developer.find(:all, :order => 'salary DESC, name DESC').collect { |dev| dev.name }
received = DeveloperOrderedBySalary.by_name.find(:all).collect { |dev| dev.name }
assert_equal expected, received
......
......@@ -36,7 +36,7 @@ def test_bind_values
assert_equal [], relation.bind_values
end
def test_two_named_scopes_with_includes_should_not_drop_any_include
def test_two_scopes_with_includes_should_not_drop_any_include
car = Car.incl_engines.incl_tyres.first
assert_no_queries { car.tyres.length }
assert_no_queries { car.engines.length }
......@@ -248,7 +248,7 @@ def test_respond_to_dynamic_finders
end
end
def test_respond_to_class_methods_and_named_scopes
def test_respond_to_class_methods_and_scopes
assert DeveloperOrderedBySalary.scoped.respond_to?(:all_ordered_by_name)
assert Topic.scoped.respond_to?(:by_lifo)
end
......@@ -754,7 +754,7 @@ def test_order_with_find_with_order
assert_equal 'zyke', FastCar.order('name desc').find(:first, :order => 'id').name
end
def test_default_scope_order_with_named_scope_order
def test_default_scope_order_with_scope_order
assert_equal 'zyke', CoolCar.order_using_new_style.limit(1).first.name
assert_equal 'zyke', CoolCar.order_using_old_style.limit(1).first.name
assert_equal 'zyke', FastCar.order_using_new_style.limit(1).first.name
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册