state_transitions 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +56 -0
- data/Rakefile +8 -0
- data/app/models/state_transitions/application_record.rb +5 -0
- data/app/models/state_transitions/current.rb +3 -0
- data/app/models/state_transitions/state_transition.rb +9 -0
- data/config/routes.rb +2 -0
- data/db/migrate/20221017084844_create_state_transitions.rb +11 -0
- data/lib/state_transitions/engine.rb +5 -0
- data/lib/state_transitions/version.rb +3 -0
- data/lib/state_transitions.rb +69 -0
- data/lib/tasks/state_transitions_tasks.rake +4 -0
- metadata +86 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5ef2f65947ee5d807bdb938c6938eb419464640429d37003e357482d15226b8c
|
4
|
+
data.tar.gz: 64b5b628b9cafa63434b76e9fbc1fe4231a49a6d83e84db7283075812350e021
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5cd733d4d500d471d08346bffd84856c8a95357f1d9793b679d08cdc171e0d1a780774d530ed4aa5ff70b19d7594ebc6fff547b7a00b2626994e79a5e45d6139
|
7
|
+
data.tar.gz: 7555504b497d804229ca92f1196f9768c8227b8e3ea9ce6e304d4148cd981912a1fe5a1fee63f7c58907f69d0afc0553388ac5aac0703c5fbb6c42268263780e
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2022 kaspernj
|
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,56 @@
|
|
1
|
+
# StateTransitions
|
2
|
+
Short description and motivation.
|
3
|
+
|
4
|
+
## Usage
|
5
|
+
How to use my plugin.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem "state_transitions"
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
```bash
|
16
|
+
bundle
|
17
|
+
```
|
18
|
+
|
19
|
+
Install migrations:
|
20
|
+
```bash
|
21
|
+
rails state_transitions:install:migrations
|
22
|
+
```
|
23
|
+
|
24
|
+
Run migrations:
|
25
|
+
```bash
|
26
|
+
rails db:migrate
|
27
|
+
```
|
28
|
+
|
29
|
+
Include the module in a model:
|
30
|
+
```ruby
|
31
|
+
class Project < ApplicationRecord
|
32
|
+
include StateTransitions
|
33
|
+
end
|
34
|
+
```
|
35
|
+
|
36
|
+
Track current user on state transitions from requests:
|
37
|
+
```ruby
|
38
|
+
class ApplicationController
|
39
|
+
around_action :set_state_transitions_current
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def set_state_transitions_current
|
44
|
+
StateTransitions::Current.user = current_user
|
45
|
+
yield
|
46
|
+
ensure
|
47
|
+
StateTransitions::Current.user = nil
|
48
|
+
end
|
49
|
+
end
|
50
|
+
```
|
51
|
+
|
52
|
+
## Contributing
|
53
|
+
Contribution directions go here.
|
54
|
+
|
55
|
+
## License
|
56
|
+
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,9 @@
|
|
1
|
+
class StateTransitions::StateTransition < ApplicationRecord
|
2
|
+
self.table_name = "state_transitions"
|
3
|
+
|
4
|
+
belongs_to :resource, polymorphic: true
|
5
|
+
belongs_to :user, optional: true, polymorphic: true
|
6
|
+
|
7
|
+
validates :state_from, presence: true, if: :persisted?
|
8
|
+
validates :state_to, presence: true
|
9
|
+
end
|
data/config/routes.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
class CreateStateTransitions < ActiveRecord::Migration[7.0]
|
2
|
+
def change
|
3
|
+
create_table :state_transitions do |t|
|
4
|
+
t.references :resource, polymorphic: true, null: false
|
5
|
+
t.references :user, polymorphic: true
|
6
|
+
t.string :state_from, null: false
|
7
|
+
t.string :state_to, null: false
|
8
|
+
t.timestamps
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require "state_transitions/version"
|
2
|
+
require "state_transitions/engine"
|
3
|
+
|
4
|
+
module StateTransitions
|
5
|
+
def self.included(base)
|
6
|
+
base.extend ClassMethods
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
def track_state_transitions
|
11
|
+
model_class = self
|
12
|
+
|
13
|
+
has_one :current_state_transition,
|
14
|
+
lambda {
|
15
|
+
where(
|
16
|
+
<<~SQL.squish
|
17
|
+
#{current_table_name}.id = (
|
18
|
+
SELECT current_state_transition.id
|
19
|
+
FROM state_transitions AS current_state_transition
|
20
|
+
WHERE
|
21
|
+
current_state_transition.resource_type = #{current_table_name}.resource_type AND
|
22
|
+
current_state_transition.resource_id = #{current_table_name}.resource_id
|
23
|
+
ORDER BY current_state_transition.created_at DESC
|
24
|
+
LIMIT 1
|
25
|
+
)
|
26
|
+
SQL
|
27
|
+
)
|
28
|
+
},
|
29
|
+
as: :resource,
|
30
|
+
class_name: "StateTransitions::StateTransition"
|
31
|
+
|
32
|
+
has_many :state_transitions,
|
33
|
+
as: :resource,
|
34
|
+
class_name: "StateTransitions::StateTransition",
|
35
|
+
dependent: :destroy
|
36
|
+
|
37
|
+
after_save :save_state_transition, if: :saved_change_to_state?
|
38
|
+
end
|
39
|
+
|
40
|
+
def define_first_state_transition_scope(state)
|
41
|
+
sql = <<~SQL.squish
|
42
|
+
#{current_table_name}.id = (
|
43
|
+
SELECT first_state_transition.id
|
44
|
+
FROM state_transitions AS first_state_transition
|
45
|
+
WHERE
|
46
|
+
first_state_transition.resource_type = #{current_table_name}.resource_type AND
|
47
|
+
first_state_transition.resource_id = #{current_table_name}.resource_id AND
|
48
|
+
first_state_transition.state_to = ?
|
49
|
+
ORDER BY first_state_transition.created_at
|
50
|
+
LIMIT 1
|
51
|
+
)
|
52
|
+
SQL
|
53
|
+
|
54
|
+
has_one :"first_#{state}_state_transition",
|
55
|
+
-> { where(sql, state) },
|
56
|
+
as: :resource,
|
57
|
+
class_name: "StateTransitions::StateTransition"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def save_state_transition
|
62
|
+
StateTransitions::StateTransition.create!(
|
63
|
+
resource: self,
|
64
|
+
state_from: saved_change_to_state.fetch(0),
|
65
|
+
state_to: saved_change_to_state.fetch(1),
|
66
|
+
user: StateTransitions::Current.user
|
67
|
+
)
|
68
|
+
end
|
69
|
+
end
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: state_transitions
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- kaspernj
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2025-01-29 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.4
|
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.4
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec-rails
|
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: Tracks state transitions with AASM.
|
42
|
+
email:
|
43
|
+
- kasper@diestoeckels.de
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- MIT-LICENSE
|
49
|
+
- README.md
|
50
|
+
- Rakefile
|
51
|
+
- app/models/state_transitions/application_record.rb
|
52
|
+
- app/models/state_transitions/current.rb
|
53
|
+
- app/models/state_transitions/state_transition.rb
|
54
|
+
- config/routes.rb
|
55
|
+
- db/migrate/20221017084844_create_state_transitions.rb
|
56
|
+
- lib/state_transitions.rb
|
57
|
+
- lib/state_transitions/engine.rb
|
58
|
+
- lib/state_transitions/version.rb
|
59
|
+
- lib/tasks/state_transitions_tasks.rake
|
60
|
+
homepage: https://github.com/kaspernj/state_transitions
|
61
|
+
licenses:
|
62
|
+
- MIT
|
63
|
+
metadata:
|
64
|
+
homepage_uri: https://github.com/kaspernj/state_transitions
|
65
|
+
source_code_uri: https://github.com/kaspernj/state_transitions
|
66
|
+
changelog_uri: https://github.com/kaspernj/state_transitions
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
requirements: []
|
82
|
+
rubygems_version: 3.5.22
|
83
|
+
signing_key:
|
84
|
+
specification_version: 4
|
85
|
+
summary: Tracks state transitions with AASM.
|
86
|
+
test_files: []
|