ar_transaction_changes 1.1.6 → 1.1.7

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: 4a6cb7a7d939217a2aa0ce44c25c2f89a97ed7457be1c1836d6e7d82e2250c9a
4
- data.tar.gz: 4d7295698fedf9d8981be7266b86f5c0ef75794ded3f0fce511b3aec151df76e
3
+ metadata.gz: 1b75ccc7bc393e60284a26e6077bdfc48e8032b52780770f0b762f292a6a3393
4
+ data.tar.gz: 2a048448c70283320792559fd7f7ae90d339384fda9fa3b9d437abf2607fd701
5
5
  SHA512:
6
- metadata.gz: b3fbd5e913fc97722ea9181f088f9dd2b0282751ceb5cc1588a9f73c63b5f3e1df97dfad7f4aa9ee9952b3bb52bba49edafa65419ca904623475806aad5ebb92
7
- data.tar.gz: 8bbbcf7edd439aa32bed2d1ef988a20adc49629256b779f1e6c473193c9b28c50c0d246e80b57195b37e452d81324a7ec71fd448a35f4b8f5bebe9abd4f4b7b8
6
+ metadata.gz: f3148c75a2a2bebc626451d1cbfbb7263cb1e39a56571af89761dc1f5b96a3a988de2a97ee2bef546744765f2b3eb569de77ce45fea5e7da1289821231435076
7
+ data.tar.gz: e21dff998d73c00ee6f6b8b7d92d9814412bf3421b8dc009e41429b7fc33e014cd9cd4f01d05288bc5009fc530749628de7d19c6e97973c53a3ddd37bc8f9cd0
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "ar_transaction_changes/version"
2
4
  require "active_record"
3
5
 
@@ -36,12 +38,34 @@ module ArTransactionChanges
36
38
 
37
39
  def _store_transaction_changed_attributes(attr_name)
38
40
  attr_name = attr_name.to_s
39
- old_value = read_attribute(attr_name)
41
+ old_value = _read_attribute_for_transaction(attr_name)
40
42
  ret = yield
41
- new_value = read_attribute(attr_name)
43
+ new_value = _read_attribute_for_transaction(attr_name)
42
44
  unless transaction_changed_attributes.key?(attr_name) || new_value == old_value
43
- transaction_changed_attributes[attr_name] = old_value
45
+ attribute = @attributes[attr_name]
46
+ transaction_changed_attributes[attr_name] = if attribute.type.is_a?(::ActiveRecord::Type::Serialized)
47
+ attribute.type.deserialize(old_value)
48
+ else
49
+ old_value
50
+ end
51
+ transaction_changed_attributes
44
52
  end
45
53
  ret
46
54
  end
55
+
56
+ def _read_attribute_for_transaction(attr_name)
57
+ attribute = @attributes[attr_name]
58
+ # Avoid causing an earlier memoized type cast of mutable serialized user values,
59
+ # since could prevent mutations of that user value from affecting the attribute value
60
+ # that would affect it without using this library.
61
+ if attribute.type.is_a?(::ActiveRecord::Type::Serialized)
62
+ if attribute.came_from_user?
63
+ attribute.type.serialize(attribute.value_before_type_cast)
64
+ else
65
+ attribute.value_before_type_cast
66
+ end
67
+ else
68
+ _read_attribute(attr_name)
69
+ end
70
+ end
47
71
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ArTransactionChanges
2
- VERSION = "1.1.6"
4
+ VERSION = "1.1.7"
3
5
  end
@@ -1,6 +1,16 @@
1
1
  class User < ActiveRecord::Base
2
2
  include ArTransactionChanges
3
3
 
4
+ class ConnectionDetails
5
+ attr_accessor :client_ip
6
+
7
+ def initialize(client_ip:)
8
+ @client_ip = client_ip
9
+ end
10
+ end
11
+
12
+ serialize :connection_details, Array
13
+
4
14
  attr_accessor :stored_transaction_changes
5
15
 
6
16
  after_commit :store_transaction_changes_for_tests
@@ -19,6 +19,7 @@ ActiveRecord::Base.connection.tap do |db|
19
19
  t.string :name
20
20
  t.string :occupation
21
21
  t.integer :age
22
+ t.text :connection_details
22
23
  t.timestamps null: false
23
24
  end
24
25
  end
@@ -110,4 +110,20 @@ class TransactionChangesTest < MiniTest::Unit::TestCase
110
110
  end
111
111
  assert_empty @user.stored_transaction_changes
112
112
  end
113
+
114
+ def test_serialized_attributes_value
115
+ @user.connection_details = [User::ConnectionDetails.new(client_ip: '1.1.1.1')]
116
+ @user.save!
117
+ old_value, new_value = @user.stored_transaction_changes['connection_details']
118
+ assert_equal([], old_value)
119
+ assert_equal(['1.1.1.1'], new_value.map(&:client_ip))
120
+ end
121
+
122
+ def test_serialized_attributes_mutation
123
+ details = User::ConnectionDetails.new(client_ip: '1.1.1.1')
124
+ @user.connection_details = [details]
125
+ details.client_ip = '2.2.2.2'
126
+ @user.save!
127
+ assert_equal '2.2.2.2', @user.connection_details.first.client_ip
128
+ end
113
129
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ar_transaction_changes
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.6
4
+ version: 1.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dylan Thacker-Smith
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-06-16 00:00:00.000000000 Z
11
+ date: 2020-07-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -79,7 +79,7 @@ homepage: https://github.com/dylanahsmith/ar_transaction_changes
79
79
  licenses:
80
80
  - MIT
81
81
  metadata: {}
82
- post_install_message:
82
+ post_install_message:
83
83
  rdoc_options: []
84
84
  require_paths:
85
85
  - lib
@@ -95,7 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
95
95
  version: '0'
96
96
  requirements: []
97
97
  rubygems_version: 3.1.2
98
- signing_key:
98
+ signing_key:
99
99
  specification_version: 4
100
100
  summary: Store transaction changes for active record objects
101
101
  test_files: