state_machines_transactions 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a0df940ae92db6dac8d0cdcbda69b06ac91748ee9db4ec136a11e96779f98ed3
4
+ data.tar.gz: 6224e37b1ca28eeff4c42c1b90a20b546e1c8b26255a34d0f86b01cc9cd55c0f
5
+ SHA512:
6
+ metadata.gz: 43396d8b274822694745257268621cbc8e49e6afc1f1d539ed3c2c07c247f46b01d10aafbe7ba742c34b6af01eacc3983dc4b23685bd51d983be46559d68f366
7
+ data.tar.gz: b5c4db4df5d99d88a4e3f16ec5171fa5613bde6c81f77a9fff6d2a602a24b41f541b78b4aee0fbf163cf6747635c793e82d567a64fa4e3b17c657d5c4e0165ad
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2023 Abhilash Achuthan
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # StateMachinesTransactions
2
+ This gem persists the state transitions of state_machines gem. The transactions (transition data) can be used to represent the path a stateful object took to reach the current state.
3
+
4
+ ## Installation
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem "state_machines_transactions"
9
+ ```
10
+
11
+ And then execute:
12
+ ```bash
13
+ $ bundle
14
+ ```
15
+
16
+ Or install it yourself as:
17
+ ```bash
18
+ $ gem install state_machines_transactions
19
+ ```
20
+
21
+ ## Usage
22
+ Generate the database migration for state_machines_transactions table using:
23
+ ```bash
24
+ $ rails generate state_machine_transactions
25
+ ```
26
+
27
+ And then execute:
28
+ ```bash
29
+ $ rails db:migrate
30
+ ```
31
+
32
+ Add `audit_transition` inside your state machine to persist the state transitions.
33
+ ```ruby
34
+ state_machine :state, initial: :parked do
35
+ audit_transition
36
+
37
+ state :parked
38
+ state :idling
39
+ end
40
+ ```
41
+
42
+ Use `StateMachinesTransaction` model to fetch the transactions. The schema is:
43
+ ```ruby
44
+ create_table "state_machines_transactions", force: :cascade do |t|
45
+ t.string "object_type"
46
+ t.integer "object_id"
47
+ t.string "state_field"
48
+ t.string "event"
49
+ t.string "from_state"
50
+ t.string "to_state"
51
+ t.string "status"
52
+ t.datetime "created_at", null: false
53
+ t.datetime "updated_at", null: false
54
+ end
55
+ ```
56
+
57
+ ## Contributing
58
+ 1. Fork it ( https://github.com/infraspecdev/state_machines_transactions/fork )
59
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
60
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
61
+ 4. Push to the branch (`git push origin my-new-feature`)
62
+ 5. Create a new Pull Request
63
+
64
+ ## License
65
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ require "bundler/setup"
2
+
3
+ require "bundler/gem_tasks"
@@ -0,0 +1,19 @@
1
+ require 'rails/generators/migration'
2
+
3
+ class StateMachineTransactionsGenerator < Rails::Generators::Base
4
+ include Rails::Generators::Migration
5
+
6
+ class << self
7
+ def source_root
8
+ @_state_machine_transactions_source_root ||= File.expand_path('../templates', __FILE__)
9
+ end
10
+
11
+ def next_migration_number path
12
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
13
+ end
14
+ end
15
+
16
+ def create_model_file
17
+ migration_template 'create_state_machines_transactions.rb', 'db/migrate/create_state_machines_transactions.rb'
18
+ end
19
+ end
@@ -0,0 +1,15 @@
1
+ class CreateStateMachinesTransactions < ActiveRecord::Migration[7.0]
2
+ def change
3
+ create_table :state_machines_transactions do |t|
4
+ t.string :object_type
5
+ t.integer :object_id
6
+ t.string :state_field
7
+ t.string :event
8
+ t.string :from_state
9
+ t.string :to_state
10
+ t.string :status
11
+
12
+ t.timestamps
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,7 @@
1
+ class CreateTransactionJob < ActiveJob::Base
2
+ queue_as :default
3
+
4
+ def perform(attributes)
5
+ StateMachinesTransaction.create(attributes)
6
+ end
7
+ end
@@ -0,0 +1,8 @@
1
+ class StateMachinesTransaction < ActiveRecord::Base
2
+ enum status: { success: 'success', failure: 'failure' }
3
+ STATUS_COLOR = OpenStruct.new(success: 'green', failure: 'red')
4
+
5
+ def status_color
6
+ @_status_color ||= self.success? ? STATUS_COLOR.success : STATUS_COLOR.failure
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ module StateMachinesTransactions
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,36 @@
1
+ require "state_machines"
2
+ require "state_machines-activerecord"
3
+ require "state_machines_transactions/state_machines_transaction"
4
+ require "state_machines_transactions/create_transaction_job"
5
+
6
+ module StateMachinesTransactions
7
+ def audit_transition
8
+ state_machine = self
9
+ state_machine.after_transition(any => any) do |object, _transition|
10
+ state_transaction = StateMachinesTransaction.create(
11
+ object_type: object.class.name,
12
+ object_id: object.id,
13
+ state_field: state_machine.attribute,
14
+ event: _transition.event,
15
+ from_state: _transition.from,
16
+ to_state: _transition.to,
17
+ status: 'success'
18
+ )
19
+ end
20
+
21
+ state_machine.after_failure do |object, _transition|
22
+ state_transaction = StateMachinesTransaction.new(
23
+ object_type: object.class.name,
24
+ object_id: object.id,
25
+ state_field: state_machine.attribute,
26
+ event: _transition.event,
27
+ from_state: _transition.from,
28
+ to_state: _transition.to,
29
+ status: 'failure'
30
+ )
31
+ CreateTransactionJob.perform_later(state_transaction.attributes)
32
+ end
33
+ end
34
+ end
35
+
36
+ StateMachines::Machine.include StateMachinesTransactions
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: state_machines_transactions
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Abhilash Achuthan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-07-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 7.0.6
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 7.0.6
27
+ - !ruby/object:Gem::Dependency
28
+ name: state_machines
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.6.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.6.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: state_machines-activerecord
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.9.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.9.0
55
+ description: Description of StateMachinesTransactions.
56
+ email:
57
+ - abhilash@infraspec.dev
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - MIT-LICENSE
63
+ - README.md
64
+ - Rakefile
65
+ - lib/generators/state_machine_transactions_generator.rb
66
+ - lib/generators/templates/create_state_machines_transactions.rb
67
+ - lib/state_machines_transactions.rb
68
+ - lib/state_machines_transactions/create_transaction_job.rb
69
+ - lib/state_machines_transactions/state_machines_transaction.rb
70
+ - lib/state_machines_transactions/version.rb
71
+ homepage: https://github.com/infraspecdev/state_machines_transactions
72
+ licenses:
73
+ - MIT
74
+ metadata:
75
+ homepage_uri: https://github.com/infraspecdev/state_machines_transactions
76
+ source_code_uri: https://github.com/infraspecdev/state_machines_transactions
77
+ changelog_uri: https://github.com/infraspecdev/state_machines_transactions
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: '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.4.15
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Summary of StateMachinesTransactions.
97
+ test_files: []