ar_transaction_changes 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +46 -0
- data/Rakefile +11 -0
- data/ar_transaction_changes.gemspec +23 -0
- data/lib/ar_transaction_changes.rb +37 -0
- data/lib/ar_transaction_changes/version.rb +3 -0
- data/test/database.yml.default +7 -0
- data/test/models/user.rb +14 -0
- data/test/test_helper.rb +22 -0
- data/test/transaction_changes_test.rb +69 -0
- metadata +94 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Shopify Inc.
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# ArTransactionChanges
|
2
|
+
|
3
|
+
Store transaction changes for active record objects so that they
|
4
|
+
are available in an after_commit callbacks.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'ar_transaction_changes'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install ar_transaction_changes
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
Just include ArTransactionChanges and transaction_changed_attributes
|
23
|
+
will store the original values for all the changed attributes
|
24
|
+
throughout the transaction.
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
class User < ActiveRecord::Base
|
28
|
+
include ArTransactionChanges
|
29
|
+
|
30
|
+
after_commit :print_transaction_changes
|
31
|
+
|
32
|
+
def print_transaction_changes
|
33
|
+
transaction_changed_attributes.each do |name, old_value|
|
34
|
+
puts "attribute #{name}: #{old_value.inspect} -> #{send(name).inspect}"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
```
|
39
|
+
|
40
|
+
## Contributing
|
41
|
+
|
42
|
+
1. Fork it
|
43
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
44
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
45
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
46
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'ar_transaction_changes/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "ar_transaction_changes"
|
8
|
+
gem.version = ArTransactionChanges::VERSION
|
9
|
+
gem.authors = ["Dylan Smith"]
|
10
|
+
gem.email = ["Dylan.Smith@shopify.com"]
|
11
|
+
gem.description = %q{Solves the problem of trying to get all the changes to an object during a transaction in an after_commit callbacks.}
|
12
|
+
gem.summary = %q{Store transaction changes for active record objects}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_dependency("activerecord", [">= 3.0.0"])
|
21
|
+
|
22
|
+
gem.add_development_dependency("mysql2")
|
23
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require "ar_transaction_changes/version"
|
2
|
+
|
3
|
+
module ArTransactionChanges
|
4
|
+
def create(*)
|
5
|
+
super.tap do |status|
|
6
|
+
store_transaction_changed_attributes if status != false
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def update(*)
|
11
|
+
super.tap do |status|
|
12
|
+
store_transaction_changed_attributes if status != false
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def committed!(*)
|
17
|
+
super
|
18
|
+
ensure
|
19
|
+
@transaction_changed_attributes = nil
|
20
|
+
end
|
21
|
+
|
22
|
+
def rolledback!(*)
|
23
|
+
super
|
24
|
+
ensure
|
25
|
+
@transaction_changed_attributes = nil
|
26
|
+
end
|
27
|
+
|
28
|
+
def transaction_changed_attributes
|
29
|
+
changed_attributes.merge(@transaction_changed_attributes || {})
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def store_transaction_changed_attributes
|
35
|
+
@transaction_changed_attributes = transaction_changed_attributes
|
36
|
+
end
|
37
|
+
end
|
data/test/models/user.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
class User < ActiveRecord::Base
|
2
|
+
include ArTransactionChanges
|
3
|
+
|
4
|
+
attr_accessor :stored_transaction_changes
|
5
|
+
|
6
|
+
after_commit :store_transaction_changes_for_tests
|
7
|
+
|
8
|
+
def store_transaction_changes_for_tests
|
9
|
+
@stored_transaction_changes = transaction_changed_attributes.reduce({}) do |changes, (attr_name, value)|
|
10
|
+
changes[attr_name] = [value, send(attr_name)]
|
11
|
+
changes
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
ENV["RAILS_ENV"] = "test"
|
2
|
+
require 'pathname'
|
3
|
+
require 'yaml'
|
4
|
+
require 'active_record'
|
5
|
+
require 'ar_transaction_changes'
|
6
|
+
require 'minitest/autorun'
|
7
|
+
|
8
|
+
test_dir = Pathname.new(File.dirname(__FILE__))
|
9
|
+
config_filename = test_dir.join("database.yml").exist? ? "database.yml" : "database.yml.default"
|
10
|
+
database_yml = YAML.load(test_dir.join(config_filename).read)
|
11
|
+
ActiveRecord::Base.configurations = database_yml
|
12
|
+
ActiveRecord::Base.establish_connection "test"
|
13
|
+
|
14
|
+
ActiveRecord::Base.connection.tap do |db|
|
15
|
+
db.drop_table(:users) if db.table_exists?(:users)
|
16
|
+
db.create_table(:users) do |t|
|
17
|
+
t.string :name
|
18
|
+
t.string :occupation
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
Dir[test_dir.join("models/*.rb")].each{ |file| require file }
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TransactionChangesTest < MiniTest::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@user = User.new(:name => "Dylan", :occupation => "Developer")
|
6
|
+
@user.save!
|
7
|
+
@user.stored_transaction_changes = nil
|
8
|
+
end
|
9
|
+
|
10
|
+
def teardown
|
11
|
+
User.delete_all
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_transaction_changes_for_create
|
15
|
+
user = User.new(:name => "Dylan")
|
16
|
+
user.save!
|
17
|
+
assert_equal [nil, "Dylan"], user.stored_transaction_changes["name"]
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_transaction_changes_for_update
|
21
|
+
@user.name = "Dillon"
|
22
|
+
@user.save!
|
23
|
+
assert_equal ["Dylan", "Dillon"], @user.stored_transaction_changes["name"]
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_transaction_changes_for_double_save
|
27
|
+
@user.transaction do
|
28
|
+
@user.name = "Dillon"
|
29
|
+
@user.save!
|
30
|
+
|
31
|
+
@user.occupation = "Tester"
|
32
|
+
@user.save!
|
33
|
+
end
|
34
|
+
assert_equal ["Dylan", "Dillon"], @user.stored_transaction_changes["name"]
|
35
|
+
assert_equal ["Developer", "Tester"], @user.stored_transaction_changes["occupation"]
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_transaction_changes_for_rollback
|
39
|
+
@user.transaction do
|
40
|
+
@user.name = "Dillon"
|
41
|
+
@user.save!
|
42
|
+
raise ActiveRecord::Rollback
|
43
|
+
end
|
44
|
+
assert_equal nil, @user.stored_transaction_changes
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_transaction_changes_after_reload_saved
|
48
|
+
@user.transaction do
|
49
|
+
@user.name = "Dillon"
|
50
|
+
@user.save!
|
51
|
+
|
52
|
+
@user.reload
|
53
|
+
end
|
54
|
+
assert_equal ["Dylan", "Dillon"], @user.stored_transaction_changes["name"]
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_transaction_changes_after_reload_unsaved
|
58
|
+
@user.transaction do
|
59
|
+
@user.name = "Dillon"
|
60
|
+
@user.save!
|
61
|
+
|
62
|
+
@user.name = "Andrew"
|
63
|
+
assert_equal "Dylan", @user.transaction_changed_attributes["name"]
|
64
|
+
@user.reload
|
65
|
+
end
|
66
|
+
assert_equal ["Dylan", "Dillon"], @user.stored_transaction_changes["name"]
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
metadata
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ar_transaction_changes
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Dylan Smith
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-14 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activerecord
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.0.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 3.0.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: mysql2
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: Solves the problem of trying to get all the changes to an object during
|
47
|
+
a transaction in an after_commit callbacks.
|
48
|
+
email:
|
49
|
+
- Dylan.Smith@shopify.com
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- .gitignore
|
55
|
+
- Gemfile
|
56
|
+
- LICENSE.txt
|
57
|
+
- README.md
|
58
|
+
- Rakefile
|
59
|
+
- ar_transaction_changes.gemspec
|
60
|
+
- lib/ar_transaction_changes.rb
|
61
|
+
- lib/ar_transaction_changes/version.rb
|
62
|
+
- test/database.yml.default
|
63
|
+
- test/models/user.rb
|
64
|
+
- test/test_helper.rb
|
65
|
+
- test/transaction_changes_test.rb
|
66
|
+
homepage: ''
|
67
|
+
licenses: []
|
68
|
+
post_install_message:
|
69
|
+
rdoc_options: []
|
70
|
+
require_paths:
|
71
|
+
- lib
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
|
+
none: false
|
80
|
+
requirements:
|
81
|
+
- - ! '>='
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
requirements: []
|
85
|
+
rubyforge_project:
|
86
|
+
rubygems_version: 1.8.23
|
87
|
+
signing_key:
|
88
|
+
specification_version: 3
|
89
|
+
summary: Store transaction changes for active record objects
|
90
|
+
test_files:
|
91
|
+
- test/database.yml.default
|
92
|
+
- test/models/user.rb
|
93
|
+
- test/test_helper.rb
|
94
|
+
- test/transaction_changes_test.rb
|