Skip to content

Commit 1119c89

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 (cherry picked from commit ac2b7a5) Conflicts: activerecord/CHANGELOG.md activerecord/lib/active_record/associations/builder/association.rb
1 parent 166e92a commit 1119c89

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
* Fixed a bug where uniqueness validations would error on out of range values,
28
even if an validation should have prevented it from hitting the database.
39

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class << self
2121
end
2222
self.extensions = []
2323

24-
self.valid_options = [:class_name, :class, :foreign_key, :validate]
24+
self.valid_options = [:class_name, :anonymous_class, :foreign_key, :validate]
2525

2626
attr_reader :name, :scope, :options
2727

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
@@ -632,7 +632,7 @@ class ThroughReflection < AbstractReflection #:nodoc:
632632

633633
def initialize(delegate_reflection)
634634
@delegate_reflection = delegate_reflection
635-
@klass = delegate_reflection.options[:class]
635+
@klass = delegate_reflection.options[:anonymous_class]
636636
@source_reflection_name = delegate_reflection.options[:source]
637637
end
638638

activerecord/test/cases/associations/belongs_to_associations_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ def test_default_scope_on_relations_is_not_cached
7474
where("id = :inc", :inc => counter)
7575
}
7676

77-
has_many :comments, :class => comments
77+
has_many :comments, :anonymous_class => comments
7878
}
79-
belongs_to :post, :class => posts, :inverse_of => false
79+
belongs_to :post, :anonymous_class => posts, :inverse_of => false
8080
}
8181

8282
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
@@ -118,9 +118,9 @@ def test_anonymous_has_many
118118

119119
developer_project = Class.new(ActiveRecord::Base) {
120120
self.table_name = 'developers_projects'
121-
belongs_to :developer, :class => dev
121+
belongs_to :developer, :anonymous_class => dev
122122
}
123-
has_many :developer_projects, :class => developer_project, :foreign_key => 'developer_id'
123+
has_many :developer_projects, :anonymous_class => developer_project, :foreign_key => 'developer_id'
124124
}
125125
dev = developer.first
126126
named = Developer.find(dev.id)
@@ -139,13 +139,13 @@ def test_default_scope_on_relations_is_not_cached
139139
comments = Class.new(ActiveRecord::Base) {
140140
self.table_name = 'comments'
141141
self.inheritance_column = 'not_there'
142-
belongs_to :post, :class => post
142+
belongs_to :post, :anonymous_class => post
143143
default_scope -> {
144144
counter += 1
145145
where("id = :inc", :inc => counter)
146146
}
147147
}
148-
has_many :comments, :class => comments, :foreign_key => 'post_id'
148+
has_many :comments, :anonymous_class => comments, :foreign_key => 'post_id'
149149
}
150150
assert_equal 0, counter
151151
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)