Skip to content

Commit ac2b7a5

Browse files
committed
Rename association option :class to :anonymous_class
In 1f006c an option was added called :class to allow passing anonymous classes to association definitions. Since using :class instead of :class_name is a fairly common typo even amongst experienced developers this can result in hard to debug errors arising in raise_on_type_mismatch? To fix this we're renaming the option from :class to :anonymous_class as that is a more correct description of what the option is for. Since this was an internal, undocumented option there is no need for a deprecation. Fixes rails#19659
1 parent b12abe6 commit ac2b7a5

File tree

9 files changed

+31
-25
lines changed

9 files changed

+31
-25
lines changed

activerecord/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
* Rename `:class` to `:anonymous_class` in association options.
2+
3+
Fixes #19659.
4+
5+
*Andrew White*
6+
17
* Autosave existing records on a has many through association when the parent
28
is new.
39

activerecord/lib/active_record/associations/builder/association.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class << self
1616
end
1717
self.extensions = []
1818

19-
VALID_OPTIONS = [:class_name, :class, :foreign_key, :validate] # :nodoc:
19+
VALID_OPTIONS = [:class_name, :anonymous_class, :foreign_key, :validate] # :nodoc:
2020

2121
def self.build(model, name, scope, options, &block)
2222
if model.dangerous_attribute_method?(name)

activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def self.add_right_association(name, options)
7878
join_model.table_name_resolver = habtm
7979
join_model.class_resolver = lhs_model
8080

81-
join_model.add_left_association :left_side, class: lhs_model
81+
join_model.add_left_association :left_side, anonymous_class: lhs_model
8282
join_model.add_right_association association_name, belongs_to_options(options)
8383
join_model
8484
end

activerecord/lib/active_record/reflection.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def initialize(name, scope, options, active_record)
196196
@scope = scope
197197
@options = options
198198
@active_record = active_record
199-
@klass = options[:class]
199+
@klass = options[:anonymous_class]
200200
@plural_name = active_record.pluralize_table_names ?
201201
name.to_s.pluralize : name.to_s
202202
end
@@ -635,7 +635,7 @@ class ThroughReflection < AbstractReflection #:nodoc:
635635

636636
def initialize(delegate_reflection)
637637
@delegate_reflection = delegate_reflection
638-
@klass = delegate_reflection.options[:class]
638+
@klass = delegate_reflection.options[:anonymous_class]
639639
@source_reflection_name = delegate_reflection.options[:source]
640640
end
641641

activerecord/test/cases/associations/belongs_to_associations_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,9 @@ def test_default_scope_on_relations_is_not_cached
124124
where("id = :inc", :inc => counter)
125125
}
126126

127-
has_many :comments, :class => comments
127+
has_many :comments, :anonymous_class => comments
128128
}
129-
belongs_to :post, :class => posts, :inverse_of => false
129+
belongs_to :post, :anonymous_class => posts, :inverse_of => false
130130
}
131131

132132
assert_equal 0, counter

activerecord/test/cases/associations/has_many_associations_test.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,9 @@ def test_anonymous_has_many
119119

120120
developer_project = Class.new(ActiveRecord::Base) {
121121
self.table_name = 'developers_projects'
122-
belongs_to :developer, :class => dev
122+
belongs_to :developer, :anonymous_class => dev
123123
}
124-
has_many :developer_projects, :class => developer_project, :foreign_key => 'developer_id'
124+
has_many :developer_projects, :anonymous_class => developer_project, :foreign_key => 'developer_id'
125125
}
126126
dev = developer.first
127127
named = Developer.find(dev.id)
@@ -140,13 +140,13 @@ def test_default_scope_on_relations_is_not_cached
140140
comments = Class.new(ActiveRecord::Base) {
141141
self.table_name = 'comments'
142142
self.inheritance_column = 'not_there'
143-
belongs_to :post, :class => post
143+
belongs_to :post, :anonymous_class => post
144144
default_scope -> {
145145
counter += 1
146146
where("id = :inc", :inc => counter)
147147
}
148148
}
149-
has_many :comments, :class => comments, :foreign_key => 'post_id'
149+
has_many :comments, :anonymous_class => comments, :foreign_key => 'post_id'
150150
}
151151
assert_equal 0, counter
152152
post = posts.first

