Skip to content

Commit fd975a8

Browse files
authored
Revert changes to delegated_type in rails#50280 (rails#53016)
Automatically inferring `:inverse_of` is incompatible with records that do not declare inverse associations. Revert for the sake of the impending release.
1 parent a5d1e10 commit fd975a8

File tree

11 files changed

+11
-79
lines changed

11 files changed

+11
-79
lines changed

activerecord/CHANGELOG.md

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,6 @@
105105

106106
*Justin Talbott*
107107

108-
* Infer default `:inverse_of` option for `delegated_type` definitions.
109-
110-
```ruby
111-
class Entry < ApplicationRecord
112-
delegated_type :entryable, types: %w[ Message ]
113-
# => defaults to inverse_of: :entry
114-
end
115-
```
116-
117-
*Sean Doyle*
118-
119108
* Add support for `ActiveRecord::Point` type casts using `Hash` values
120109

121110
This allows `ActiveRecord::Point` to be cast or serialized from a hash

activerecord/lib/active_record/delegated_type.rb

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -220,14 +220,6 @@ module DelegatedType
220220
# [:primary_key]
221221
# Specify the method that returns the primary key of associated object used for the convenience methods.
222222
# By default this is +id+.
223-
# [+:inverse_of+]
224-
# Specifies the name of the #belongs_to association on the associated object
225-
# that is the inverse of this #has_one association. By default, the class
226-
# singularized class name is used unless a <tt>:foreign_key</tt> option is
227-
# also provided. For example, a call to
228-
# <tt>Entry.delegated_type</tt> will default to <tt>inverse_of: :entry</tt>.
229-
# See ActiveRecord::Associations::ClassMethods's overview on Bi-directional
230-
# associations for more detail.
231223
#
232224
# Option examples:
233225
# class Entry < ApplicationRecord
@@ -237,8 +229,6 @@ module DelegatedType
237229
# Entry#message_uuid # => returns entryable_uuid, when entryable_type == "Message", otherwise nil
238230
# Entry#comment_uuid # => returns entryable_uuid, when entryable_type == "Comment", otherwise nil
239231
def delegated_type(role, types:, **options)
240-
options[:inverse_of] = model_name.singular unless options.key?(:inverse_of) || options.key?(:foreign_key)
241-
242232
belongs_to role, options.delete(:scope), **options.merge(polymorphic: true)
243233
define_delegated_type_methods role, types: types, options: options
244234
end

activerecord/test/cases/delegated_type_test.rb

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
require "cases/helper"
44
require "models/account"
55
require "models/entry"
6-
require "models/essay"
76
require "models/message"
87
require "models/recipient"
98
require "models/comment"
@@ -12,12 +11,12 @@
1211
require "models/uuid_comment"
1312

1413
class DelegatedTypeTest < ActiveRecord::TestCase
15-
fixtures :comments, :accounts, :essays
14+
fixtures :comments, :accounts, :posts
1615

1716
setup do
1817
@entry_with_message = Entry.create! entryable: Message.new(subject: "Hello world!"), account: accounts(:signals37)
1918
@entry_with_comment = Entry.create! entryable: comments(:greetings), account: accounts(:signals37)
20-
@entry_with_essay = Entry.create! thing: essays(:mary_stay_home), account: accounts(:signals37)
19+
@entry_with_post = Entry.create! thing: posts(:welcome), account: accounts(:signals37)
2120

2221
if current_adapter?(:PostgreSQLAdapter)
2322
@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
3736
test "delegated class with custom foreign_type" do
3837
assert_equal Message, @entry_with_message.thing_class
3938
assert_equal Comment, @entry_with_comment.thing_class
40-
assert_equal Essay, @entry_with_essay.thing_class
39+
assert_equal Post, @entry_with_post.thing_class
4140
end
4241

4342
test "delegated type name" do
@@ -57,9 +56,9 @@ class DelegatedTypeTest < ActiveRecord::TestCase
5756
end
5857

5958
test "delegated type predicates with custom foreign_type" do
60-
assert_predicate @entry_with_essay, :essay?
61-
assert_not @entry_with_message.essay?
62-
assert_not @entry_with_comment.essay?
59+
assert_predicate @entry_with_post, :post?
60+
assert_not @entry_with_message.post?
61+
assert_not @entry_with_comment.post?
6362
end
6463

6564
test "scope" do
@@ -68,7 +67,7 @@ class DelegatedTypeTest < ActiveRecord::TestCase
6867
end
6968

7069
test "scope with custom foreign_type" do
71-
assert_predicate Entry.essays.first, :essay?
70+
assert_predicate Entry.posts.first, :post?
7271
end
7372

7473
test "accessor" do
@@ -97,15 +96,6 @@ class DelegatedTypeTest < ActiveRecord::TestCase
9796
assert_nil @uuid_entry_with_comment.uuid_message_uuid
9897
end
9998

100-
test "association inverse_of" do
101-
created = @entry_with_message.message
102-
updated = @entry_with_message.build_entryable subject: "Goodbye world!"
103-
104-
assert_changes -> { @entry_with_message.reload.message }, from: created, to: updated do
105-
updated.save!
106-
end
107-
end
108-
10999
test "touch account" do
110100
previous_account_updated_at = @entry_with_message.account.updated_at
111101
previous_entry_updated_at = @entry_with_message.updated_at

activerecord/test/models/comment.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
# frozen_string_literal: true
22

3-
require "models/entryable"
4-
53
# `counter_cache` requires association class before `attr_readonly`.
64
class Post < ActiveRecord::Base; end
75

86
class Comment < ActiveRecord::Base
9-
include Entryable
10-
117
scope :limit_by, lambda { |l| limit(l) }
128
scope :containing_the_letter_e, -> { where("comments.body LIKE '%e%'") }
139
scope :not_again, -> { where("comments.body NOT LIKE '%again%'") }

activerecord/test/models/entry.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ class Entry < ActiveRecord::Base
55
belongs_to :account, touch: true
66

77
# alternate delegation for custom foreign_key/foreign_type
8-
delegated_type :thing, types: %w[ Essay ],
8+
delegated_type :thing, types: %w[ Post ],
99
foreign_key: :entryable_id, foreign_type: :entryable_type
1010
end

activerecord/test/models/entryable.rb

Lines changed: 0 additions & 11 deletions
This file was deleted.

activerecord/test/models/essay.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
# frozen_string_literal: true
22

3-
require "models/entryable"
4-
53
class Essay < ActiveRecord::Base
6-
include Entryable
7-
84
belongs_to :author, primary_key: :name
95
belongs_to :writer, primary_key: :name, polymorphic: true
106
belongs_to :category, primary_key: :name

activerecord/test/models/message.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
# frozen_string_literal: true
22

3-
require "models/entryable"
4-
53
class Message < ActiveRecord::Base
6-
include Entryable
7-
4+
has_one :entry, as: :entryable, touch: true
85
has_many :recipients
96
end
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# frozen_string_literal: true
22

3-
require "models/uuid_entryable"
4-
53
class UuidComment < ActiveRecord::Base
6-
include UuidEntryable
4+
has_one :uuid_entry, as: :entryable
75
end

activerecord/test/models/uuid_entryable.rb

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)