@@ -6,7 +6,8 @@ module DelayTouching
66 extend ActiveSupport ::Concern
77
88 # Override ActiveRecord::Base#touch.
9- def touch ( *names )
9+ # see https://github.com/godaddy/activerecord-delay_touching/pull/21 for Rails 5 support
10+ def touch ( *names , time : nil )
1011 if self . class . delay_touching? && !try ( :no_touching? )
1112 DelayTouching . add_record ( self , *names )
1213 true
@@ -88,10 +89,8 @@ def self.touch_records(attr, klass, records)
8889 records . each do |record |
8990 # Don't bother if destroyed or not-saved
9091 next unless record . persisted?
91- record . instance_eval do
92- write_attribute column , current_time
93- @changed_attributes . except! ( *changes . keys )
94- end
92+ record . send ( :write_attribute , column , current_time )
93+ clear_attribute_changes ( record , changes . keys )
9594 end
9695 end
9796
@@ -100,6 +99,16 @@ def self.touch_records(attr, klass, records)
10099 state . updated attr , records
101100 records . each { |record | record . run_callbacks ( :touch ) }
102101 end
102+
103+ if ActiveRecord ::VERSION ::MAJOR >= 5
104+ def self . clear_attribute_changes ( record , attr_names )
105+ record . clear_attribute_changes ( attr_names )
106+ end
107+ else
108+ def self . clear_attribute_changes ( record , attr_names )
109+ record . instance_variable_get ( '@changed_attributes' ) . except! ( *attr_names )
110+ end
111+ end
103112 end
104113end
105114
0 commit comments