@@ -6,12 +6,14 @@ module DelayTouching
66 extend ActiveSupport ::Concern
77
88 # Override ActiveRecord::Base#touch.
9- def touch ( *names )
10- if self . class . delay_touching? && !try ( :no_touching? )
11- DelayTouching . add_record ( self , *names )
12- true
13- else
14- super
9+ if ActiveRecord ::VERSION ::MAJOR >= 5
10+ def touch ( *names , time : nil )
11+ names = self . class . send ( :timestamp_attributes_for_update_in_model ) if names . empty?
12+ DelayTouching . handle_touch ( self , names ) || super
13+ end
14+ else
15+ def touch ( *names )
16+ DelayTouching . handle_touch ( self , names ) || super
1517 end
1618 end
1719
@@ -45,6 +47,13 @@ class << self
4547 delegate :add_record , to : :state
4648 end
4749
50+ def self . handle_touch ( record , names )
51+ if record . class . delay_touching? && !record . try ( :no_touching? )
52+ add_record ( record , *names )
53+ true
54+ end
55+ end
56+
4857 # Start delaying all touches. When done, apply them. (Unless nested.)
4958 def self . call
5059 state . nesting += 1
@@ -88,10 +97,8 @@ def self.touch_records(attr, klass, records)
8897 records . each do |record |
8998 # Don't bother if destroyed or not-saved
9099 next unless record . persisted?
91- record . instance_eval do
92- write_attribute column , current_time
93- @changed_attributes . except! ( *changes . keys )
94- end
100+ record . send ( :write_attribute , column , current_time )
101+ clear_attribute_changes ( record , changes . keys )
95102 end
96103 end
97104
@@ -100,6 +107,14 @@ def self.touch_records(attr, klass, records)
100107 state . updated attr , records
101108 records . each { |record | record . run_callbacks ( :touch ) }
102109 end
110+
111+ def self . clear_attribute_changes ( record , attr_names )
112+ if ActiveRecord ::VERSION ::MAJOR >= 5
113+ record . clear_attribute_changes ( attr_names )
114+ else
115+ record . instance_variable_get ( '@changed_attributes' ) . except! ( *attr_names )
116+ end
117+ end
103118 end
104119end
105120
0 commit comments