HornsAndHooves-moribus 0.9.0 → 0.9.1
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
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 54e52262da75d0c70f93a440dab8adbebc1c9ccb23d8bf9eff905572ca9028ae
|
|
4
|
+
data.tar.gz: a4c982838e5ea190585f1e8d8bfe776574584bb669136f372506fec7d9e5e38e
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 7b1bbadc5517c5309af3dd7f8dcc358e9966f5c7ef0ba6bbf3ee47b466d1771ab27620a3204ac45635bcd71f9619e16010eb5559d3b651adc951663cadeec101
|
|
7
|
+
data.tar.gz: 268eab60e758cc9f5f4dee5c44d8d60dfc6b5ec0f471d7108b73ba1bafe62b963b09724929fa3e65b5a07a400f9024545cb9a26bbc626ba944d1d4a75f65fa22
|
|
@@ -6,21 +6,32 @@ module Moribus
|
|
|
6
6
|
# Sets 'is_current' flag of overridden record to false, instead
|
|
7
7
|
# of deleting it or setting foreign key to nil.
|
|
8
8
|
def remove_target!(*)
|
|
9
|
+
# Use custom update to avoid running ActiveRecord optimistic locking
|
|
10
|
+
# and to avoid updating lock_version column:
|
|
9
11
|
if target.new_record?
|
|
10
12
|
target.is_current = false
|
|
13
|
+
target.updated_at = Time.zone.now if has_updated_at_column?
|
|
11
14
|
else
|
|
12
|
-
# Use custom update to avoid running ActiveRecord optimistic locking
|
|
13
|
-
# and to avoid updating lock_version column:
|
|
14
15
|
klass = target.class
|
|
15
16
|
|
|
16
17
|
sql = "UPDATE #{klass.quoted_table_name} " \
|
|
17
18
|
"SET \"is_current\" = #{klass.connection.quote(false)} "
|
|
19
|
+
|
|
20
|
+
sql << %{, "updated_at" = #{klass.connection.quote(Time.zone.now)} } if has_updated_at_column?
|
|
21
|
+
|
|
18
22
|
sql << "WHERE #{klass.quoted_primary_key} = " \
|
|
19
23
|
"#{klass.connection.quote(target.send(klass.primary_key))} "
|
|
20
24
|
|
|
21
25
|
klass.connection.update sql
|
|
22
26
|
end
|
|
23
27
|
end
|
|
28
|
+
|
|
29
|
+
# Is the record has a column with name `updated_at`
|
|
30
|
+
#
|
|
31
|
+
# @return [Boolean]
|
|
32
|
+
private def has_updated_at_column?
|
|
33
|
+
target.class.column_names.include?("updated_at")
|
|
34
|
+
end
|
|
24
35
|
end
|
|
25
36
|
end
|
|
26
37
|
end
|
data/lib/moribus/version.rb
CHANGED
|
@@ -16,6 +16,7 @@ describe Moribus::Extensions::HasCurrentExtension do
|
|
|
16
16
|
|
|
17
17
|
class SpecCustomer < MoribusSpecModel(spec_status_id: :integer)
|
|
18
18
|
has_one_current :spec_customer_info, inverse_of: :spec_customer, dependent: :destroy
|
|
19
|
+
delegate_associated :previous_id, to: :spec_customer_info
|
|
19
20
|
end
|
|
20
21
|
end
|
|
21
22
|
|
|
@@ -24,17 +25,45 @@ describe Moribus::Extensions::HasCurrentExtension do
|
|
|
24
25
|
end
|
|
25
26
|
|
|
26
27
|
let(:customer) { SpecCustomer.create }
|
|
27
|
-
let!(:info)
|
|
28
|
+
let!(:info) do
|
|
29
|
+
SpecCustomerInfo.create(spec_customer: customer, is_current: true, created_at: 1.hour.ago, updated_at: 1.hour.ago)
|
|
30
|
+
end
|
|
28
31
|
|
|
29
32
|
describe ".remove_target!" do
|
|
30
|
-
|
|
31
|
-
|
|
33
|
+
context "new record" do
|
|
34
|
+
before do
|
|
35
|
+
allow(customer.spec_customer_info).to receive(:new_record?).and_return(true)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
it "sets 'is_current' flag of overridden record to false for new record" do
|
|
39
|
+
old_info = customer.spec_customer_info
|
|
40
|
+
customer.spec_customer_info = SpecCustomerInfo.new
|
|
41
|
+
expect(old_info.is_current).to be false
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
it "sets 'is_current' flag updates updated_at column" do
|
|
45
|
+
old_info = customer.spec_customer_info
|
|
46
|
+
customer.spec_customer_info = SpecCustomerInfo.new
|
|
47
|
+
expect(old_info.updated_at >= old_info.created_at + 1.hour).to be true
|
|
48
|
+
end
|
|
32
49
|
end
|
|
33
50
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
51
|
+
context "persisted record" do
|
|
52
|
+
it "sets 'is_current' flag updates updated_at column" do
|
|
53
|
+
old_info = customer.spec_customer_info
|
|
54
|
+
customer.spec_customer_info = SpecCustomerInfo.new
|
|
55
|
+
old_info.reload
|
|
56
|
+
# I use 59 minutes instead of hour because there can be small difference in milliseconds
|
|
57
|
+
expect(old_info.updated_at >= old_info.created_at + 59.minutes).to be true
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
it "sets 'is_current' flag updates updated_at column" do
|
|
61
|
+
old_info = customer.spec_customer_info
|
|
62
|
+
customer.update!(previous_id: 123)
|
|
63
|
+
old_info.reload
|
|
64
|
+
# I use 59 minutes instead of hour because there can be small difference in milliseconds
|
|
65
|
+
expect(old_info.updated_at >= old_info.created_at + 59.minutes).to be true
|
|
66
|
+
end
|
|
38
67
|
end
|
|
39
68
|
end
|
|
40
69
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: HornsAndHooves-moribus
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.9.
|
|
4
|
+
version: 0.9.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- HornsAndHooves
|
|
@@ -11,7 +11,7 @@ authors:
|
|
|
11
11
|
autorequire:
|
|
12
12
|
bindir: bin
|
|
13
13
|
cert_chain: []
|
|
14
|
-
date: 2022-08-
|
|
14
|
+
date: 2022-08-24 00:00:00.000000000 Z
|
|
15
15
|
dependencies:
|
|
16
16
|
- !ruby/object:Gem::Dependency
|
|
17
17
|
name: rails
|
|
@@ -208,7 +208,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
208
208
|
- !ruby/object:Gem::Version
|
|
209
209
|
version: '0'
|
|
210
210
|
requirements: []
|
|
211
|
-
rubygems_version: 3.0.
|
|
211
|
+
rubygems_version: 3.0.8
|
|
212
212
|
signing_key:
|
|
213
213
|
specification_version: 4
|
|
214
214
|
summary: Introduces Aggregated and Tracked behavior to ActiveRecord::Base models
|