Skip to content
This repository was archived by the owner on Jan 27, 2021. It is now read-only.

Commit 42ee314

Browse files
committed
support rails 5
1 parent e71ade4 commit 42ee314

2 files changed

Lines changed: 26 additions & 11 deletions

File tree

activerecord-delay_touching.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
1818
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
1919
spec.require_paths = ["lib"]
2020

21-
spec.add_dependency "activerecord", "~> 4.2"
21+
spec.add_dependency "activerecord", ">= 4.2", "< 5.3"
2222

2323
spec.add_development_dependency "bundler", "~> 1.6"
2424
spec.add_development_dependency "rake"

lib/activerecord/delay_touching.rb

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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
104119
end
105120

0 commit comments

Comments
 (0)