active_record-updated_at 0.0.0 → 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +5 -5
- data/active_record-updated_at.gemspec +1 -1
- data/lib/active_record/updated_at/relation.rb +4 -10
- data/lib/active_record-updated_at.rb +21 -4
- data/spec/active_record-updated_at_spec.rb +19 -5
- metadata +1 -2
- data/lib/active_record/updated_at/model.rb +0 -27
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 395371ee4b162e5c5bf8235811222b4097e6ead7
|
4
|
+
data.tar.gz: d2b3362724771f82de0efb358348025eaf937da7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b859883939d611983f98be7d5495e0aaad6888f443548ac7a5fb8bf05e17ba9f5b95ef72d7ff7b006eb982b857e0728c71c53a75f2d5b53f290761fdd98d0f0b
|
7
|
+
data.tar.gz: 5d9496e37271d9e5c1e0c1b84a03be46cb4b98bd6c6aaaf519d4e0c07abbdc5b037925d85dbcee7eaaf6702cdafcf6357cc2a34878c05514b6eba88e79fdb81f
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -19,11 +19,11 @@ 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 occassions that we don't want the touching we can
|
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 occassions that we don't want the touching we can wrap these calls in a `disable` block explicitly:
|
23
23
|
|
24
|
-
|
25
|
-
|
26
|
-
|
24
|
+
```ruby
|
25
|
+
ActiveRecord::UpdatedAt.disable { User.update_all(role: "member") }
|
26
|
+
```
|
27
27
|
|
28
28
|
**If `updated_at` is explicitly specified then the UPDATE query is not modified**.
|
29
29
|
|
@@ -38,7 +38,7 @@ User.update_all(role: "member", updated_at: 1.day.ago)
|
|
38
38
|
User.update_all(role: "member", updated_at: nil)
|
39
39
|
|
40
40
|
# This doesn't touch `updated_at`
|
41
|
-
User.
|
41
|
+
ActiveRecord::UpdatedAt.disable { User.update_all(role: "member") }
|
42
42
|
```
|
43
43
|
|
44
44
|
## Testing
|
@@ -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.1"
|
15
15
|
|
16
16
|
s.add_dependency "activerecord"
|
17
17
|
end
|
@@ -1,19 +1,13 @@
|
|
1
1
|
module ActiveRecord
|
2
2
|
module UpdatedAt
|
3
3
|
module Relation
|
4
|
-
|
5
|
-
|
6
|
-
included do
|
7
|
-
alias_method :update_all_without_updated_at, :update_all
|
8
|
-
alias_method :update_all, :update_all_with_updated_at
|
9
|
-
end
|
10
|
-
|
11
|
-
def update_all_with_updated_at(query, *args, &block)
|
4
|
+
def update_all(query, *args, &block)
|
12
5
|
attribute_exists = column_names.include?("updated_at")
|
13
6
|
already_specified = Array(query).flatten.grep(/\bupdated_at\b/).any?
|
7
|
+
enabled = UpdatedAt.enabled?
|
14
8
|
updated_at = Time.current
|
15
9
|
|
16
|
-
if attribute_exists && !already_specified
|
10
|
+
if attribute_exists && !already_specified && enabled
|
17
11
|
case query
|
18
12
|
when Array
|
19
13
|
query.first << ", updated_at = ?"
|
@@ -25,7 +19,7 @@ module ActiveRecord
|
|
25
19
|
end
|
26
20
|
end
|
27
21
|
|
28
|
-
|
22
|
+
super
|
29
23
|
end
|
30
24
|
end
|
31
25
|
end
|
@@ -1,9 +1,26 @@
|
|
1
1
|
require "active_record"
|
2
|
-
require_relative "active_record/updated_at/model"
|
3
2
|
require_relative "active_record/updated_at/relation"
|
4
3
|
|
5
4
|
module ActiveRecord
|
6
|
-
|
7
|
-
|
8
|
-
|
5
|
+
module UpdatedAt
|
6
|
+
ActiveRecord::Relation.send(:prepend, Relation)
|
7
|
+
|
8
|
+
class << self
|
9
|
+
def disable(state = true)
|
10
|
+
disabled_was = @disabled
|
11
|
+
@disabled = state
|
12
|
+
yield
|
13
|
+
ensure
|
14
|
+
@disabled = disabled_was
|
15
|
+
end
|
16
|
+
|
17
|
+
def enable(&block)
|
18
|
+
disable(false, &block)
|
19
|
+
end
|
20
|
+
|
21
|
+
def enabled?
|
22
|
+
!@disabled
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
9
26
|
end
|
@@ -4,6 +4,20 @@ RSpec.describe ActiveRecord::UpdatedAt do
|
|
4
4
|
let!(:user) { User.create!(name: "test") }
|
5
5
|
let!(:timestamp) { 1.day.ago }
|
6
6
|
|
7
|
+
describe "#enabled" do
|
8
|
+
it "can be nested with disable blocks" do
|
9
|
+
expect(described_class).to be_enabled
|
10
|
+
|
11
|
+
described_class.disable do
|
12
|
+
expect(described_class).not_to be_enabled
|
13
|
+
described_class.enable { expect(described_class).to be_enabled }
|
14
|
+
expect(described_class).not_to be_enabled
|
15
|
+
end
|
16
|
+
|
17
|
+
expect(described_class).to be_enabled
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
7
21
|
describe "#update_all" do
|
8
22
|
context "with an array" do
|
9
23
|
it "touches updated_at" do
|
@@ -19,7 +33,7 @@ RSpec.describe ActiveRecord::UpdatedAt do
|
|
19
33
|
end
|
20
34
|
|
21
35
|
it "does not touch updated_at with workaround method" do
|
22
|
-
User.
|
36
|
+
described_class.disable { User.update_all(["name = ?", "changed"]) }
|
23
37
|
expect(reloaded.name).to eq("changed")
|
24
38
|
expect(reloaded.updated_at).to eq(user.updated_at)
|
25
39
|
end
|
@@ -39,7 +53,7 @@ RSpec.describe ActiveRecord::UpdatedAt do
|
|
39
53
|
end
|
40
54
|
|
41
55
|
it "does not touch updated_at with workaround method" do
|
42
|
-
User.
|
56
|
+
described_class.disable { User.update_all(name: "changed") }
|
43
57
|
expect(reloaded.name).to eq("changed")
|
44
58
|
expect(reloaded.updated_at).to eq(user.updated_at)
|
45
59
|
end
|
@@ -60,7 +74,7 @@ RSpec.describe ActiveRecord::UpdatedAt do
|
|
60
74
|
end
|
61
75
|
|
62
76
|
it "does not touch updated_at with workaround method" do
|
63
|
-
User.
|
77
|
+
described_class.disable { User.update_all("name = 'changed'") }
|
64
78
|
expect(reloaded.name).to eq("changed")
|
65
79
|
expect(reloaded.updated_at).to eq(user.updated_at)
|
66
80
|
end
|
@@ -80,7 +94,7 @@ RSpec.describe ActiveRecord::UpdatedAt do
|
|
80
94
|
end
|
81
95
|
|
82
96
|
it "does not touch updated_at with workaround method" do
|
83
|
-
user.
|
97
|
+
described_class.disable { user.update_column(:name, "changed") }
|
84
98
|
expect(reloaded.name).to eq("changed")
|
85
99
|
expect(reloaded.updated_at).to eq(user.updated_at)
|
86
100
|
end
|
@@ -100,7 +114,7 @@ RSpec.describe ActiveRecord::UpdatedAt do
|
|
100
114
|
end
|
101
115
|
|
102
116
|
it "does not touch updated_at with workaround method" do
|
103
|
-
user.
|
117
|
+
described_class.disable { user.update_columns(name: "changed") }
|
104
118
|
expect(reloaded.name).to eq("changed")
|
105
119
|
expect(reloaded.updated_at).to eq(user.updated_at)
|
106
120
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
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.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- LendingHome
|
@@ -45,7 +45,6 @@ files:
|
|
45
45
|
- bin/test
|
46
46
|
- docker-compose.yml
|
47
47
|
- lib/active_record-updated_at.rb
|
48
|
-
- lib/active_record/updated_at/model.rb
|
49
48
|
- lib/active_record/updated_at/relation.rb
|
50
49
|
- spec/active_record-updated_at_spec.rb
|
51
50
|
- spec/internal/app/models/user.rb
|
@@ -1,27 +0,0 @@
|
|
1
|
-
module ActiveRecord
|
2
|
-
module UpdatedAt
|
3
|
-
module Model
|
4
|
-
extend ActiveSupport::Concern
|
5
|
-
|
6
|
-
included do
|
7
|
-
alias_method :update_columns_without_updated_at, :update_columns
|
8
|
-
alias_method :update_columns, :update_columns_with_updated_at
|
9
|
-
end
|
10
|
-
|
11
|
-
def update_column_without_updated_at(attribute, value)
|
12
|
-
update_columns_without_updated_at(attribute => value)
|
13
|
-
end
|
14
|
-
|
15
|
-
def update_columns_with_updated_at(attributes)
|
16
|
-
attribute_exists = has_attribute?(:updated_at)
|
17
|
-
already_specified = attributes.with_indifferent_access.key?(:updated_at)
|
18
|
-
|
19
|
-
if attribute_exists && !already_specified
|
20
|
-
attributes[:updated_at] = Time.current
|
21
|
-
end
|
22
|
-
|
23
|
-
update_columns_without_updated_at(attributes)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|