Skip to content
This repository
Browse code

Add a bunch of Relation -> Array delegate methods to the whitelist. T…

…his won't last - aim to switch back to a blacklist for mutator methods.
  • Loading branch information...
commit 0b142a6f842051e3f1f3c146d1e1318050274352 1 parent b474d06
Jeremy Kemper jeremy authored
16 activerecord/lib/active_record/relation/delegation.rb
@@ -36,10 +36,18 @@ def inherited(child_class)
36 36 # may vary depending on the klass of a relation, so we create a subclass of Relation
37 37 # for each different klass, and the delegations are compiled into that subclass only.
38 38
39   - delegate :&, :+, :[], :all?, :collect, :detect, :each, :each_cons,
40   - :each_with_index, :flat_map, :group_by, :include?, :length,
41   - :map, :none?, :one?, :reverse, :sample, :second, :sort, :sort_by,
42   - :to_ary, :to_set, :to_xml, :to_yaml, :to => :to_a
  39 + # TODO: This is not going to work. Brittle, painful. We'll switch to a blacklist
  40 + # to disallow mutator methods like map!, pop, and delete_if instead.
  41 + ARRAY_DELEGATES = [
  42 + :+, :-, :|, :&, :[],
  43 + :all?, :collect, :detect, :each, :each_cons, :each_with_index,
  44 + :exclude?, :find_all, :flat_map, :group_by, :include?, :length,
  45 + :map, :none?, :one?, :partition, :reject, :reverse,
  46 + :sample, :second, :sort, :sort_by, :third,
  47 + :to_ary, :to_set, :to_xml, :to_yaml
  48 + ]
  49 +
  50 + delegate *ARRAY_DELEGATES, to: :to_a
43 51
44 52 delegate :table_name, :quoted_table_name, :primary_key, :quoted_primary_key,
45 53 :connection, :columns_hash, :to => :klass
42 activerecord/test/cases/relation/delegation_test.rb
@@ -25,16 +25,9 @@ def call_method(target, method)
25 25 end
26 26 end
27 27
28   - class DelegationAssociationTest < DelegationTest
29   - def target
30   - Post.first.comments
31   - end
32   -
33   - [:&, :+, :[], :all?, :collect, :detect, :each, :each_cons,
34   - :each_with_index, :flat_map, :group_by, :include?, :length,
35   - :map, :none?, :one?, :reverse, :sample, :second, :sort, :sort_by,
36   - :to_ary, :to_set, :to_xml, :to_yaml].each do |method|
37   - test "association delegates #{method} to Array" do
  28 + module DelegationWhitelistBlacklistTests
  29 + ActiveRecord::Delegation::ARRAY_DELEGATES.each do |method|
  30 + define_method "test_delegates_#{method}_to_Array" do
38 31 assert_respond_to target, method
39 32 end
40 33 end
@@ -42,34 +35,27 @@ def target
42 35 [:compact!, :flatten!, :reject!, :reverse!, :rotate!,
43 36 :shuffle!, :slice!, :sort!, :sort_by!, :delete_if,
44 37 :keep_if, :pop, :shift, :delete_at, :compact].each do |method|
45   - test "#{method} is not delegated to Array" do
  38 + define_method "test_#{method}_is_not_delegated_to_Array" do
46 39 assert_raises(NoMethodError) { call_method(target, method) }
47 40 end
48 41 end
49 42 end
50 43
51   - class DelegationRelationTest < DelegationTest
52   - fixtures :comments
  44 + class DelegationAssociationTest < DelegationTest
  45 + include DelegationWhitelistBlacklistTests
53 46
54 47 def target
55   - Comment.all
  48 + Post.first.comments
56 49 end
  50 + end
57 51
58   - [:&, :+, :[], :all?, :collect, :detect, :each, :each_cons,
59   - :each_with_index, :flat_map, :group_by, :include?, :length,
60   - :map, :none?, :one?, :reverse, :sample, :second, :sort, :sort_by,
61   - :to_ary, :to_set, :to_xml, :to_yaml].each do |method|
62   - test "relation delegates #{method} to Array" do
63   - assert_respond_to target, method
64   - end
65   - end
  52 + class DelegationRelationTest < DelegationTest
  53 + include DelegationWhitelistBlacklistTests
66 54
67   - [:compact!, :flatten!, :reject!, :reverse!, :rotate!,
68   - :shuffle!, :slice!, :sort!, :sort_by!, :delete_if,
69   - :keep_if, :pop, :shift, :delete_at, :compact].each do |method|
70   - test "#{method} is not delegated to Array" do
71   - assert_raises(NoMethodError) { call_method(target, method) }
72   - end
  55 + fixtures :comments
  56 +
  57 + def target
  58 + Comment.all
73 59 end
74 60 end
75 61 end

0 comments on commit 0b142a6

Please sign in to comment.
Something went wrong with that request. Please try again.