Skip to content

Commit

Permalink
Revert changes to delegated_type in rails#50280 (rails#53016)
Browse files Browse the repository at this point in the history
Automatically inferring `:inverse_of` is incompatible with records that
do not declare inverse associations.

Revert for the sake of the impending release.
  • Loading branch information
seanpdoyle authored Sep 23, 2024
1 parent a5d1e10 commit fd975a8
Show file tree
Hide file tree
Showing 11 changed files with 11 additions and 79 deletions.
11 changes: 0 additions & 11 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 0 additions & 10 deletions activerecord/lib/active_record/delegated_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 <tt>:foreign_key</tt> option is
# also provided. For example, a call to
# <tt>Entry.delegated_type</tt> will default to <tt>inverse_of: :entry</tt>.
# See ActiveRecord::Associations::ClassMethods's overview on Bi-directional
# associations for more detail.
#
# Option examples:
# class Entry < ApplicationRecord
Expand All @@ -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
Expand Down
24 changes: 7 additions & 17 deletions activerecord/test/cases/delegated_type_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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!")
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions activerecord/test/models/comment.rb
Original file line number Diff line number Diff line change
@@ -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%'") }
Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/models/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 0 additions & 11 deletions activerecord/test/models/entryable.rb

This file was deleted.

4 changes: 0 additions & 4 deletions activerecord/test/models/essay.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 1 addition & 4 deletions activerecord/test/models/message.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 1 addition & 3 deletions activerecord/test/models/uuid_comment.rb
Original file line number Diff line number Diff line change
@@ -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
11 changes: 0 additions & 11 deletions activerecord/test/models/uuid_entryable.rb

This file was deleted.

4 changes: 1 addition & 3 deletions activerecord/test/models/uuid_message.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit fd975a8

Please sign in to comment.