diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 439395297e1b0..36c5997ea78ac 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -105,17 +105,6 @@ *Justin Talbott* -* Infer default `:inverse_of` option for `delegated_type` definitions. - - ```ruby - class Entry < ApplicationRecord - delegated_type :entryable, types: %w[ Message ] - # => defaults to inverse_of: :entry - end - ``` - - *Sean Doyle* - * Add support for `ActiveRecord::Point` type casts using `Hash` values This allows `ActiveRecord::Point` to be cast or serialized from a hash diff --git a/activerecord/lib/active_record/delegated_type.rb b/activerecord/lib/active_record/delegated_type.rb index 44735adc9e096..e680edfa4e2f2 100644 --- a/activerecord/lib/active_record/delegated_type.rb +++ b/activerecord/lib/active_record/delegated_type.rb @@ -220,14 +220,6 @@ module DelegatedType # [:primary_key] # Specify the method that returns the primary key of associated object used for the convenience methods. # By default this is +id+. - # [+:inverse_of+] - # Specifies the name of the #belongs_to association on the associated object - # that is the inverse of this #has_one association. By default, the class - # singularized class name is used unless a :foreign_key option is - # also provided. For example, a call to - # Entry.delegated_type will default to inverse_of: :entry. - # See ActiveRecord::Associations::ClassMethods's overview on Bi-directional - # associations for more detail. # # Option examples: # class Entry < ApplicationRecord @@ -237,8 +229,6 @@ module DelegatedType # Entry#message_uuid # => returns entryable_uuid, when entryable_type == "Message", otherwise nil # Entry#comment_uuid # => returns entryable_uuid, when entryable_type == "Comment", otherwise nil def delegated_type(role, types:, **options) - options[:inverse_of] = model_name.singular unless options.key?(:inverse_of) || options.key?(:foreign_key) - belongs_to role, options.delete(:scope), **options.merge(polymorphic: true) define_delegated_type_methods role, types: types, options: options end diff --git a/activerecord/test/cases/delegated_type_test.rb b/activerecord/test/cases/delegated_type_test.rb index 4455cdecb81fc..b541435ee605a 100644 --- a/activerecord/test/cases/delegated_type_test.rb +++ b/activerecord/test/cases/delegated_type_test.rb @@ -3,7 +3,6 @@ require "cases/helper" require "models/account" require "models/entry" -require "models/essay" require "models/message" require "models/recipient" require "models/comment" @@ -12,12 +11,12 @@ require "models/uuid_comment" class DelegatedTypeTest < ActiveRecord::TestCase - fixtures :comments, :accounts, :essays + fixtures :comments, :accounts, :posts setup do @entry_with_message = Entry.create! entryable: Message.new(subject: "Hello world!"), account: accounts(:signals37) @entry_with_comment = Entry.create! entryable: comments(:greetings), account: accounts(:signals37) - @entry_with_essay = Entry.create! thing: essays(:mary_stay_home), account: accounts(:signals37) + @entry_with_post = Entry.create! thing: posts(:welcome), account: accounts(:signals37) if current_adapter?(:PostgreSQLAdapter) @uuid_entry_with_message = UuidEntry.create! uuid: SecureRandom.uuid, entryable: UuidMessage.new(uuid: SecureRandom.uuid, subject: "Hello world!") @@ -37,7 +36,7 @@ class DelegatedTypeTest < ActiveRecord::TestCase test "delegated class with custom foreign_type" do assert_equal Message, @entry_with_message.thing_class assert_equal Comment, @entry_with_comment.thing_class - assert_equal Essay, @entry_with_essay.thing_class + assert_equal Post, @entry_with_post.thing_class end test "delegated type name" do @@ -57,9 +56,9 @@ class DelegatedTypeTest < ActiveRecord::TestCase end test "delegated type predicates with custom foreign_type" do - assert_predicate @entry_with_essay, :essay? - assert_not @entry_with_message.essay? - assert_not @entry_with_comment.essay? + assert_predicate @entry_with_post, :post? + assert_not @entry_with_message.post? + assert_not @entry_with_comment.post? end test "scope" do @@ -68,7 +67,7 @@ class DelegatedTypeTest < ActiveRecord::TestCase end test "scope with custom foreign_type" do - assert_predicate Entry.essays.first, :essay? + assert_predicate Entry.posts.first, :post? end test "accessor" do @@ -97,15 +96,6 @@ class DelegatedTypeTest < ActiveRecord::TestCase assert_nil @uuid_entry_with_comment.uuid_message_uuid end - test "association inverse_of" do - created = @entry_with_message.message - updated = @entry_with_message.build_entryable subject: "Goodbye world!" - - assert_changes -> { @entry_with_message.reload.message }, from: created, to: updated do - updated.save! - end - end - test "touch account" do previous_account_updated_at = @entry_with_message.account.updated_at previous_entry_updated_at = @entry_with_message.updated_at diff --git a/activerecord/test/models/comment.rb b/activerecord/test/models/comment.rb index 76b319ac65bae..c8ebc12429069 100644 --- a/activerecord/test/models/comment.rb +++ b/activerecord/test/models/comment.rb @@ -1,13 +1,9 @@ # frozen_string_literal: true -require "models/entryable" - # `counter_cache` requires association class before `attr_readonly`. class Post < ActiveRecord::Base; end class Comment < ActiveRecord::Base - include Entryable - scope :limit_by, lambda { |l| limit(l) } scope :containing_the_letter_e, -> { where("comments.body LIKE '%e%'") } scope :not_again, -> { where("comments.body NOT LIKE '%again%'") } diff --git a/activerecord/test/models/entry.rb b/activerecord/test/models/entry.rb index 37f71e6874b74..c5ca4bcc14892 100644 --- a/activerecord/test/models/entry.rb +++ b/activerecord/test/models/entry.rb @@ -5,6 +5,6 @@ class Entry < ActiveRecord::Base belongs_to :account, touch: true # alternate delegation for custom foreign_key/foreign_type - delegated_type :thing, types: %w[ Essay ], + delegated_type :thing, types: %w[ Post ], foreign_key: :entryable_id, foreign_type: :entryable_type end diff --git a/activerecord/test/models/entryable.rb b/activerecord/test/models/entryable.rb deleted file mode 100644 index 7225542acb49f..0000000000000 --- a/activerecord/test/models/entryable.rb +++ /dev/null @@ -1,11 +0,0 @@ -# frozen_string_literal: true - -require "models/entry" - -module Entryable - extend ActiveSupport::Concern - - included do - has_one :entry, as: :entryable, touch: true - end -end diff --git a/activerecord/test/models/essay.rb b/activerecord/test/models/essay.rb index 662fc7fab6d0d..eb2fb51f1f16c 100644 --- a/activerecord/test/models/essay.rb +++ b/activerecord/test/models/essay.rb @@ -1,10 +1,6 @@ # frozen_string_literal: true -require "models/entryable" - class Essay < ActiveRecord::Base - include Entryable - belongs_to :author, primary_key: :name belongs_to :writer, primary_key: :name, polymorphic: true belongs_to :category, primary_key: :name diff --git a/activerecord/test/models/message.rb b/activerecord/test/models/message.rb index 67ecb50586ee1..b8a6c315e2a01 100644 --- a/activerecord/test/models/message.rb +++ b/activerecord/test/models/message.rb @@ -1,9 +1,6 @@ # frozen_string_literal: true -require "models/entryable" - class Message < ActiveRecord::Base - include Entryable - + has_one :entry, as: :entryable, touch: true has_many :recipients end diff --git a/activerecord/test/models/uuid_comment.rb b/activerecord/test/models/uuid_comment.rb index bd145178e1cb9..62736af32f63b 100644 --- a/activerecord/test/models/uuid_comment.rb +++ b/activerecord/test/models/uuid_comment.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require "models/uuid_entryable" - class UuidComment < ActiveRecord::Base - include UuidEntryable + has_one :uuid_entry, as: :entryable end diff --git a/activerecord/test/models/uuid_entryable.rb b/activerecord/test/models/uuid_entryable.rb deleted file mode 100644 index 4200208979849..0000000000000 --- a/activerecord/test/models/uuid_entryable.rb +++ /dev/null @@ -1,11 +0,0 @@ -# frozen_string_literal: true - -require "models/uuid_entry" - -module UuidEntryable - extend ActiveSupport::Concern - - included do - has_one :uuid_entry, as: :entryable - end -end diff --git a/activerecord/test/models/uuid_message.rb b/activerecord/test/models/uuid_message.rb index 0b69cbaf56d86..904414f8de5cf 100644 --- a/activerecord/test/models/uuid_message.rb +++ b/activerecord/test/models/uuid_message.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require "models/uuid_entryable" - class UuidMessage < ActiveRecord::Base - include UuidEntryable + has_one :uuid_entry, as: :entryable end