Skip to content

Commit fae23a3

Browse files
authored
Merge pull request rails#51932 from Shopify/relation-model
Unify `Relation#klass` and `Relation#model`
2 parents 630fc98 + 9f901b7 commit fae23a3

20 files changed

+146
-145
lines changed

activerecord/lib/active_record/associations/collection_association.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def reset
9494
def find(*args)
9595
if options[:inverse_of] && loaded?
9696
args_flatten = args.flatten
97-
model = scope.klass
97+
model = scope.model
9898

9999
if args_flatten.blank?
100100
error_message = "Couldn't find #{model.name} without an ID"

activerecord/lib/active_record/associations/disable_joins_association_scope.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def add_constraints(reflection, key, join_ids, owner, ordered)
4747
end
4848

4949
if scope.order_values.empty? && ordered
50-
split_scope = DisableJoinsAssociationRelation.create(scope.klass, key, join_ids)
50+
split_scope = DisableJoinsAssociationRelation.create(scope.model, key, join_ids)
5151
split_scope.where_clause += scope.where_clause
5252
split_scope
5353
else

activerecord/lib/active_record/associations/has_many_through_association.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def delete_records(records, method)
140140

141141
case method
142142
when :destroy
143-
if scope.klass.primary_key
143+
if scope.model.primary_key
144144
count = scope.destroy_all.count(&:destroyed?)
145145
else
146146
scope.each(&:_run_destroy_callbacks)

activerecord/lib/active_record/encryption/extended_deterministic_queries.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,12 @@ def exists?(*args)
104104
end
105105

106106
def scope_for_create
107-
return super unless klass.deterministic_encrypted_attributes&.any?
107+
return super unless model.deterministic_encrypted_attributes&.any?
108108

109109
scope_attributes = super
110110
wheres = where_values_hash
111111

112-
klass.deterministic_encrypted_attributes.each do |attribute_name|
112+
model.deterministic_encrypted_attributes.each do |attribute_name|
113113
attribute_name = attribute_name.to_s
114114
values = wheres[attribute_name]
115115
if values.is_a?(Array) && values[1..].all?(AdditionalValue)

activerecord/lib/active_record/relation.rb

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,19 @@ def exec_explain(&block)
6868
include FinderMethods, Calculations, SpawnMethods, QueryMethods, Batches, Explain, Delegation
6969
include SignedId::RelationMethods, TokenFor::RelationMethods
7070

71-
attr_reader :table, :klass, :loaded, :predicate_builder
71+
attr_reader :table, :model, :loaded, :predicate_builder
7272
attr_accessor :skip_preloading_value
73-
alias :model :klass
73+
alias :klass :model
7474
alias :loaded? :loaded
7575
alias :locked? :lock_value
7676

77-
def initialize(klass, table: klass.arel_table, predicate_builder: klass.predicate_builder, values: {})
78-
@klass = klass
77+
def initialize(model, table: model.arel_table, predicate_builder: model.predicate_builder, values: {})
78+
@model = model
7979
@table = table
8080
@values = values
8181
@loaded = false
8282
@predicate_builder = predicate_builder
83-
@delegate_to_klass = false
83+
@delegate_to_model = false
8484
@future_result = nil
8585
@records = nil
8686
@async = false
@@ -93,7 +93,7 @@ def initialize_copy(other)
9393
end
9494

9595
def bind_attribute(name, value) # :nodoc:
96-
if reflection = klass._reflect_on_association(name)
96+
if reflection = model._reflect_on_association(name)
9797
name = reflection.foreign_key
9898
value = value.read_attribute(reflection.association_primary_key) unless value.nil?
9999
end
@@ -430,12 +430,12 @@ def many?
430430
# Product.where("name like ?", "%Game%").cache_key(:last_reviewed_at)
431431
def cache_key(timestamp_column = "updated_at")
432432
@cache_keys ||= {}
433-
@cache_keys[timestamp_column] ||= klass.collection_cache_key(self, timestamp_column)
433+
@cache_keys[timestamp_column] ||= model.collection_cache_key(self, timestamp_column)
434434
end
435435

