HornsAndHooves-moribus 0.9.0 → 0.10.0

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
  SHA256:
3
- metadata.gz: 458dd43da4a7bb700075f307e8bac67beea88ac91cc103d01dbc873e754c53ee
4
- data.tar.gz: 699fd07e4333c5cea0c317c43f82d1f33e6b1f9629d32ff55099dd280f28a4f8
3
+ metadata.gz: 39ad765918ae1ca73efc10e704346476936df1793e19f25089d27910089871d7
4
+ data.tar.gz: 0b579beea800b1b5987e9b7996137efc9dfa5c3e12f70fb04c53f7a5e113ea84
5
5
  SHA512:
6
- metadata.gz: 8379ba55ec3c949f21961b50ac3d2979a093fa2790e3e9bd134574d76f7f2ed9cd82eb06ee752771f5e265a63568fc3cb69fe01502e38fdd5ff829d5df279add
7
- data.tar.gz: 533dc47aa565a5ce7b84ef924eea7a18d0a3d4776c29d6a971e1a430b7a3dd713143b7e9d4db253d00777dc5b430571dd62d3b7b3c64fd01a45b09b9ae8790a2
6
+ metadata.gz: bf6c76a8ae0260b424abefe0de61c9b5bb11cfd2b6c00d604db9144bf92b911363f7a5bbaed5f22bd403e218ba9a3db0c92ef1b372430cbdb0f155fba200f9f5
7
+ data.tar.gz: c54c2c7fd4f356dadb559bb41ac262c00d54f8e4ae5f8999f53be79b0bf6a651f5ea2c797f9dcff2f2d1a149df5ea4494250bf728bd27411910fbf4f049ae9a3
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.6.6
1
+ 2.7.6
@@ -26,7 +26,7 @@ module Moribus
26
26
  # the lookup succeeds, we don't want the original #save to be executed.
27
27
  # But if +false+ is returned by the callback, it will also be returned
28
28
  # by the #save method, wrongly indicating the result of saving.
29
- def save(*)
29
+ def save(**)
30
30
  @updated_as_aggregated = false
31
31
  run_callbacks(:save) do
32
32
  return (lookup_self_and_replace or super) if new_record?
@@ -47,8 +47,8 @@ module Moribus
47
47
  end
48
48
 
49
49
  # Bang version of #save.
50
- def save!(*args)
51
- save(*args) or raise_record_not_saved_error
50
+ def save!(**kwargs)
51
+ save(**kwargs) or raise_record_not_saved_error
52
52
  end
53
53
 
54
54
  # Raise "record not saved".
@@ -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
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Moribus # :nodoc:
2
- VERSION = "0.9.0" # :nodoc:
4
+ VERSION = "0.10.0" # :nodoc:
3
5
  end
@@ -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) { SpecCustomerInfo.create(spec_customer: customer, is_current: true) }
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
- before do
31
- allow(customer.spec_customer_info).to receive(:new_record?).and_return(true)
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
- it "sets 'is_current' flag of overridden record to false for new record" do
35
- old_info = customer.spec_customer_info
36
- customer.spec_customer_info = SpecCustomerInfo.new
37
- expect(old_info.is_current).to be_falsey
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
@@ -47,8 +47,7 @@ def MoribusSpecModel(column_definition = {})
47
47
  end
48
48
  options[:default] = 0 if name == :lock_version
49
49
  args = [type, name]
50
- args << options unless options.empty?
51
- t.send(*args)
50
+ t.send(*args, **options)
52
51
  end
53
52
  end
54
53
  end
@@ -56,4 +55,4 @@ def MoribusSpecModel(column_definition = {})
56
55
  end
57
56
  end
58
57
  end
59
- end
58
+ 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.0
4
+ version: 0.10.0
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-11 00:00:00.000000000 Z
14
+ date: 2024-01-09 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.9
211
+ rubygems_version: 3.1.6
212
212
  signing_key:
213
213
  specification_version: 4
214
214
  summary: Introduces Aggregated and Tracked behavior to ActiveRecord::Base models