ar_transaction_changes 1.1.7 → 1.1.9

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: 1b75ccc7bc393e60284a26e6077bdfc48e8032b52780770f0b762f292a6a3393
4
- data.tar.gz: 2a048448c70283320792559fd7f7ae90d339384fda9fa3b9d437abf2607fd701
3
+ metadata.gz: 5871403ece76d3c6fceb41b1a5aecd240d5460363e0d86a88f4b27ba4fc1d562
4
+ data.tar.gz: fda845379838e83afc84c11bc23005f4af0ac8e12df804c48c5010d724b52ba3
5
5
  SHA512:
6
- metadata.gz: f3148c75a2a2bebc626451d1cbfbb7263cb1e39a56571af89761dc1f5b96a3a988de2a97ee2bef546744765f2b3eb569de77ce45fea5e7da1289821231435076
7
- data.tar.gz: e21dff998d73c00ee6f6b8b7d92d9814412bf3421b8dc009e41429b7fc33e014cd9cd4f01d05288bc5009fc530749628de7d19c6e97973c53a3ddd37bc8f9cd0
6
+ metadata.gz: 386cd79e7207afdc17fc9f019183fe14674c5a3af5fe6b35c87433417bfe0ce94573abbe43954f46812e22911051aa3ab80a8d47ddee7a78b6f5801b6d10fef2
7
+ data.tar.gz: 628f82d89b3c54455dc9c5071e9beb1610deb710bbfa158f616d886b04c9136f446bc25ad04d8f1ba9e6f3096dd5c93f64340a207cc4541f53e6420ab74db115
@@ -0,0 +1,44 @@
1
+ name: CI
2
+
3
+ on:
4
+ push: {}
5
+ pull_request:
6
+ types: [opened, synchronize]
7
+
8
+ jobs:
9
+ build:
10
+ if: github.event_name == 'push' || github.event.pull_request.head.repo.owner.login != 'dylanahsmith'
11
+
12
+ runs-on: ubuntu-latest
13
+
14
+ strategy:
15
+ matrix:
16
+ entry:
17
+ - name: 'Minimum supported'
18
+ ruby: 2.7
19
+ gemfile: Gemfile.rails6
20
+ - name: "Latest released versions"
21
+ ruby: 3.2
22
+ gemfile: Gemfile
23
+ - name: "Rails main branch"
24
+ ruby: 3.2
25
+ gemfile: Gemfile.rails_head
26
+
27
+ name: ${{ matrix.entry.name }}
28
+
29
+ env:
30
+ BUNDLE_GEMFILE: ${{ matrix.entry.gemfile }}
31
+
32
+ steps:
33
+ - name: Install required packages
34
+ run: |
35
+ sudo apt-get update
36
+ sudo apt-get -y install libsqlite3-dev
37
+ - uses: actions/checkout@v3
38
+ - name: Set up Ruby
39
+ uses: ruby/setup-ruby@v1
40
+ with:
41
+ ruby-version: ${{ matrix.entry.ruby }}
42
+ bundler-cache: true
43
+ - name: Run tests
44
+ run: bundle exec rake
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  gemspec
4
+
5
+ gem 'sqlite3', '~> 1.4'
data/Gemfile.rails6 ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem 'activerecord', '~> 6.0.0'
4
+ gem "sqlite3", "~> 1.3", ">= 1.3.6"
5
+
6
+ gemspec
data/Gemfile.rails_head CHANGED
@@ -1,5 +1,6 @@
1
1
  source "https://rubygems.org"
2
2
 
3
3
  gem 'activerecord', github: 'rails/rails'
4
+ gem 'sqlite3', '~> 1.4'
4
5
 
5
6
  gemspec