436436
def compute_cache_key(timestamp_column = :updated_at) # :nodoc:
437437
query_signature = ActiveSupport::Digest.hexdigest(to_sql)
438-
key = "#{klass.model_name.cache_key}/query-#{query_signature}"
438+
key = "#{model.model_name.cache_key}/query-#{query_signature}"
439439

440440
if model.collection_cache_versioning
441441
key
@@ -475,7 +475,7 @@ def compute_cache_version(timestamp_column) # :nodoc:
475475

476476
with_connection do |c|
477477
column = c.visitor.compile(table[timestamp_column])
478-
select_values = "COUNT(*) AS #{klass.adapter_class.quote_column_name("size")}, MAX(%s) AS timestamp"
478+
select_values = "COUNT(*) AS #{model.adapter_class.quote_column_name("size")}, MAX(%s) AS timestamp"
479479

480480
if collection.has_limit_or_offset?
481481
query = collection.select("#{column} AS collection_cache_key_timestamp")
@@ -492,7 +492,7 @@ def compute_cache_version(timestamp_column) # :nodoc:
492492
size, timestamp = c.select_rows(arel, nil).first
493493

494494
if size
495-
column_type = klass.type_for_attribute(timestamp_column)
495+
column_type = model.type_for_attribute(timestamp_column)
496496
timestamp = column_type.deserialize(timestamp)
497497
else
498498
size = 0
@@ -532,7 +532,7 @@ def cache_key_with_version
532532
# Please check unscoped if you want to remove all previous scopes (including
533533
# the default_scope) during the execution of a block.
534534
def scoping(all_queries: nil, &block)
535-
registry = klass.scope_registry
535+
registry = model.scope_registry
536536
if global_scope?(registry) && all_queries == false
537537
raise ArgumentError, "Scoping is set to apply to all queries and cannot be unset in a nested block."
538538
elsif already_in_scope?(registry)
@@ -543,11 +543,11 @@ def scoping(all_queries: nil, &block)
543543
end
544544

545545
def _exec_scope(...) # :nodoc:
546-
@delegate_to_klass = true
547-
registry = klass.scope_registry
546+
@delegate_to_model = true
547+
registry = model.scope_registry
548548
_scoping(nil, registry) { instance_exec(...) || self }
549549
ensure
550-
@delegate_to_klass = false
550+
@delegate_to_model = false
551551
end
552552

553553
# Updates all records in the current relation with details given. This method constructs a single SQL UPDATE
@@ -584,46 +584,46 @@ def update_all(updates)
584584
return 0 if @none
585585

586586
if updates.is_a?(Hash)
587-
if klass.locking_enabled? &&
588-
!updates.key?(klass.locking_column) &&
589-
!updates.key?(klass.locking_column.to_sym)
590-
attr = table[klass.locking_column]
587+
if model.locking_enabled? &&
588+
!updates.key?(model.locking_column) &&
589+
!updates.key?(model.locking_column.to_sym)
590+
attr = table[model.locking_column]
591591
updates[attr.name] = _increment_attribute(attr)
592592
end
593593
values = _substitute_values(updates)
594594
else
595-
values = Arel.sql(klass.sanitize_sql_for_assignment(updates, table.name))
595+
values = Arel.sql(model.sanitize_sql_for_assignment(updates, table.name))
596596
end
597597

598-
klass.with_connection do |c|
598+
model.with_connection do |c|
599599
arel = eager_loading? ? apply_join_dependency.arel : build_arel(c)
600600
arel.source.left = table
601601

602602
group_values_arel_columns = arel_columns(group_values.uniq)
603603
having_clause_ast = having_clause.ast unless having_clause.empty?
604-
key = if klass.composite_primary_key?
604+
key = if model.composite_primary_key?
605605
primary_key.map { |pk| table[pk] }
606606
else
607607
table[primary_key]
608608
end
609609
stmt = arel.compile_update(values, key, having_clause_ast, group_values_arel_columns)
610-
c.update(stmt, "#{klass} Update All").tap { reset }
610+
c.update(stmt, "#{model} Update All").tap { reset }
611611
end
612612
end
613613

614614
def update(id = :all, attributes) # :nodoc:
615615
if id == :all
616616
each { |record| record.update(attributes) }
617617
else
618-
klass.update(id, attributes)
618+
model.update(id, attributes)
619619
end
620620
end
621621

