active_record-updated_at 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +2 -2
- data/active_record-updated_at.gemspec +1 -1
- data/lib/active_record-updated_at.rb +7 -5
- data/lib/active_record/updated_at/relation.rb +22 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e7e02ac57f228b518bd7cc7126a8cd6671f69378
|
4
|
+
data.tar.gz: 23a49a276233f00325ab7ab31b72ace2c3b6eb57
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 56b39e970c3d9e6db9ab23e4bf5ca9091543f8ec07916b9f3875d892f68af0e6d88be8bff500b736aba3eb276f47da5949a76fc08e1b8e677269347a808c7dcd
|
7
|
+
data.tar.gz: e86f9f6f381e4dc9487e3c187b98d300453c260b24e84cce02553cc75e70597c233f9259ee6464bbc0c8be19bf1cd381e8b97b564fac2b3c0ffacf973c3de601
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
#  active_record-updated_at
|
2
2
|
[](https://codeclimate.com/github/LendingHome/active_record-updated_at) [](https://codeclimate.com/github/LendingHome/active_record-updated_at) [](http://badge.fury.io/rb/active_record-updated_at)
|
3
3
|
|
4
|
-
> Touch `updated_at` by default with calls to `
|
4
|
+
> Touch `updated_at` by default with calls to `update_all` and `update_column(s)`
|
5
5
|
|
6
6
|
## Installation
|
7
7
|
|
@@ -19,7 +19,7 @@ The default `ActiveRecord` behavior does not touch `updated_at` when the followi
|
|
19
19
|
* `ActiveRecord::Base#update_columns`
|
20
20
|
* `ActiveRecord::Relation#update_all`
|
21
21
|
|
22
|
-
We **rarely ever have a case to modify data WITHOUT touching `updated_at`** so this gem enables the touching behavior by default. For those rare
|
22
|
+
We **rarely ever have a case to modify data WITHOUT touching `updated_at`** so this gem enables the touching behavior by default. For those rare occasions that we don't want the touching we can wrap these calls in a `disable` block explicitly:
|
23
23
|
|
24
24
|
```ruby
|
25
25
|
ActiveRecord::UpdatedAt.disable { User.update_all(role: "member") }
|
@@ -11,7 +11,7 @@ Gem::Specification.new do |s|
|
|
11
11
|
s.required_ruby_version = ">= 2.0.0"
|
12
12
|
s.summary = "Touch `updated_at` by default with calls to `update_column(s)` and `update_all`"
|
13
13
|
s.test_files = `git ls-files -- spec/*`.split("\n")
|
14
|
-
s.version = "0.0.
|
14
|
+
s.version = "0.0.2"
|
15
15
|
|
16
16
|
s.add_dependency "activerecord"
|
17
17
|
end
|
@@ -3,15 +3,17 @@ require_relative "active_record/updated_at/relation"
|
|
3
3
|
|
4
4
|
module ActiveRecord
|
5
5
|
module UpdatedAt
|
6
|
-
ActiveRecord::Relation.send(:
|
6
|
+
ActiveRecord::Relation.send(:include, Relation)
|
7
|
+
|
8
|
+
STATE = "#{name}::DISABLED".freeze
|
7
9
|
|
8
10
|
class << self
|
9
11
|
def disable(state = true)
|
10
|
-
disabled_was =
|
11
|
-
|
12
|
+
disabled_was = Thread.current[STATE]
|
13
|
+
Thread.current[STATE] = state
|
12
14
|
yield
|
13
15
|
ensure
|
14
|
-
|
16
|
+
Thread.current[STATE] = disabled_was
|
15
17
|
end
|
16
18
|
|
17
19
|
def enable(&block)
|
@@ -19,7 +21,7 @@ module ActiveRecord
|
|
19
21
|
end
|
20
22
|
|
21
23
|
def enabled?
|
22
|
-
|
24
|
+
!Thread.current[STATE]
|
23
25
|
end
|
24
26
|
end
|
25
27
|
end
|
@@ -1,7 +1,27 @@
|
|
1
1
|
module ActiveRecord
|
2
2
|
module UpdatedAt
|
3
3
|
module Relation
|
4
|
-
def
|
4
|
+
def self.included(base)
|
5
|
+
base.class_eval do
|
6
|
+
# We were originally using `prepend` to inject this behavior
|
7
|
+
# directly into the `update_all` method but this was causing
|
8
|
+
# `SystemStackError` exceptions when loaded alongside other
|
9
|
+
# gems like `newrelic_rpm` which uses alias method chains.
|
10
|
+
#
|
11
|
+
# It's unlikely NewRelic will change their API anytime soon
|
12
|
+
# since they have to support older versions of Ruby which do
|
13
|
+
# not support `prepend` so we'll use this deprecated style
|
14
|
+
# of method injection.
|
15
|
+
#
|
16
|
+
# Newer versions of ActiveRecord have already deprecated the
|
17
|
+
# old `alias_method_chain` method so we're doing it manually
|
18
|
+
# here to avoid deprecation warnings.
|
19
|
+
alias_method :update_all_without_updated_at, :update_all
|
20
|
+
alias_method :update_all, :update_all_with_updated_at
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def update_all_with_updated_at(query, *args, &block)
|
5
25
|
attribute_exists = column_names.include?("updated_at")
|
6
26
|
already_specified = Array(query).flatten.grep(/\bupdated_at\b/).any?
|
7
27
|
enabled = UpdatedAt.enabled?
|
@@ -19,7 +39,7 @@ module ActiveRecord
|
|
19
39
|
end
|
20
40
|
end
|
21
41
|
|
22
|
-
|
42
|
+
update_all_without_updated_at(query, *args, &block)
|
23
43
|
end
|
24
44
|
end
|
25
45
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_record-updated_at
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- LendingHome
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-11-
|
11
|
+
date: 2016-11-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|