aboutsummaryrefslogblamecommitdiffstats
path: root/activerecord/test/cases/relation/delegation_test.rb
blob: 3b15f051b2167be99b335176608153287cd00748 (plain) (tree)
1
2
3
4
5
6
7
8
9


                        

                   
                                 
                       
                                    
                                                                              


                                                                     
                                                 
                                                                        


                                    
                                                          
                                        

         

     












                                                                                                         

                                                          
                                         

                   
 
              
                         
       
     
 

                                                       
                                         
 



                      


       
require "cases/helper"
require "models/post"
require "models/comment"

module ActiveRecord
  module DelegationWhitelistTests
    ARRAY_DELEGATES = [
      :+, :-, :|, :&, :[], :shuffle,
      :all?, :collect, :compact, :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, :join,
      :in_groups, :in_groups_of, :to_sentence, :to_formatted_s, :as_json
    ]

    ARRAY_DELEGATES.each do |method|
      define_method "test_delegates_#{method}_to_Array" do
        assert_respond_to target, method
      end
    end
  end

  module DeprecatedArelDelegationTests
    AREL_METHODS = [
      :with, :orders, :froms, :project, :projections, :taken, :constraints, :exists, :locked, :where_sql,
      :ast, :source, :join_sources, :to_dot, :bind_values, :create_insert, :create_true, :create_false
    ]

    def test_deprecate_arel_delegation
      AREL_METHODS.each do |method|
        assert_deprecated { target.public_send(method) }
      end
    end
  end

  class DelegationAssociationTest < ActiveRecord::TestCase
    include DelegationWhitelistTests
    include DeprecatedArelDelegationTests

    fixtures :posts

    def target
      Post.first.comments
    end
  end

  class DelegationRelationTest < ActiveRecord::TestCase
    include DelegationWhitelistTests
    include DeprecatedArelDelegationTests

    fixtures :comments

    def target
      Comment.all
    end
  end
end