data/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # ArTransactionChanges
2
- [![Build Status](https://travis-ci.org/dylanahsmith/ar_transaction_changes.png?branch=master)](https://travis-ci.org/dylanahsmith/ar_transaction_changes)
2
+ [![Build Status](https://github.com/dylanahsmith/ar_transaction_changes/workflows/CI/badge.svg?branch=main)](https://github.com/dylanahsmith/ar_transaction_changes/actions?query=branch%3Amain)
3
3
 
4
4
  Store all attribute changes for active record objects during a
5
5
  transaction so that they are available in an after_commit callbacks.
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ArTransactionChanges
4
- VERSION = "1.1.7"
4
+ VERSION = "1.1.9"
5
5
  end
@@ -34,21 +34,43 @@ module ArTransactionChanges
34
34
  end
35
35
  end
36
36
 
37
+ def attribute_will_change!(attr_name)
38
+ unless transaction_changed_attributes.key?(attr_name)
39
+ value = _read_attribute_for_transaction(attr_name)
40
+ value = _deserialize_transaction_change_value(attr_name, value)
41
+ transaction_changed_attributes[attr_name] = value
42
+ end
43
+ super
44
+ end
45
+
37
46
  private
38
47
 
48
+ def init_internals
49
+ super
50
+ @transaction_changed_attributes = nil
51
+ end
52
+
53
+ def _deserialize_transaction_change_value(attr_name, value)
54
+ attribute = @attributes[attr_name]
55
+ return value unless attribute.type.is_a?(::ActiveRecord::Type::Serialized)
56
+ attribute.type.deserialize(value)
57
+ end
58
+
39
59
  def _store_transaction_changed_attributes(attr_name)
40
60
  attr_name = attr_name.to_s
41
61
  old_value = _read_attribute_for_transaction(attr_name)
42
62
  ret = yield
43
63
  new_value = _read_attribute_for_transaction(attr_name)
44
- unless transaction_changed_attributes.key?(attr_name) || new_value == 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
64
+ if !transaction_changed_attributes.key?(attr_name) && new_value != old_value
65
+ transaction_changed_attributes[attr_name] = _deserialize_transaction_change_value(attr_name, old_value)
66
+ elsif transaction_changed_attributes.key?(attr_name)
67
+ new_value = _deserialize_transaction_change_value(attr_name, new_value)
68
+
69
+ stored_value = transaction_changed_attributes[attr_name]
70
+
71
+ if new_value == stored_value
72
+ transaction_changed_attributes.delete(attr_name)
50
73
  end
51
- transaction_changed_attributes
52
74
  end
53
75
  ret
54
76
  end
@@ -1,7 +1,3 @@
1
1
  test:
2
- adapter: mysql2
3
- database: ar_transaction_changes_test
4
- host: 127.0.0.1
5
- encoding: utf8
6
- username: root
7
- password:
2
+ adapter: sqlite3
3
+ database: ':memory:'
data/test/models/user.rb CHANGED
@@ -10,6 +10,7 @@ class User < ActiveRecord::Base
10
10
  end
11
11
 
12
12
  serialize :connection_details, Array
13
+ serialize :notes, Array
13
14
 
14
15
  attr_accessor :stored_transaction_changes
15
16
 
data/test/test_helper.rb CHANGED
@@ -10,8 +10,10 @@ config_filename = test_dir.join("database.yml").exist? ? "database.yml" : "datab
10
10
  database_yml = YAML.load(test_dir.join(config_filename).read)
11
11
  database_config = database_yml.fetch("test")
12
12
 
13
- ActiveRecord::Base.establish_connection(database_config.except("database"))
14
- ActiveRecord::Base.connection.recreate_database(database_config.fetch("database"))
13
+ if database_config.fetch('adapter') != 'sqlite3'
14
+ ActiveRecord::Base.establish_connection(database_config.except("database"))
15
+ ActiveRecord::Base.connection.recreate_database(database_config.fetch("database"))
16
+ end
15
17
  ActiveRecord::Base.establish_connection(database_config)
16
18
 
17
19
  ActiveRecord::Base.connection.tap do |db|
@@ -20,8 +22,16 @@ ActiveRecord::Base.connection.tap do |db|
20
22
  t.string :occupation
21
23
  t.integer :age
22
24
  t.text :connection_details
25
+ t.text :notes
23
26
  t.timestamps null: false
24
27
  end
25
28
  end
26
29
 
27
30
  Dir[test_dir.join("models/*.rb")].each{ |file| require file }
31
+
32
+ serializable_classes = [User::ConnectionDetails]
33
+ if ActiveRecord::VERSION::MAJOR >= 7
34
+ ActiveRecord.yaml_column_permitted_classes += serializable_classes
35
+ else
36
+ ActiveRecord::Base.yaml_column_permitted_classes += serializable_classes
37
+ end
@@ -126,4 +126,52 @@ class TransactionChangesTest < MiniTest::Unit::TestCase
126
126
  @user.save!
127
127
  assert_equal '2.2.2.2', @user.connection_details.first.client_ip
128
128
  end
129
+
130
+ def test_double_save_back_to_original_value
131
+ @user.transaction do
132
+ @user.name = "Dillon"
133
+ @user.save!(touch: false)
134
+
135
+ @user.name = "Dylan"
136
+ @user.save!(touch: false)
137
+ end
138
+
139
+ assert_empty @user.stored_transaction_changes
140
+ end
141
+
142
+ def test_double_save_back_to_original_value_for_serialized_attribute
143
+ @user.notes = ['a', 'b']
144
+ @user.save!
145
+
146
+ @user.transaction do
147
+ @user.notes = ['b', 'c']
148
+ @user.save!(touch: false)
149
+
150
+ @user.notes = ['a', 'b']
151
+ @user.save!(touch: false)
152
+ end
153
+
154
+ assert_empty @user.stored_transaction_changes
155
+ end
156
+
157
+ def test_mutating_serialized_attribute_in_place_with_attribute_will_change
158
+ @user.notes_will_change!
159
+ @user.notes.push('a')
160
+ @user.save!
161
+
162
+ assert_equal [[], ['a']], @user.stored_transaction_changes['notes']
163
+ end
164
+
165
+ def test_double_modification_with_attribute_will_change
166
+ @user.transaction do
167
+ @user.notes = ['a']
168
+ @user.save!
169
+
170
+ @user.notes_will_change!
171
+ @user.notes.push('b')
172
+ @user.save!
173
+ end
174
+
175
+ assert_equal [[], ['a', 'b']], @user.stored_transaction_changes['notes']
176
+ end
129
177
  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.7
4
+ version: 1.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dylan Thacker-Smith
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-01 00:00:00.000000000 Z
11
+ date: 2023-01-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -60,10 +60,10 @@ executables: []
60
60
  extensions: []
61
61
  extra_rdoc_files: []
62
62
  files:
63
+ - ".github/workflows/ci.yml"
63
64
  - ".gitignore"
64
- - ".travis.yml"
65
65
  - Gemfile
66
- - Gemfile.rails52
66
+ - Gemfile.rails6
67
67
  - Gemfile.rails_head
68
68
  - LICENSE.txt
69
69
  - README.md
@@ -94,7 +94,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
94
94
  - !ruby/object:Gem::Version
95
95
  version: '0'
96
96
  requirements: []
97
- rubygems_version: 3.1.2
97
+ rubygems_version: 3.4.1
98
98
  signing_key:
99
99
  specification_version: 4
100
100
  summary: Store transaction changes for active record objects
data/.travis.yml DELETED
@@ -1,17 +0,0 @@
1
- language: ruby
2
- sudo: false
3
-
4
- jobs:
5
- include:
6
- - name: "Minimum supported versions"
7
- rvm: 2.4
8
- gemfile: Gemfile.rails52
9
- - name: "Latest released versions"
10
- rvm: 2.7
11
- gemfile: Gemfile
12
- - name: "Rails master branch"
13
- rvm: 2.7
14
- gemfile: Gemfile.rails_head
15
-
16
- services:
17
- - mysql
data/Gemfile.rails52 DELETED
@@ -1,5 +0,0 @@
1
- source "https://rubygems.org"
2
-
3
- gem 'activerecord', '~> 5.2.0'
4
-
5
- gemspec