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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 11b3013f3d9c32bc668986d89b880ea877a10bd3
4
- data.tar.gz: 085d518fa7940abd8e894604bf60103d00ce3cb2
3
+ metadata.gz: 395371ee4b162e5c5bf8235811222b4097e6ead7
4
+ data.tar.gz: d2b3362724771f82de0efb358348025eaf937da7
5
5
  SHA512:
6
- metadata.gz: 32d49111d8517931ae445b3a36eee57fbc290323377aa357bcc2baba6bf6fb464262a8468f5a125d9b43fd77012efef5d2d7b73cd63ced9079cc2ea9ba2b9c02
7
- data.tar.gz: e1004fd0bf6cc3b0794042eadceca4db61ae8f544b2f4d19c8145f2ede0d993c18da5377a23c886f2d80ac2ab1ca7bd9675d40e9e3c40172d4736c3a8cc1b658
6
+ metadata.gz: b859883939d611983f98be7d5495e0aaad6888f443548ac7a5fb8bf05e17ba9f5b95ef72d7ff7b006eb982b857e0728c71c53a75f2d5b53f290761fdd98d0f0b
7
+ data.tar.gz: 5d9496e37271d9e5c1e0c1b84a03be46cb4b98bd6c6aaaf519d4e0c07abbdc5b037925d85dbcee7eaaf6702cdafcf6357cc2a34878c05514b6eba88e79fdb81f
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- active_record-updated_at (0.0.0)
4
+ active_record-updated_at (0.0.1)
5
5
  activerecord
6
6
 
7
7
  GEM
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 call these alternative methods to explicitly disable it:
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
- * `ActiveRecord::Base#update_column_without_updated_at`
25
- * `ActiveRecord::Base#update_columns_without_updated_at`
26
- * `ActiveRecord::Relation#update_all_without_updated_at`
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.update_all_without_updated_at(role: "member")
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.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
- extend ActiveSupport::Concern
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
- update_all_without_updated_at(query, *args, &block)
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
- Base.send(:include, UpdatedAt::Model)
7
- Relation.send(:include, UpdatedAt::Relation)
8
- Querying.delegate(:update_all_with_updated_at, :update_all_without_updated_at, to: :all)
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.update_all_without_updated_at(["name = ?", "changed"])
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.update_all_without_updated_at(name: "changed")
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.update_all_without_updated_at("name = 'changed'")
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.update_column_without_updated_at(:name, "changed")
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.update_columns_without_updated_at(name: "changed")
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.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