2017-07-09 13:41:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
require "cases/helper"
|
|
|
|
require "models/post"
|
|
|
|
require "models/comment"
|
2013-09-04 00:46:33 -04:00
|
|
|
|
|
|
|
module ActiveRecord
|
2018-08-27 09:30:05 -04:00
|
|
|
module ArrayDelegationTests
|
2013-12-13 19:11:39 -05:00
|
|
|
ARRAY_DELEGATES = [
|
2015-12-01 18:41:50 -05:00
|
|
|
:+, :-, :|, :&, :[], :shuffle,
|
2015-05-27 23:37:21 -04:00
|
|
|
:all?, :collect, :compact, :detect, :each, :each_cons, :each_with_index,
|
2013-12-13 19:11:39 -05:00
|
|
|
:exclude?, :find_all, :flat_map, :group_by, :include?, :length,
|
2017-08-23 04:42:21 -04:00
|
|
|
:map, :none?, :one?, :partition, :reject, :reverse, :rotate,
|
|
|
|
:sample, :second, :sort, :sort_by, :slice, :third, :index, :rindex,
|
2017-02-04 08:49:47 -05:00
|
|
|
:to_ary, :to_set, :to_xml, :to_yaml, :join,
|
2017-03-10 09:32:05 -05:00
|
|
|
:in_groups, :in_groups_of, :to_sentence, :to_formatted_s, :as_json
|
2013-12-13 19:11:39 -05:00
|
|
|
]
|
|
|
|
|
|
|
|
ARRAY_DELEGATES.each do |method|
|
2013-12-12 23:10:03 -05:00
|
|
|
define_method "test_delegates_#{method}_to_Array" do
|
2013-12-03 15:30:13 -05:00
|
|
|
assert_respond_to target, method
|
2013-09-04 00:46:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-05 17:02:01 -04:00
|
|
|
module DeprecatedArelDelegationTests
|
|
|
|
AREL_METHODS = [
|
|
|
|
:with, :orders, :froms, :project, :projections, :taken, :constraints, :exists, :locked, :where_sql,
|
2017-07-25 09:14:18 -04:00
|
|
|
:ast, :source, :join_sources, :to_dot, :create_insert, :create_true, :create_false
|
2017-05-05 17:02:01 -04:00
|
|
|
]
|
|
|
|
|
|
|
|
def test_deprecate_arel_delegation
|
|
|
|
AREL_METHODS.each do |method|
|
|
|
|
assert_deprecated { target.public_send(method) }
|
2017-08-27 09:25:10 -04:00
|
|
|
assert_deprecated { target.public_send(method) }
|
2017-05-05 17:02:01 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-05 15:52:59 -04:00
|
|
|
class DelegationAssociationTest < ActiveRecord::TestCase
|
2018-08-27 09:30:05 -04:00
|
|
|
include ArrayDelegationTests
|
2017-05-05 17:02:01 -04:00
|
|
|
include DeprecatedArelDelegationTests
|
2017-05-05 15:52:59 -04:00
|
|
|
|
2013-09-04 00:46:33 -04:00
|
|
|
def target
|
2017-08-24 08:00:00 -04:00
|
|
|
Post.new.comments
|
2013-09-04 00:46:33 -04:00
|
|
|
end
|
2013-12-12 23:10:03 -05:00
|
|
|
end
|
2013-09-04 00:46:33 -04:00
|
|
|
|
2017-05-05 15:52:59 -04:00
|
|
|
class DelegationRelationTest < ActiveRecord::TestCase
|
2018-08-27 09:30:05 -04:00
|
|
|
include ArrayDelegationTests
|
2017-05-05 17:02:01 -04:00
|
|
|
include DeprecatedArelDelegationTests
|
2013-09-04 00:46:33 -04:00
|
|
|
|
2013-12-12 23:10:03 -05:00
|
|
|
def target
|
|
|
|
Comment.all
|
2013-09-04 00:46:33 -04:00
|
|
|
end
|
|
|
|
end
|
2018-03-12 14:43:41 -04:00
|
|
|
|
|
|
|
class QueryingMethodsDelegationTest < ActiveRecord::TestCase
|
|
|
|
QUERYING_METHODS = [
|
|
|
|
:find, :take, :take!, :first, :first!, :last, :last!, :exists?, :any?, :many?, :none?, :one?,
|
|
|
|
:second, :second!, :third, :third!, :fourth, :fourth!, :fifth, :fifth!, :forty_two, :forty_two!, :third_to_last, :third_to_last!, :second_to_last, :second_to_last!,
|
|
|
|
:first_or_create, :first_or_create!, :first_or_initialize,
|
|
|
|
:find_or_create_by, :find_or_create_by!, :create_or_find_by, :create_or_find_by!, :find_or_initialize_by,
|
|
|
|
:find_by, :find_by!,
|
|
|
|
:destroy_all, :delete_all, :update_all,
|
|
|
|
:find_each, :find_in_batches, :in_batches,
|
|
|
|
:select, :group, :order, :except, :reorder, :limit, :offset, :joins, :left_joins, :left_outer_joins, :or,
|
|
|
|
:where, :rewhere, :preload, :eager_load, :includes, :from, :lock, :readonly, :extending,
|
|
|
|
:having, :create_with, :distinct, :references, :none, :unscope, :merge,
|
|
|
|
:count, :average, :minimum, :maximum, :sum, :calculate,
|
|
|
|
:pluck, :pick, :ids,
|
|
|
|
]
|
|
|
|
|
|
|
|
def test_delegate_querying_methods
|
|
|
|
klass = Class.new(ActiveRecord::Base) do
|
|
|
|
self.table_name = "posts"
|
|
|
|
end
|
|
|
|
|
|
|
|
QUERYING_METHODS.each do |method|
|
|
|
|
assert_respond_to klass.all, method
|
|
|
|
assert_respond_to klass, method
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-09-04 00:46:33 -04:00
|
|
|
end
|