1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #40291 from rails/revert-39254-through_cast_check_attribute_override

Revert "Refactor uncastable through reflection test to detect join key overrides"
This commit is contained in:
Gannon McGibbon 2020-09-25 18:51:17 -04:00 committed by GitHub
commit f40c17dcfa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 1 additions and 113 deletions

View file

@ -914,10 +914,6 @@
*Ryuta Kamizono* *Ryuta Kamizono*
* Ensure custom PK types are casted in through reflection queries.
*Gannon McGibbon*
* Preserve user supplied joins order as much as possible. * Preserve user supplied joins order as much as possible.
Fixes #36761, #34328, #24281, #12953. Fixes #36761, #34328, #24281, #12953.

View file

@ -766,9 +766,7 @@ module ActiveRecord
end end
def klass def klass
@klass ||= delegate_reflection.compute_class(class_name).tap do |klass| @klass ||= delegate_reflection.compute_class(class_name)
check_reflection_validity!(klass)
end
end end
# Returns the source of the through reflection. It checks both a singularized # Returns the source of the through reflection. It checks both a singularized
@ -999,26 +997,6 @@ module ActiveRecord
options[:source_type] || source_reflection.class_name options[:source_type] || source_reflection.class_name
end end
def custom_join_key_type?
source_reflection.active_record.attributes_to_define_after_schema_loads.key?(
delegate_reflection.foreign_key
)
end
def check_reflection_validity!(klass)
return unless custom_join_key_type?
through_reflection = options[:through].to_s
unless klass.reflections.key?(through_reflection) ||
klass.reflections.key?(through_reflection.pluralize)
raise NotImplementedError, <<~MSG.squish
In order to correctly type cast #{active_record}.#{active_record.primary_key},
#{klass} needs to define a :#{through_reflection} association.
MSG
end
end
delegate_methods = AssociationReflection.public_instance_methods - delegate_methods = AssociationReflection.public_instance_methods -
public_instance_methods public_instance_methods

View file

@ -518,85 +518,3 @@ class ReflectionTest < ActiveRecord::TestCase
end end
end end
end end
class UncastableOverriddenAttributeReflectionTest < ActiveRecord::TestCase
class NickType < ActiveRecord::Type::Binary
def serialize(value)
super("nickname-#{value}")
end
def deserialize(value)
super(value).to_s.delete_prefix("nickname-")
end
def cast_value(value)
value.to_s
end
end
class Book < ActiveRecord::Base
end
class Subscription < ActiveRecord::Base
belongs_to :subscriber
belongs_to :book
attribute :subscriber_id, NickType.new
end
class Subscriber < ActiveRecord::Base
self.primary_key = "nick"
attribute :nick, NickType.new
has_many :subscriptions
has_one :subscription
has_many :books, through: :subscriptions
has_one :book, through: :subscription
end
setup do
@subscriber = Subscriber.create!(nick: "unique")
end
teardown do
Book._reflections.clear
Book.clear_reflections_cache
silence_warnings do
Subscriber.has_many :books, through: :subscriptions
Subscriber.has_one :book, through: :subscription
end
end
test "uncastable has_many through: reflection" do
error = assert_raises(NotImplementedError) { @subscriber.books }
assert_equal <<~MSG.squish, error.message
In order to correctly type cast #{self.class}::Subscriber.nick,
#{self.class}::Book needs to define a :subscriptions association.
MSG
end
test "uncastable has_one through: reflection" do
error = assert_raises(NotImplementedError) { @subscriber.book }
assert_equal <<~MSG.squish, error.message
In order to correctly type cast #{self.class}::Subscriber.nick,
#{self.class}::Book needs to define a :subscription association.
MSG
end
test "fixing uncastable has_many through: reflection with has_many" do
silence_warnings do
Book.has_many :subscriptions
end
@subscriber.books
end
test "fixing uncastable has_one through: reflection with has_many" do
silence_warnings do
Book.has_many :subscriptions
end
@subscriber.book
end
test "fixing uncastable has_one through: reflection with has_one" do
Book.has_one :subscription
@subscriber.book
end
end

View file

@ -2,6 +2,4 @@
class Dashboard < ActiveRecord::Base class Dashboard < ActiveRecord::Base
self.primary_key = :dashboard_id self.primary_key = :dashboard_id
has_one :speedometer
end end

View file

@ -4,7 +4,6 @@ class Subscriber < ActiveRecord::Base
self.primary_key = "nick" self.primary_key = "nick"
has_many :subscriptions has_many :subscriptions
has_many :books, through: :subscriptions has_many :books, through: :subscriptions
has_many :published_books, through: :subscriptions
end end
class SpecialSubscriber < Subscriber class SpecialSubscriber < Subscriber

View file

@ -2,7 +2,6 @@
class Subscription < ActiveRecord::Base class Subscription < ActiveRecord::Base
belongs_to :subscriber, counter_cache: :books_count belongs_to :subscriber, counter_cache: :books_count
belongs_to :published_book
belongs_to :book belongs_to :book
validates_presence_of :subscriber_id, :book_id validates_presence_of :subscriber_id, :book_id