transactio 0.1.26
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +21 -0
- data/README.md +47 -0
- data/Rakefile +14 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/db/migrate/20210929064800_fix_object_changes.rb +17 -0
- data/lib/generators/transactio/install/USAGE +2 -0
- data/lib/generators/transactio/install/install_generator.rb +17 -0
- data/lib/generators/transactio/install/templates/create_transactio.rb.erb +24 -0
- data/lib/generators/transactio/migration_generator.rb +38 -0
- data/lib/transactio/active_record/helpers.rb +21 -0
- data/lib/transactio/active_record/transaction_loggable.rb +114 -0
- data/lib/transactio/frameworks/rails.rb +13 -0
- data/lib/transactio/models/application_record.rb +9 -0
- data/lib/transactio/models/transaction_log.rb +28 -0
- data/lib/transactio/models/transaction_log_entry.rb +44 -0
- data/lib/transactio/version.rb +5 -0
- data/lib/transactio.rb +25 -0
- data/transactio.gemspec +31 -0
- metadata +97 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f9f13892d8ad520b701b8ec0a16a09deebd163ee565b9fbfcf05b919947e4813
|
4
|
+
data.tar.gz: c214c0feb0ee56d567eaf7fdd904bed5aa9022ec8165eef7cb20aa09a78aaaa9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0eb63b45430d373f2422c6360ab574d02fe41d06529393dbbc2e312493fa2da51883ed3915afd700d41c281ce1c4f5db49bd29f3020222c5b3d1940a827eb8fe
|
7
|
+
data.tar.gz: '0074027279b01f941c65d0df2b1e3b6fd6b1c0c42eaba9d01ebc0f8704883de1508585ebe7bd98ea835b01f2e4c78eeb72f587f43f73d04d6e278b69d28ecb2b'
|
data/.gitignore
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at tom@degrunt.nl. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [https://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: https://contributor-covenant.org
|
74
|
+
[version]: https://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Tom de Grunt
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# Transactio
|
2
|
+
|
3
|
+
Transaction log engine for Rails.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'transactio'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install transactio
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Run `rails g transactio:install`
|
24
|
+
|
25
|
+
Run migrations
|
26
|
+
|
27
|
+
Add a `TransactionLog` table to your application, which inherits from `Transactio::TransactionLog`.
|
28
|
+
|
29
|
+
Add `transaction_loggable` to all your models you want logged.
|
30
|
+
|
31
|
+
## Development
|
32
|
+
|
33
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
34
|
+
|
35
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
36
|
+
|
37
|
+
## Contributing
|
38
|
+
|
39
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/transactio. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/transactio/blob/master/CODE_OF_CONDUCT.md).
|
40
|
+
|
41
|
+
## License
|
42
|
+
|
43
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
44
|
+
|
45
|
+
## Code of Conduct
|
46
|
+
|
47
|
+
Everyone interacting in the Transactio project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/transactio/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rake/testtask'
|
3
|
+
|
4
|
+
Rake::TestTask.new(:test) do |t|
|
5
|
+
t.libs << 'test'
|
6
|
+
t.libs << 'lib'
|
7
|
+
t.test_files = FileList['test/**/*_test.rb']
|
8
|
+
end
|
9
|
+
|
10
|
+
task :default => :test
|
11
|
+
|
12
|
+
# Adds the Auxilium semver task
|
13
|
+
spec = Gem::Specification.find_by_name 'auxilium'
|
14
|
+
load "#{spec.gem_dir}/lib/tasks/semver.rake"
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "transactio"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
class FixObjectChanges < ActiveRecord::Migration[6.0]
|
2
|
+
disable_ddl_transaction!
|
3
|
+
|
4
|
+
def up
|
5
|
+
return unless ActiveRecord::Base.connection.data_source_exists? 'transaction_log_entries'
|
6
|
+
|
7
|
+
while Transactio::TransactionLogEntry.where('jsonb_typeof(object_changes) = ?', 'array').exists? do
|
8
|
+
Transactio::TransactionLogEntry.where('jsonb_typeof(object_changes) = ?', 'array').limit(50000).find_each do |entry|
|
9
|
+
entry.object_changes = entry.object_changes.to_h
|
10
|
+
entry.save!
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def down
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require_relative '../migration_generator'
|
2
|
+
|
3
|
+
module Transactio
|
4
|
+
# Installs PaperTrail in a rails app.
|
5
|
+
class InstallGenerator < MigrationGenerator
|
6
|
+
source_root File.expand_path('templates', __dir__)
|
7
|
+
|
8
|
+
desc 'Generates (but does not run) a migration to add the transactio tables.'
|
9
|
+
|
10
|
+
def create_migration_file
|
11
|
+
add_transactio_migration(
|
12
|
+
'create_transactio'
|
13
|
+
)
|
14
|
+
# add_paper_trail_migration('add_object_changes_to_versions') if options.with_changes?
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
class CreateTransactio < ActiveRecord::Migration<%= migration_version %>
|
2
|
+
|
3
|
+
def change
|
4
|
+
create_table "transaction_log_entries", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
|
5
|
+
t.uuid "transaction_log_id"
|
6
|
+
t.string "transaction_loggable_type"
|
7
|
+
t.uuid "transaction_loggable_id"
|
8
|
+
t.datetime "created_at", precision: 6, null: false
|
9
|
+
t.datetime "updated_at", precision: 6, null: false
|
10
|
+
t.string "event"
|
11
|
+
t.string "from"
|
12
|
+
t.string "to"
|
13
|
+
t.jsonb "object"
|
14
|
+
t.jsonb "object_changes"
|
15
|
+
t.index ["transaction_log_id"], name: "index_transaction_log_entries_on_transaction_log_id"
|
16
|
+
t.index ["transaction_loggable_type", "transaction_loggable_id"], name: "transaction_loggable_index"
|
17
|
+
end
|
18
|
+
|
19
|
+
create_table "transaction_logs", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
|
20
|
+
t.datetime "created_at", precision: 6, null: false
|
21
|
+
t.datetime "updated_at", precision: 6, null: false
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rails/generators"
|
4
|
+
require "rails/generators/active_record"
|
5
|
+
|
6
|
+
module Transactio
|
7
|
+
# Basic structure to support a generator that builds a migration
|
8
|
+
class MigrationGenerator < ::Rails::Generators::Base
|
9
|
+
include ::Rails::Generators::Migration
|
10
|
+
|
11
|
+
def self.next_migration_number(dirname)
|
12
|
+
::ActiveRecord::Generators::Base.next_migration_number(dirname)
|
13
|
+
end
|
14
|
+
|
15
|
+
protected
|
16
|
+
|
17
|
+
def add_transactio_migration(template, extra_options = {})
|
18
|
+
migration_dir = File.expand_path("db/migrate")
|
19
|
+
if self.class.migration_exists?(migration_dir, template)
|
20
|
+
::Kernel.warn "Migration already exists: #{template}"
|
21
|
+
else
|
22
|
+
migration_template(
|
23
|
+
"#{template}.rb.erb",
|
24
|
+
"db/migrate/#{template}.rb",
|
25
|
+
{ migration_version: migration_version }.merge(extra_options)
|
26
|
+
)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def migration_version
|
31
|
+
format(
|
32
|
+
"[%d.%d]",
|
33
|
+
::ActiveRecord::VERSION::MAJOR,
|
34
|
+
::ActiveRecord::VERSION::MINOR
|
35
|
+
)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_support/concern'
|
4
|
+
|
5
|
+
module Transactio
|
6
|
+
module ActiveRecord
|
7
|
+
module Helpers
|
8
|
+
extend ActiveSupport::Concern
|
9
|
+
|
10
|
+
class_methods do
|
11
|
+
def transaction_loggable
|
12
|
+
include Transactio::ActiveRecord::TransactionLoggable
|
13
|
+
end
|
14
|
+
|
15
|
+
def transaction_loggable?
|
16
|
+
included_modules.include?(Transactio::ActiveRecord::TransactionLoggable)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,114 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_support/concern'
|
4
|
+
|
5
|
+
module Transactio
|
6
|
+
module ActiveRecord
|
7
|
+
module TransactionLoggable
|
8
|
+
extend ActiveSupport::Concern
|
9
|
+
|
10
|
+
included do
|
11
|
+
has_many :transaction_log_entries, as: :transaction_loggable, class_name: 'Transactio::TransactionLogEntry'
|
12
|
+
|
13
|
+
# Only do this if we actually use a state machine
|
14
|
+
if respond_to?(:state_machines)
|
15
|
+
state_machine.after_transition do |object, transition|
|
16
|
+
if object.class.has_state_machines_key_columns?
|
17
|
+
transaction_log = ::TransactionLog.current!
|
18
|
+
transaction_log.entries
|
19
|
+
.create!(transaction_loggable: object, event: transition.event, from: transition.from, to: transition.to)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# Used to retain data about the original object
|
25
|
+
before_save :retain_transaction_log_attributes
|
26
|
+
before_destroy :retain_transaction_log_attributes_for_destroy
|
27
|
+
|
28
|
+
# Callbacks to actually store the log entries
|
29
|
+
after_save :register_transaction_log
|
30
|
+
after_destroy :register_transaction_log
|
31
|
+
|
32
|
+
def retain_transaction_log_attributes
|
33
|
+
@transaction_loggable_event = new_record? ? 'create' : 'update'
|
34
|
+
unless @transaction_loggable_event == 'create'
|
35
|
+
@transaction_loggable_attributes = attributes.merge(attributes_in_database)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def retain_transaction_log_attributes_for_destroy
|
40
|
+
@transaction_loggable_event = 'destroy'
|
41
|
+
@transaction_loggable_attributes = attributes
|
42
|
+
end
|
43
|
+
|
44
|
+
def register_transaction_log
|
45
|
+
transaction_log = ::TransactionLog.current!
|
46
|
+
transaction_log.entries
|
47
|
+
.create!(transaction_loggable: self,
|
48
|
+
object: @transaction_loggable_attributes,
|
49
|
+
object_changes: saved_changes,
|
50
|
+
event: @transaction_loggable_event)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
class_methods do
|
55
|
+
def has_state_machines_key_columns?
|
56
|
+
return false unless respond_to?(:state_machines)
|
57
|
+
|
58
|
+
state_machines.keys.all? { |key| column_names.include?(key.to_s) }
|
59
|
+
end
|
60
|
+
|
61
|
+
def in_transaction_log(transaction_log)
|
62
|
+
transaction_log = ::TransactionLog.find(transaction_log) if transaction_log.is_a?(String)
|
63
|
+
|
64
|
+
where(
|
65
|
+
id: transaction_log.entries
|
66
|
+
.where(transaction_loggable_type: name)
|
67
|
+
.select(:transaction_loggable_id)
|
68
|
+
)
|
69
|
+
end
|
70
|
+
|
71
|
+
def insert_all!(attributes, returning: nil)
|
72
|
+
records = super(attributes, returning: attribute_names)
|
73
|
+
create_transaction_log_entries(records)
|
74
|
+
|
75
|
+
records
|
76
|
+
end
|
77
|
+
|
78
|
+
def insert_all(attributes, returning: nil, unique_by: nil)
|
79
|
+
records = super(attributes, returning: attribute_names, unique_by: unique_by)
|
80
|
+
create_transaction_log_entries(records)
|
81
|
+
|
82
|
+
records
|
83
|
+
end
|
84
|
+
|
85
|
+
def upsert_all(attributes, returning: nil, unique_by: nil)
|
86
|
+
records = super(attributes, returning: attribute_names, unique_by: unique_by)
|
87
|
+
create_transaction_log_entries(records)
|
88
|
+
|
89
|
+
records
|
90
|
+
end
|
91
|
+
|
92
|
+
def upsert(attributes, returning: nil, unique_by: nil)
|
93
|
+
upsert_all([attributes], returning: returning, unique_by: unique_by)
|
94
|
+
end
|
95
|
+
|
96
|
+
def insert!(attributes, returning: nil)
|
97
|
+
insert_all([attributes], returning: returning)
|
98
|
+
end
|
99
|
+
|
100
|
+
def insert(attributes, returning: nil, unique_by: nil)
|
101
|
+
insert_all!([attributes], returning: returning, unique_by: unique_by)
|
102
|
+
end
|
103
|
+
|
104
|
+
private
|
105
|
+
|
106
|
+
def create_transaction_log_entries(records)
|
107
|
+
now = Time.now
|
108
|
+
transaction_attributes = records.map { |r| { transaction_log_id: ::TransactionLog.current!.id, transaction_loggable_id: r['id'], transaction_loggable_type: name, created_at: now, updated_at: now, object_changes: r.map { |k, v| [k, [nil, v]] }.to_h } }
|
109
|
+
Transactio::TransactionLogEntry.upsert_all(transaction_attributes)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module Transactio
|
2
|
+
module Rails
|
3
|
+
class Engine < ::Rails::Engine
|
4
|
+
initializer :append_migrations do |app|
|
5
|
+
unless app.root.to_s.match root.to_s
|
6
|
+
config.paths['db/migrate'].expanded.each do |expanded_path|
|
7
|
+
app.config.paths['db/migrate'] << expanded_path
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Transactio
|
4
|
+
class TransactionLog < ApplicationRecord
|
5
|
+
self.abstract_class = true
|
6
|
+
|
7
|
+
has_many :entries, class_name: 'Transactio::TransactionLogEntry'
|
8
|
+
|
9
|
+
after_rollback :clear_current!
|
10
|
+
after_commit :clear_current!
|
11
|
+
|
12
|
+
default_scope -> { order(created_at: :desc) }
|
13
|
+
|
14
|
+
def self.current!
|
15
|
+
Current.transaction_log ||= create!
|
16
|
+
end
|
17
|
+
|
18
|
+
def name
|
19
|
+
id.split('-').first.upcase
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def clear_current!
|
25
|
+
Current.transaction_log = nil
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_dependency 'transactio/models/application_record'
|
4
|
+
|
5
|
+
module Transactio
|
6
|
+
class TransactionLogEntry < Transactio::ApplicationRecord
|
7
|
+
belongs_to :transaction_log, class_name: '::TransactionLog'
|
8
|
+
belongs_to :transaction_loggable, polymorphic: true
|
9
|
+
|
10
|
+
default_scope -> { order(created_at: :desc) }
|
11
|
+
|
12
|
+
scope :ascending, -> { reorder(created_at: :asc) }
|
13
|
+
scope :descending, -> { reorder(created_at: :desc) }
|
14
|
+
scope :with_object_change_from, lambda { |field, change_from|
|
15
|
+
where("object_changes -> '#{field}' ->> 0 = ?", change_from)
|
16
|
+
}
|
17
|
+
scope :with_object_change_to, ->(field, change_to) { where("object_changes -> '#{field}' ->> 1 = ?", change_to) }
|
18
|
+
scope :with_object_change_not_to, ->(field, change_not_to) { where("object_changes -> '#{field}' ->> 1 != ?", change_not_to) }
|
19
|
+
|
20
|
+
def object_change?(attribute)
|
21
|
+
return false if object_change_from(attribute) == object_change_to(attribute)
|
22
|
+
|
23
|
+
object_changes.key?(attribute.to_s)
|
24
|
+
end
|
25
|
+
|
26
|
+
def object_change(attribute)
|
27
|
+
return nil if object_change_from(attribute) == object_change_to(attribute)
|
28
|
+
|
29
|
+
object_changes[attribute.to_s]
|
30
|
+
end
|
31
|
+
|
32
|
+
def object_change_from(attribute)
|
33
|
+
return nil if object_changes.nil?
|
34
|
+
|
35
|
+
object_changes[attribute.to_s]&.first
|
36
|
+
end
|
37
|
+
|
38
|
+
def object_change_to(attribute)
|
39
|
+
return nil if object_changes.nil?
|
40
|
+
|
41
|
+
object_changes[attribute.to_s]&.last
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/lib/transactio.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'transactio/version'
|
2
|
+
require 'transactio/active_record/transaction_loggable'
|
3
|
+
require 'transactio/active_record/helpers'
|
4
|
+
require 'transactio/models/application_record'
|
5
|
+
require 'transactio/models/transaction_log'
|
6
|
+
require 'transactio/models/transaction_log_entry'
|
7
|
+
|
8
|
+
module Transactio
|
9
|
+
class Error < StandardError; end
|
10
|
+
end
|
11
|
+
|
12
|
+
ActiveSupport.on_load(:active_record) do
|
13
|
+
include Transactio::ActiveRecord::Helpers
|
14
|
+
end
|
15
|
+
|
16
|
+
# Require frameworks
|
17
|
+
if defined?(::Rails)
|
18
|
+
# Rails module is sometimes defined by gems like rails-html-sanitizer
|
19
|
+
# so we check for presence of Rails.application.
|
20
|
+
if defined?(::Rails.application)
|
21
|
+
require 'transactio/frameworks/rails'
|
22
|
+
else
|
23
|
+
::Kernel.warn(::PaperTrail::E_RAILS_NOT_LOADED)
|
24
|
+
end
|
25
|
+
end
|
data/transactio.gemspec
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require_relative 'lib/transactio/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "transactio"
|
5
|
+
spec.version = Transactio::VERSION
|
6
|
+
spec.authors = ["Tom de Grunt"]
|
7
|
+
spec.email = ["tom@degrunt.nl"]
|
8
|
+
|
9
|
+
spec.summary = %q{Within one transaction, track changes to your models and see all state transitions.}
|
10
|
+
spec.description = %q{Within one transaction, track changes to your models and see all state transitions.}
|
11
|
+
spec.homepage = "https://code.entropydecelerator.com/components/transactio"
|
12
|
+
spec.license = "MIT"
|
13
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
14
|
+
|
15
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
16
|
+
spec.metadata["source_code_uri"] = "https://code.entropydecelerator.com/components/transactio"
|
17
|
+
spec.metadata["changelog_uri"] = "https://code.entropydecelerator.com/components/transactio"
|
18
|
+
|
19
|
+
# Specify which files should be added to the gem when it is released.
|
20
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
21
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
22
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
23
|
+
end
|
24
|
+
spec.bindir = "exe"
|
25
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
26
|
+
spec.require_paths = ["lib"]
|
27
|
+
|
28
|
+
spec.add_dependency "activerecord", ">= 6"
|
29
|
+
|
30
|
+
spec.add_development_dependency 'auxilium'
|
31
|
+
end
|
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: transactio
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.26
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tom de Grunt
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-11-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '6'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: auxilium
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: Within one transaction, track changes to your models and see all state
|
42
|
+
transitions.
|
43
|
+
email:
|
44
|
+
- tom@degrunt.nl
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- ".gitignore"
|
50
|
+
- CODE_OF_CONDUCT.md
|
51
|
+
- Gemfile
|
52
|
+
- LICENSE.txt
|
53
|
+
- README.md
|
54
|
+
- Rakefile
|
55
|
+
- bin/console
|
56
|
+
- bin/setup
|
57
|
+
- db/migrate/20210929064800_fix_object_changes.rb
|
58
|
+
- lib/generators/transactio/install/USAGE
|
59
|
+
- lib/generators/transactio/install/install_generator.rb
|
60
|
+
- lib/generators/transactio/install/templates/create_transactio.rb.erb
|
61
|
+
- lib/generators/transactio/migration_generator.rb
|
62
|
+
- lib/transactio.rb
|
63
|
+
- lib/transactio/active_record/helpers.rb
|
64
|
+
- lib/transactio/active_record/transaction_loggable.rb
|
65
|
+
- lib/transactio/frameworks/rails.rb
|
66
|
+
- lib/transactio/models/application_record.rb
|
67
|
+
- lib/transactio/models/transaction_log.rb
|
68
|
+
- lib/transactio/models/transaction_log_entry.rb
|
69
|
+
- lib/transactio/version.rb
|
70
|
+
- transactio.gemspec
|
71
|
+
homepage: https://code.entropydecelerator.com/components/transactio
|
72
|
+
licenses:
|
73
|
+
- MIT
|
74
|
+
metadata:
|
75
|
+
homepage_uri: https://code.entropydecelerator.com/components/transactio
|
76
|
+
source_code_uri: https://code.entropydecelerator.com/components/transactio
|
77
|
+
changelog_uri: https://code.entropydecelerator.com/components/transactio
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: 2.3.0
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubygems_version: 3.3.7
|
94
|
+
signing_key:
|
95
|
+
specification_version: 4
|
96
|
+
summary: Within one transaction, track changes to your models and see all state transitions.
|
97
|
+
test_files: []
|