@@ -68,19 +68,19 @@ def exec_explain(&block)
68
68
include FinderMethods , Calculations , SpawnMethods , QueryMethods , Batches , Explain , Delegation
69
69
include SignedId ::RelationMethods , TokenFor ::RelationMethods
70
70
71
- attr_reader :table , :klass , :loaded , :predicate_builder
71
+ attr_reader :table , :model , :loaded , :predicate_builder
72
72
attr_accessor :skip_preloading_value
73
- alias :model :klass
73
+ alias :klass :model
74
74
alias :loaded? :loaded
75
75
alias :locked? :lock_value
76
76
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
79
79
@table = table
80
80
@values = values
81
81
@loaded = false
82
82
@predicate_builder = predicate_builder
83
- @delegate_to_klass = false
83
+ @delegate_to_model = false
84
84
@future_result = nil
85
85
@records = nil
86
86
@async = false
@@ -93,7 +93,7 @@ def initialize_copy(other)
93
93
end
94
94
95
95
def bind_attribute ( name , value ) # :nodoc:
96
- if reflection = klass . _reflect_on_association ( name )
96
+ if reflection = model . _reflect_on_association ( name )
97
97
name = reflection . foreign_key
98
98
value = value . read_attribute ( reflection . association_primary_key ) unless value . nil?
99
99
end
@@ -430,12 +430,12 @@ def many?
430
430
# Product.where("name like ?", "%Game%").cache_key(:last_reviewed_at)
431
431
def cache_key ( timestamp_column = "updated_at" )
432
432
@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 )
434
434
end
435
435
436
436
def compute_cache_key ( timestamp_column = :updated_at ) # :nodoc:
437
437
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 } "
439
439
440
440
if model . collection_cache_versioning
441
441
key
@@ -475,7 +475,7 @@ def compute_cache_version(timestamp_column) # :nodoc:
475
475
476
476
with_connection do |c |
477
477
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"
479
479
480
480
if collection . has_limit_or_offset?
481
481
query = collection . select ( "#{ column } AS collection_cache_key_timestamp" )
@@ -492,7 +492,7 @@ def compute_cache_version(timestamp_column) # :nodoc:
492
492
size , timestamp = c . select_rows ( arel , nil ) . first
493
493
494
494
if size
495
- column_type = klass . type_for_attribute ( timestamp_column )
495
+ column_type = model . type_for_attribute ( timestamp_column )
496
496
timestamp = column_type . deserialize ( timestamp )
497
497
else
498
498
size = 0
@@ -532,7 +532,7 @@ def cache_key_with_version
532
532
# Please check unscoped if you want to remove all previous scopes (including
533
533
# the default_scope) during the execution of a block.
534
534
def scoping ( all_queries : nil , &block )
535
- registry = klass . scope_registry
535
+ registry = model . scope_registry
536
536
if global_scope? ( registry ) && all_queries == false
537
537
raise ArgumentError , "Scoping is set to apply to all queries and cannot be unset in a nested block."
538
538
elsif already_in_scope? ( registry )
@@ -543,11 +543,11 @@ def scoping(all_queries: nil, &block)
543
543
end
544
544
545
545
def _exec_scope ( ...) # :nodoc:
546
- @delegate_to_klass = true
547
- registry = klass . scope_registry
546
+ @delegate_to_model = true
547
+ registry = model . scope_registry
548
548
_scoping ( nil , registry ) { instance_exec ( ...) || self }
549
549
ensure
550
- @delegate_to_klass = false
550
+ @delegate_to_model = false
551
551
end
552
552
553
553
# 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)
584
584
return 0 if @none
585
585
586
586
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 ]
591
591
updates [ attr . name ] = _increment_attribute ( attr )
592
592
end
593
593
values = _substitute_values ( updates )
594
594
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 ) )
596
596
end
597
597
598
- klass . with_connection do |c |
598
+ model . with_connection do |c |
599
599
arel = eager_loading? ? apply_join_dependency . arel : build_arel ( c )
600
600
arel . source . left = table
601
601
602
602
group_values_arel_columns = arel_columns ( group_values . uniq )
603
603
having_clause_ast = having_clause . ast unless having_clause . empty?
604
- key = if klass . composite_primary_key?
604
+ key = if model . composite_primary_key?
605
605
primary_key . map { |pk | table [ pk ] }
606
606
else
607
607
table [ primary_key ]
608
608
end
609
609
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 }
611
611
end
612
612
end
613
613
614
614
def update ( id = :all , attributes ) # :nodoc:
615
615
if id == :all
616
616
each { |record | record . update ( attributes ) }
617
617
else
618
- klass . update ( id , attributes )
618
+ model . update ( id , attributes )
619
619
end
620
620
end
621
621
622
622
def update! ( id = :all , attributes ) # :nodoc:
623
623
if id == :all
624
624
each { |record | record . update! ( attributes ) }
625
625
else
626
- klass . update! ( id , attributes )
626
+ model . update! ( id , attributes )
627
627
end
628
628
end
629
629
@@ -929,7 +929,7 @@ def update_counters(counters)
929
929
names = touch if touch != true
930
930
names = Array . wrap ( names )
931
931
options = names . extract_options!
932
- touch_updates = klass . touch_attributes_with_time ( *names , **options )
932
+ touch_updates = model . touch_attributes_with_time ( *names , **options )
933
933
updates . merge! ( touch_updates ) unless touch_updates . empty?
934
934
end
935
935
@@ -960,7 +960,7 @@ def update_counters(counters)
960
960
# Person.where(name: 'David').touch_all
961
961
# # => "UPDATE \"people\" SET \"updated_at\" = '2018-01-04 22:55:23.132670' WHERE \"people\".\"name\" = 'David'"
962
962
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 )
964
964
end
965
965
966
966
# Destroys the records by instantiating each
@@ -1012,20 +1012,20 @@ def delete_all
1012
1012
raise ActiveRecordError . new ( "delete_all doesn't support #{ invalid_methods . join ( ', ' ) } " )
1013
1013
end
1014
1014
1015
- klass . with_connection do |c |
1015
+ model . with_connection do |c |
1016
1016
arel = eager_loading? ? apply_join_dependency . arel : build_arel ( c )
1017
1017
arel . source . left = table
1018
1018
1019
1019
group_values_arel_columns = arel_columns ( group_values . uniq )
1020
1020
having_clause_ast = having_clause . ast unless having_clause . empty?
1021
- key = if klass . composite_primary_key?
1021
+ key = if model . composite_primary_key?
1022
1022
primary_key . map { |pk | table [ pk ] }
1023
1023
else
1024
1024
table [ primary_key ]
1025
1025
end
1026
1026
stmt = arel . compile_delete ( key , having_clause_ast , group_values_arel_columns )
1027
1027
1028
- c . delete ( stmt , "#{ klass } Delete All" ) . tap { reset }
1028
+ c . delete ( stmt , "#{ model } Delete All" ) . tap { reset }
1029
1029
end
1030
1030
end
1031
1031
@@ -1180,7 +1180,7 @@ def reload
1180
1180
def reset
1181
1181
@future_result &.cancel
1182
1182
@future_result = nil
1183
- @delegate_to_klass = false
1183
+ @delegate_to_model = false
1184
1184
@to_sql = @arel = @loaded = @should_eager_load = nil
1185
1185
@offsets = @take = nil
1186
1186
@cache_keys = nil
@@ -1200,7 +1200,7 @@ def to_sql
1200
1200
relation . to_sql
1201
1201
end
1202
1202
else
1203
- klass . with_connection do |conn |
1203
+ model . with_connection do |conn |
1204
1204
conn . unprepared_statement { conn . to_sql ( arel ) }
1205
1205
end
1206
1206
end
@@ -1210,12 +1210,12 @@ def to_sql
1210
1210
#
1211
1211
# User.where(name: 'Oscar').where_values_hash
1212
1212
# # => {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:
1214
1214
where_clause . to_h ( relation_table_name )
1215
1215
end
1216
1216
1217
1217
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 )
1219
1219
create_with_value . each { |k , v | hash [ k . to_s ] = v } unless create_with_value . empty?
1220
1220
hash
1221
1221
end
@@ -1283,15 +1283,15 @@ def inspect
1283
1283
end
1284
1284
1285
1285
def empty_scope? # :nodoc:
1286
- @values == klass . unscoped . values
1286
+ @values == model . unscoped . values
1287
1287
end
1288
1288
1289
1289
def has_limit_or_offset? # :nodoc:
1290
1290
limit_value || offset_value
1291
1291
end
1292
1292
1293
1293
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 )
1295
1295
end
1296
1296
1297
1297
class StrictLoadingScope # :nodoc:
@@ -1321,46 +1321,46 @@ def load_records(records)
1321
1321
1322
1322
private
1323
1323
def already_in_scope? ( registry )
1324
- @delegate_to_klass && registry . current_scope ( klass , true )
1324
+ @delegate_to_model && registry . current_scope ( model , true )
1325
1325
end
1326
1326
1327
1327
def global_scope? ( registry )
1328
- registry . global_current_scope ( klass , true )
1328
+ registry . global_current_scope ( model , true )
1329
1329
end
1330
1330
1331
1331
def current_scope_restoring_block ( &block )
1332
- current_scope = klass . current_scope ( true )
1332
+ current_scope = model . current_scope ( true )
1333
1333
-> record do
1334
- klass . current_scope = current_scope
1334
+ model . current_scope = current_scope
1335
1335
yield record if block_given?
1336
1336
end
1337
1337
end
1338
1338
1339
1339
def _new ( attributes , &block )
1340
- klass . new ( attributes , &block )
1340
+ model . new ( attributes , &block )
1341
1341
end
1342
1342
1343
1343
def _create ( attributes , &block )
1344
- klass . create ( attributes , &block )
1344
+ model . create ( attributes , &block )
1345
1345
end
1346
1346
1347
1347
def _create! ( attributes , &block )
1348
- klass . create! ( attributes , &block )
1348
+ model . create! ( attributes , &block )
1349
1349
end
1350
1350
1351
1351
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 )
1354
1354
1355
1355
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 )
1358
1358
end
1359
1359
yield
1360
1360
ensure
1361
- registry . set_current_scope ( klass , previous )
1361
+ registry . set_current_scope ( model , previous )
1362
1362
if all_queries
1363
- registry . set_global_current_scope ( klass , previous_global )
1363
+ registry . set_global_current_scope ( model , previous_global )
1364
1364
end
1365
1365
end
1366
1366
@@ -1372,7 +1372,7 @@ def _substitute_values(values)
1372
1372
value = Arel ::Nodes ::Grouping . new ( value )
1373
1373
end
1374
1374
else
1375
- type = klass . type_for_attribute ( attr . name )
1375
+ type = model . type_for_attribute ( attr . name )
1376
1376
value = predicate_builder . build_bind_attribute ( attr . name , type . cast ( value ) )
1377
1377
end
1378
1378
[ attr , value ]
@@ -1419,7 +1419,7 @@ def exec_main_query(async: false)
1419
1419
if where_clause . contradiction?
1420
1420
[ ] . freeze
1421
1421
elsif eager_loading?
1422
- klass . with_connection do |c |
1422
+ model . with_connection do |c |
1423
1423
apply_join_dependency do |relation , join_dependency |
1424
1424
if relation . null_relation?
1425
1425
[ ] . freeze
@@ -1431,8 +1431,8 @@ def exec_main_query(async: false)
1431
1431
end
1432
1432
end
1433
1433
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 )
1436
1436
end
1437
1437
end
1438
1438
end
@@ -1445,7 +1445,7 @@ def instantiate_records(rows, &block)
1445
1445
@_join_dependency = nil
1446
1446
records
1447
1447
else
1448
- klass . _load_from_sql ( rows , &block ) . freeze
1448
+ model . _load_from_sql ( rows , &block ) . freeze
1449
1449
end
1450
1450
end
1451
1451
0 commit comments