Skip to content

Commit dc18b9d

Browse files
committed
Rails 5 support (#3)
1 parent 3817efa commit dc18b9d

3 files changed

Lines changed: 16 additions & 7 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: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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
104113
end
105114

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module Activerecord
22
module DelayTouching
3-
VERSION = "1.1.0"
3+
VERSION = "1.1.1"
44
end
55
end

0 commit comments

Comments
 (0)