622622
def update!(id = :all, attributes) # :nodoc:
623623
if id == :all
624624
each { |record| record.update!(attributes) }
625625
else
626-
klass.update!(id, attributes)
626+
model.update!(id, attributes)
627627
end
628628
end
629629

@@ -929,7 +929,7 @@ def update_counters(counters)
929929
names = touch if touch != true
930930
names = Array.wrap(names)
931931
options = names.extract_options!
932-
touch_updates = klass.touch_attributes_with_time(*names, **options)
932+
touch_updates = model.touch_attributes_with_time(*names, **options)
933933
updates.merge!(touch_updates) unless touch_updates.empty?
934934
end
935935

@@ -960,7 +960,7 @@ def update_counters(counters)
960960
# Person.where(name: 'David').touch_all
961961
# # => "UPDATE \"people\" SET \"updated_at\" = '2018-01-04 22:55:23.132670' WHERE \"people\".\"name\" = 'David'"
962962
def touch_all(*names, time: nil)
963-
update_all klass.touch_attributes_with_time(*names, time: time)
963+
update_all model.touch_attributes_with_time(*names, time: time)
964964
end
965965

966966
# Destroys the records by instantiating each
@@ -1012,20 +1012,20 @@ def delete_all
10121012
raise ActiveRecordError.new("delete_all doesn't support #{invalid_methods.join(', ')}")
10131013
end
10141014

1015-
klass.with_connection do |c|
1015+
model.with_connection do |c|
10161016
arel = eager_loading? ? apply_join_dependency.arel : build_arel(c)
10171017
arel.source.left = table
10181018

10191019
group_values_arel_columns = arel_columns(group_values.uniq)
10201020
having_clause_ast = having_clause.ast unless having_clause.empty?
1021-
key = if klass.composite_primary_key?
1021+
key = if model.composite_primary_key?
10221022
primary_key.map { |pk| table[pk] }
10231023
else
10241024
table[primary_key]
10251025
end
10261026
stmt = arel.compile_delete(key, having_clause_ast, group_values_arel_columns)
10271027

1028-
c.delete(stmt, "#{klass} Delete All").tap { reset }
1028+
c.delete(stmt, "#{model} Delete All").tap { reset }
10291029
end
10301030
end
10311031

@@ -1180,7 +1180,7 @@ def reload
11801180
def reset
11811181
@future_result&.cancel
11821182
@future_result = nil
1183-
@delegate_to_klass = false
1183+
@delegate_to_model = false
11841184
@to_sql = @arel = @loaded = @should_eager_load = nil
11851185
@offsets = @take = nil
11861186
@cache_keys = nil
@@ -1200,7 +1200,7 @@ def to_sql
12001200
relation.to_sql
12011201
end
12021202
else
1203-
klass.with_connection do |conn|
1203+
model.with_connection do |conn|
12041204
conn.unprepared_statement { conn.to_sql(arel) }
12051205
end
12061206
end
@@ -1210,12 +1210,12 @@ def to_sql
12101210
#
12111211
# User.where(name: 'Oscar').where_values_hash
12121212
# # => {name: "Oscar"}
1213-
def where_values_hash(relation_table_name = klass.table_name) # :nodoc:
1213+
def where_values_hash(relation_table_name = model.table_name) # :nodoc:
12141214
where_clause.to_h(relation_table_name)
12151215
end
12161216

12171217
def scope_for_create
1218-
hash = where_clause.to_h(klass.table_name, equality_only: true)
1218+
hash = where_clause.to_h(model.table_name, equality_only: true)
12191219
create_with_value.each { |k, v| hash[k.to_s] = v } unless create_with_value.empty?
12201220
hash
12211221
end
@@ -1283,15 +1283,15 @@ def inspect
12831283
end
12841284

12851285
def empty_scope? # :nodoc:
1286-
@values == klass.unscoped.values
1286+
@values == model.unscoped.values
12871287
end
12881288

12891289
def has_limit_or_offset? # :nodoc:
12901290
limit_value || offset_value
12911291
end
12921292

12931293
def alias_tracker(joins = [], aliases = nil) # :nodoc:
1294-
ActiveRecord::Associations::AliasTracker.create(klass.connection_pool, table.name, joins, aliases)
1294+
ActiveRecord::Associations::AliasTracker.create(model.connection_pool, table.name, joins, aliases)
12951295
end
12961296

12971297
class StrictLoadingScope # :nodoc:
@@ -1321,46 +1321,46 @@ def load_records(records)
13211321

13221322
private
13231323
def already_in_scope?(registry)
1324-
@delegate_to_klass && registry.current_scope(klass, true)
1324+
@delegate_to_model && registry.current_scope(model, true)
13251325
end
13261326

13271327
def global_scope?(registry)
1328-
registry.global_current_scope(klass, true)
1328+
registry.global_current_scope(model, true)
13291329
end
13301330

13311331
def current_scope_restoring_block(&block)
1332-
current_scope = klass.current_scope(true)
1332+
current_scope = model.current_scope(true)
13331333
-> record do
1334-
klass.current_scope = current_scope
1334+
model.current_scope = current_scope
13351335
yield record if block_given?
13361336
end
13371337
end
13381338

13391339
def _new(attributes, &block)
1340-
klass.new(attributes, &block)
1340+
model.new(attributes, &block)
13411341
end
13421342

13431343
def _create(attributes, &block)
1344-
klass.create(attributes, &block)
1344+
model.create(attributes, &block)
13451345
end
13461346

13471347
def _create!(attributes, &block)
1348-
klass.create!(attributes, &block)
1348+
model.create!(attributes, &block)
13491349
end
13501350

13511351
def _scoping(scope, registry, all_queries = false)
1352-
previous = registry.current_scope(klass, true)
1353-
registry.set_current_scope(klass, scope)
1352+
previous = registry.current_scope(model, true)
1353+
registry.set_current_scope(model, scope)
13541354

13551355
if all_queries
1356-
previous_global = registry.global_current_scope(klass, true)
1357-
registry.set_global_current_scope(klass, scope)
1356+
previous_global = registry.global_current_scope(model, true)
1357+
registry.set_global_current_scope(model, scope)
13581358
end
13591359
yield
13601360
ensure
1361-
registry.set_current_scope(klass, previous)
1361+
registry.set_current_scope(model, previous)
13621362
if all_queries
1363-
registry.set_global_current_scope(klass, previous_global)
1363+
registry.set_global_current_scope(model, previous_global)
13641364
end
13651365
end
13661366

@@ -1372,7 +1372,7 @@ def _substitute_values(values)
13721372
value = Arel::Nodes::Grouping.new(value)
13731373
end
13741374
else
1375-
type = klass.type_for_attribute(attr.name)
1375+
type = model.type_for_attribute(attr.name)
13761376
value = predicate_builder.build_bind_attribute(attr.name, type.cast(value))
13771377
end
13781378
[attr, value]
@@ -1419,7 +1419,7 @@ def exec_main_query(async: false)
14191419
if where_clause.contradiction?
14201420
[].freeze
14211421
elsif eager_loading?
1422-
klass.with_connection do |c|
1422+
model.with_connection do |c|
14231423
apply_join_dependency do |relation, join_dependency|
14241424
if relation.null_relation?
14251425
[].freeze
@@ -1431,8 +1431,8 @@ def exec_main_query(async: false)
14311431
end
14321432
end
14331433
else
1434-
klass.with_connection do |c|
1435-
klass._query_by_sql(c, arel, async: async)
1434+
model.with_connection do |c|
1435+
model._query_by_sql(c, arel, async: async)
14361436
end
14371437
end
14381438
end
@@ -1445,7 +1445,7 @@ def instantiate_records(rows, &block)
14451445
@_join_dependency = nil
14461446
records
14471447
else
1448-
klass._load_from_sql(rows, &block).freeze
1448+
model._load_from_sql(rows, &block).freeze
14491449
end
14501450
end
14511451

activerecord/lib/active_record/relation/batches.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ def batch_on_unloaded_relation(relation:, start:, finish:, load:, order:, use_ra
367367
relation = apply_limits(relation, start, finish, batch_orders)
368368
relation.skip_query_cache! # Retaining the results in the query cache would undermine the point of batching
369369
batch_relation = relation
370-
empty_scope = to_sql == klass.unscoped.all.to_sql
370+
empty_scope = to_sql == model.unscoped.all.to_sql
371371

372372
loop do
373373
if load

0 commit comments

Comments
 (0)