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 +4 -4
- data/lib/ar_transaction_changes.rb +27 -3
- data/lib/ar_transaction_changes/version.rb +3 -1
- data/test/models/user.rb +10 -0
- data/test/test_helper.rb +1 -0
- data/test/transaction_changes_test.rb +16 -0
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1b75ccc7bc393e60284a26e6077bdfc48e8032b52780770f0b762f292a6a3393
|
4
|
+
data.tar.gz: 2a048448c70283320792559fd7f7ae90d339384fda9fa3b9d437abf2607fd701
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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 =
|
41
|
+
old_value = _read_attribute_for_transaction(attr_name)
|
40
42
|
ret = yield
|
41
|
-
new_value =
|
43
|
+
new_value = _read_attribute_for_transaction(attr_name)
|
42
44
|
unless transaction_changed_attributes.key?(attr_name) || new_value == old_value
|
43
|
-
|
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
|
data/test/models/user.rb
CHANGED
@@ -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
|
data/test/test_helper.rb
CHANGED
@@ -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.
|
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-
|
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:
|