提交 0b142a6f 编写于 作者: J Jeremy Kemper

Add a bunch of Relation -> Array delegate methods to the whitelist. This won't...

Add a bunch of Relation -> Array delegate methods to the whitelist. This won't last - aim to switch back to a blacklist for mutator methods.
上级 b474d06d
...@@ -36,10 +36,18 @@ def inherited(child_class) ...@@ -36,10 +36,18 @@ def inherited(child_class)
# may vary depending on the klass of a relation, so we create a subclass of Relation # may vary depending on the klass of a relation, so we create a subclass of Relation
# for each different klass, and the delegations are compiled into that subclass only. # for each different klass, and the delegations are compiled into that subclass only.
delegate :&, :+, :[], :all?, :collect, :detect, :each, :each_cons, # TODO: This is not going to work. Brittle, painful. We'll switch to a blacklist
:each_with_index, :flat_map, :group_by, :include?, :length, # to disallow mutator methods like map!, pop, and delete_if instead.
:map, :none?, :one?, :reverse, :sample, :second, :sort, :sort_by, ARRAY_DELEGATES = [
:to_ary, :to_set, :to_xml, :to_yaml, :to => :to_a :+, :-, :|, :&, :[],
:all?, :collect, :detect, :each, :each_cons, :each_with_index,
:exclude?, :find_all, :flat_map, :group_by, :include?, :length,
:map, :none?, :one?, :partition, :reject, :reverse,
:sample, :second, :sort, :sort_by, :third,
:to_ary, :to_set, :to_xml, :to_yaml
]
delegate *ARRAY_DELEGATES, to: :to_a
delegate :table_name, :quoted_table_name, :primary_key, :quoted_primary_key, delegate :table_name, :quoted_table_name, :primary_key, :quoted_primary_key,
:connection, :columns_hash, :to => :klass :connection, :columns_hash, :to => :klass
......
...@@ -25,16 +25,9 @@ def call_method(target, method) ...@@ -25,16 +25,9 @@ def call_method(target, method)
end end
end end
class DelegationAssociationTest < DelegationTest module DelegationWhitelistBlacklistTests
def target ActiveRecord::Delegation::ARRAY_DELEGATES.each do |method|
Post.first.comments define_method "test_delegates_#{method}_to_Array" do
end
[:&, :+, :[], :all?, :collect, :detect, :each, :each_cons,
:each_with_index, :flat_map, :group_by, :include?, :length,
:map, :none?, :one?, :reverse, :sample, :second, :sort, :sort_by,
:to_ary, :to_set, :to_xml, :to_yaml].each do |method|
test "association delegates #{method} to Array" do
assert_respond_to target, method assert_respond_to target, method
end end
end end
...@@ -42,34 +35,27 @@ def target ...@@ -42,34 +35,27 @@ def target
[:compact!, :flatten!, :reject!, :reverse!, :rotate!, [:compact!, :flatten!, :reject!, :reverse!, :rotate!,
:shuffle!, :slice!, :sort!, :sort_by!, :delete_if, :shuffle!, :slice!, :sort!, :sort_by!, :delete_if,
:keep_if, :pop, :shift, :delete_at, :compact].each do |method| :keep_if, :pop, :shift, :delete_at, :compact].each do |method|
test "#{method} is not delegated to Array" do define_method "test_#{method}_is_not_delegated_to_Array" do
assert_raises(NoMethodError) { call_method(target, method) } assert_raises(NoMethodError) { call_method(target, method) }
end end
end end
end end
class DelegationRelationTest < DelegationTest class DelegationAssociationTest < DelegationTest
fixtures :comments include DelegationWhitelistBlacklistTests
def target def target
Comment.all Post.first.comments
end end
end
[:&, :+, :[], :all?, :collect, :detect, :each, :each_cons, class DelegationRelationTest < DelegationTest
:each_with_index, :flat_map, :group_by, :include?, :length, include DelegationWhitelistBlacklistTests
:map, :none?, :one?, :reverse, :sample, :second, :sort, :sort_by,
:to_ary, :to_set, :to_xml, :to_yaml].each do |method|
test "relation delegates #{method} to Array" do
assert_respond_to target, method
end
end
[:compact!, :flatten!, :reject!, :reverse!, :rotate!, fixtures :comments
:shuffle!, :slice!, :sort!, :sort_by!, :delete_if,
:keep_if, :pop, :shift, :delete_at, :compact].each do |method| def target
test "#{method} is not delegated to Array" do Comment.all
assert_raises(NoMethodError) { call_method(target, method) }
end
end end
end end
end end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册