@@ -810,159 +810,159 @@ def touch(*names, time: nil)
810
810
end
811
811
end
812
812
813
- private
814
- def init_internals
815
- super
816
- @_trigger_destroy_callback = @_trigger_update_callback = nil
817
- @previously_new_record = false
818
- end
813
+ private
814
+ def init_internals
815
+ super
816
+ @_trigger_destroy_callback = @_trigger_update_callback = nil
817
+ @previously_new_record = false
818
+ end
819
819
820
- def strict_loaded_associations
821
- @association_cache . find_all do |_ , assoc |
822
- assoc . owner . strict_loading? && !assoc . owner . strict_loading_n_plus_one_only?
823
- end . map ( &:first )
824
- end
820
+ def strict_loaded_associations
821
+ @association_cache . find_all do |_ , assoc |
822
+ assoc . owner . strict_loading? && !assoc . owner . strict_loading_n_plus_one_only?
823
+ end . map ( &:first )
824
+ end
825
825
826
- def _find_record ( options )
827
- all_queries = options ? options [ :all_queries ] : nil
828
- base = self . class . all ( all_queries : all_queries ) . preload ( strict_loaded_associations )
826
+ def _find_record ( options )
827
+ all_queries = options ? options [ :all_queries ] : nil
828
+ base = self . class . all ( all_queries : all_queries ) . preload ( strict_loaded_associations )
829
829
830
- if options && options [ :lock ]
831
- base . lock ( options [ :lock ] ) . find_by! ( _in_memory_query_constraints_hash )
832
- else
833
- base . find_by! ( _in_memory_query_constraints_hash )
830
+ if options && options [ :lock ]
831
+ base . lock ( options [ :lock ] ) . find_by! ( _in_memory_query_constraints_hash )
832
+ else
833
+ base . find_by! ( _in_memory_query_constraints_hash )
834
+ end
834
835
end
835
- end
836
836
837
- def _in_memory_query_constraints_hash
838
- if self . class . query_constraints_list . nil?
839
- { @primary_key => id }
840
- else
841
- self . class . query_constraints_list . index_with do |column_name |
842
- attribute ( column_name )
837
+ def _in_memory_query_constraints_hash
838
+ if self . class . query_constraints_list . nil?
839
+ { @primary_key => id }
840
+ else
841
+ self . class . query_constraints_list . index_with do |column_name |
842
+ attribute ( column_name )
843
+ end
843
844
end
844
845
end
845
- end
846
846
847
- def apply_scoping? ( options )
848
- !( options && options [ :unscoped ] ) &&
849
- ( self . class . default_scopes? ( all_queries : true ) || self . class . global_current_scope )
850
- end
847
+ def apply_scoping? ( options )
848
+ !( options && options [ :unscoped ] ) &&
849
+ ( self . class . default_scopes? ( all_queries : true ) || self . class . global_current_scope )
850
+ end
851
851
852
- def _query_constraints_hash
853
- if self . class . query_constraints_list . nil?
854
- { @primary_key => id_in_database }
855
- else
856
- self . class . query_constraints_list . index_with do |column_name |
857
- attribute_in_database ( column_name )
852
+ def _query_constraints_hash
853
+ if self . class . query_constraints_list . nil?
854
+ { @primary_key => id_in_database }
855
+ else
856
+ self . class . query_constraints_list . index_with do |column_name |
857
+ attribute_in_database ( column_name )
858
+ end
858
859
end
859
860
end
860
- end
861
-
862
- # A hook to be overridden by association modules.
863
- def destroy_associations
864
- end
865
-
866
- def destroy_row
867
- _delete_row
868
- end
869
861
870
- def _delete_row
871
- self . class . _delete_record ( _query_constraints_hash )
872
- end
862
+ # A hook to be overridden by association modules.
863
+ def destroy_associations
864
+ end
873
865
874
- def _touch_row ( attribute_names , time )
875
- time ||= current_time_from_proper_timezone
866
+ def destroy_row
867
+ _delete_row
868
+ end
876
869
877
- attribute_names . each do | attr_name |
878
- _write_attribute ( attr_name , time )
870
+ def _delete_row
871
+ self . class . _delete_record ( _query_constraints_hash )
879
872
end
880
873
881
- _update_row ( attribute_names , "touch" )
882
- end
874
+ def _touch_row ( attribute_names , time )
875
+ time ||= current_time_from_proper_timezone
883
876
884
- def _update_row ( attribute_names , attempted_action = "update" )
885
- self . class . _update_record (
886
- attributes_with_values ( attribute_names ) ,
887
- _query_constraints_hash
888
- )
889
- end
877
+ attribute_names . each do |attr_name |
878
+ _write_attribute ( attr_name , time )
879
+ end
890
880
891
- def create_or_update ( **, &block )
892
- _raise_readonly_record_error if readonly?
893
- return false if destroyed?
894
- result = new_record? ? _create_record ( &block ) : _update_record ( &block )
895
- result != false
896
- end
881
+ _update_row ( attribute_names , "touch" )
882
+ end
897
883
898
- # Updates the associated record with values matching those of the instance attributes.
899
- # Returns the number of affected rows.
900
- def _update_record ( attribute_names = self . attribute_names )
901
- attribute_names = attributes_for_update ( attribute_names )
884
+ def _update_row ( attribute_names , attempted_action = "update" )
885
+ self . class . _update_record (
886
+ attributes_with_values ( attribute_names ) ,
887
+ _query_constraints_hash
888
+ )
889
+ end
902
890
903
- if attribute_names . empty?
904
- affected_rows = 0
905
- @_trigger_update_callback = true
906
- else
907
- affected_rows = _update_row ( attribute_names )
908
- @_trigger_update_callback = affected_rows == 1
891
+ def create_or_update ( **, &block )
892
+ _raise_readonly_record_error if readonly?
893
+ return false if destroyed?
894
+ result = new_record? ? _create_record ( &block ) : _update_record ( &block )
895
+ result != false
909
896
end
910
897
911
- @previously_new_record = false
898
+ # Updates the associated record with values matching those of the instance attributes.
899
+ # Returns the number of affected rows.
900
+ def _update_record ( attribute_names = self . attribute_names )
901
+ attribute_names = attributes_for_update ( attribute_names )
912
902
913
- yield ( self ) if block_given?
903
+ if attribute_names . empty?
904
+ affected_rows = 0
905
+ @_trigger_update_callback = true
906
+ else
907
+ affected_rows = _update_row ( attribute_names )
908
+ @_trigger_update_callback = affected_rows == 1
909
+ end
914
910
915
- affected_rows
916
- end
911
+ @previously_new_record = false
917
912
918
- # Creates a record with values matching those of the instance attributes
919
- # and returns its id.
920
- def _create_record ( attribute_names = self . attribute_names )
921
- attribute_names = attributes_for_create ( attribute_names )
913
+ yield ( self ) if block_given?
922
914
923
- self . class . with_connection do | connection |
924
- returning_columns = self . class . _returning_columns_for_insert ( connection )
915
+ affected_rows
916
+ end
925
917
926
- returning_values = self . class . _insert_record (
927
- connection ,
928
- attributes_with_values ( attribute_names ) ,
929
- returning_columns
930
- )
918
+ # Creates a record with values matching those of the instance attributes
919
+ # and returns its id.
920
+ def _create_record ( attribute_names = self . attribute_names )
921
+ attribute_names = attributes_for_create ( attribute_names )
931
922
932
- returning_columns . zip ( returning_values ) . each do |column , value |
933
- _write_attribute ( column , value ) if !_read_attribute ( column )
934
- end if returning_values
935
- end
923
+ self . class . with_connection do |connection |
924
+ returning_columns = self . class . _returning_columns_for_insert ( connection )
936
925
937
- @new_record = false
938
- @previously_new_record = true
926
+ returning_values = self . class . _insert_record (
927
+ connection ,
928
+ attributes_with_values ( attribute_names ) ,
929
+ returning_columns
930
+ )
939
931
940
- yield ( self ) if block_given?
932
+ returning_columns . zip ( returning_values ) . each do |column , value |
933
+ _write_attribute ( column , value ) if !_read_attribute ( column )
934
+ end if returning_values
935
+ end
941
936
942
- id
943
- end
937
+ @new_record = false
938
+ @previously_new_record = true
944
939
945
- def verify_readonly_attribute ( name )
946
- raise ActiveRecordError , "#{ name } is marked as readonly" if self . class . readonly_attribute? ( name )
947
- end
940
+ yield ( self ) if block_given?
948
941
949
- def _raise_record_not_destroyed
950
- @_association_destroy_exception ||= nil
951
- key = self . class . primary_key
952
- raise @_association_destroy_exception || RecordNotDestroyed . new ( "Failed to destroy #{ self . class } with #{ key } =#{ id } " , self )
953
- ensure
954
- @_association_destroy_exception = nil
955
- end
942
+ id
943
+ end
956
944
957
- def _raise_readonly_record_error
958
- raise ReadOnlyRecord , "#{ self . class } is marked as readonly"
959
- end
945
+ def verify_readonly_attribute ( name )
946
+ raise ActiveRecordError , "#{ name } is marked as readonly" if self . class . readonly_attribute? ( name )
947
+ end
960
948
961
- def _raise_record_not_touched_error
962
- raise ActiveRecordError , <<~MSG . squish
963
- Cannot touch on a new or destroyed record object. Consider using
964
- persisted?, new_record?, or destroyed? before touching.
965
- MSG
966
- end
949
+ def _raise_record_not_destroyed
950
+ @_association_destroy_exception ||= nil
951
+ key = self . class . primary_key
952
+ raise @_association_destroy_exception || RecordNotDestroyed . new ( "Failed to destroy #{ self . class } with #{ key } =#{ id } " , self )
953
+ ensure
954
+ @_association_destroy_exception = nil
955
+ end
956
+
957
+ def _raise_readonly_record_error
958
+ raise ReadOnlyRecord , "#{ self . class } is marked as readonly"
959
+ end
960
+
961
+ def _raise_record_not_touched_error
962
+ raise ActiveRecordError , <<~MSG . squish
963
+ Cannot touch on a new or destroyed record object. Consider using
964
+ persisted?, new_record?, or destroyed? before touching.
965
+ MSG
966
+ end
967
967
end
968
968
end
0 commit comments