activerecord/test/cases/associations/has_many_through_associations_test.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ def test_singleton_has_many_through
8484
subscriber = make_model "Subscriber"
8585

8686
subscriber.primary_key = 'nick'
87-
subscription.belongs_to :book, class: book
88-
subscription.belongs_to :subscriber, class: subscriber
87+
subscription.belongs_to :book, anonymous_class: book
88+
subscription.belongs_to :subscriber, anonymous_class: subscriber
8989

90-
book.has_many :subscriptions, class: subscription
91-
book.has_many :subscribers, through: :subscriptions, class: subscriber
90+
book.has_many :subscriptions, anonymous_class: subscription
91+
book.has_many :subscribers, through: :subscriptions, anonymous_class: subscriber
9292

9393
anonbook = book.first
9494
namebook = Book.find anonbook.id
@@ -154,10 +154,10 @@ def make_no_pk_hm_t
154154
lesson_student = make_model 'LessonStudent'
155155
lesson_student.table_name = 'lessons_students'
156156

157-
lesson_student.belongs_to :lesson, :class => lesson
158-
lesson_student.belongs_to :student, :class => student
159-
lesson.has_many :lesson_students, :class => lesson_student
160-
lesson.has_many :students, :through => :lesson_students, :class => student
157+
lesson_student.belongs_to :lesson, :anonymous_class => lesson
158+
lesson_student.belongs_to :student, :anonymous_class => student
159+
lesson.has_many :lesson_students, :anonymous_class => lesson_student
160+
lesson.has_many :students, :through => :lesson_students, :anonymous_class => student
161161
[lesson, lesson_student, student]
162162
end
163163

activerecord/test/cases/autosave_association_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def should_be_cool
4343
reference = Class.new(ActiveRecord::Base) {
4444
self.table_name = "references"
4545
def self.name; 'Reference'; end
46-
belongs_to :person, autosave: true, class: person
46+
belongs_to :person, autosave: true, anonymous_class: person
4747
}
4848

4949
u = person.create!(first_name: 'cool')

activerecord/test/cases/fixtures_test.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -279,10 +279,10 @@ def test_has_many_through_with_default_table_name
279279
treasure = make_model "Treasure"
280280

281281
pt.table_name = "parrots_treasures"
282-
pt.belongs_to :parrot, :class => parrot
283-
pt.belongs_to :treasure, :class => treasure
282+
pt.belongs_to :parrot, :anonymous_class => parrot
283+
pt.belongs_to :treasure, :anonymous_class => treasure
284284

285-
parrot.has_many :parrot_treasures, :class => pt
285+
parrot.has_many :parrot_treasures, :anonymous_class => pt
286286
parrot.has_many :treasures, :through => :parrot_treasures
287287

288288
parrots = File.join FIXTURES_ROOT, 'parrots'
@@ -297,10 +297,10 @@ def test_has_many_through_with_renamed_table
297297
parrot = make_model "Parrot"
298298
treasure = make_model "Treasure"
299299

300-
pt.belongs_to :parrot, :class => parrot
301-
pt.belongs_to :treasure, :class => treasure
300+
pt.belongs_to :parrot, :anonymous_class => parrot
301+
pt.belongs_to :treasure, :anonymous_class => treasure
302302

303-
parrot.has_many :parrot_treasures, :class => pt
303+
parrot.has_many :parrot_treasures, :anonymous_class => pt
304304
parrot.has_many :treasures, :through => :parrot_treasures
305305

306306
parrots = File.join FIXTURES_ROOT, 'parrots'

0 commit comments

Comments
 (0)