snm-enum-transition 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 +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +61 -0
- data/Rakefile +3 -0
- data/lib/snm/enum/transition/railtie.rb +8 -0
- data/lib/snm/enum/transition/version.rb +7 -0
- data/lib/snm/enum/transition.rb +42 -0
- data/lib/tasks/snm/enum/transition_tasks.rake +4 -0
- metadata +71 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 553f66e61a7a58bbc3d64fa123f903e8f0e416a600f2d0313766bc35c9e00358
|
4
|
+
data.tar.gz: 1740b4d0b5f255e6f9d7674eb86f56c3345f73a4add555c23b6f120981e4819d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7d894a61696c6b7139c85f1cbbb8ce4e610840124973a08fdf2dc9a82a0b242f0b59c76aa692f55f5f513b033be990d1547846d9c88e35f140b5c3a32cf065ff
|
7
|
+
data.tar.gz: dad1895e522b52acdef011088ea7c5822495b6f42e0f1f35ba160236a0462ba852c077cc41367fe667d6c5044ae72256a9a7f1360cae24e88b58e6d5fd25b8c8
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2024 snmmaurya
|
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,61 @@
|
|
1
|
+
# Snm::Enum::Transition
|
2
|
+
Active Record ENUM based state machine. Whether you can control enum transation from one state to another state. Suppose you have an user model and you want to once user has been rejected he can't be verified, You can achive this job easily using this library.
|
3
|
+
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
Add this line to your application's Gemfile:
|
7
|
+
|
8
|
+
```ruby
|
9
|
+
gem "snm-enum-transition"
|
10
|
+
```
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
```bash
|
14
|
+
$ bundle
|
15
|
+
```
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
```bash
|
19
|
+
$ gem install snm-enum-transition
|
20
|
+
```
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
|
26
|
+
# You have an user model and you want to specify status transitions: initial to verified, initial to rejected and rejected to initial
|
27
|
+
|
28
|
+
|
29
|
+
class User
|
30
|
+
include Snm::Enum::Transition
|
31
|
+
|
32
|
+
enum status: {initial: 0, verified: 1, rejected: 2]
|
33
|
+
|
34
|
+
# Specify (allow enum transition) column name, from and to
|
35
|
+
snm_enum_transitions :status, :initial, [:verified, :rejected]
|
36
|
+
snm_enum_transitions :status, [:rejected], [:initial]
|
37
|
+
end
|
38
|
+
|
39
|
+
user = User.create(status: :initial)
|
40
|
+
|
41
|
+
|
42
|
+
# IF user status is initial
|
43
|
+
user.verified! # Okay
|
44
|
+
user.rejected! # Okay
|
45
|
+
|
46
|
+
# IF user status is verified
|
47
|
+
user.rejected! # ActiveRecord::RecordInvalid: Validation failed: Status can't be changed from verified to rejected
|
48
|
+
user.initial! # ActiveRecord::RecordInvalid: Validation failed: Status can't be changed from verified to initial
|
49
|
+
|
50
|
+
# IF user status is rejected
|
51
|
+
user.initial! # Okay
|
52
|
+
user.verified! # ActiveRecord::RecordInvalid: Validation failed: Status can't be changed from rejected to verified
|
53
|
+
|
54
|
+
```
|
55
|
+
|
56
|
+
|
57
|
+
## Contributing
|
58
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/snmmaurya/snm-enum-transition. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
59
|
+
|
60
|
+
## License
|
61
|
+
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,42 @@
|
|
1
|
+
require "snm/enum/transition/version"
|
2
|
+
require "snm/enum/transition/railtie"
|
3
|
+
|
4
|
+
module Snm
|
5
|
+
module Enum
|
6
|
+
module Transition
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
def self.included(base)
|
10
|
+
base.class_eval do
|
11
|
+
validate :ensure!
|
12
|
+
base.extend ClassMethods
|
13
|
+
@enum_transition_columns=[]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
module ClassMethods
|
18
|
+
def snm_enum_transitions column_name, from_transitions, to_transitions
|
19
|
+
from_transitions.each do |ft|
|
20
|
+
to_transitions.each do |tt|
|
21
|
+
define_method ("snm_#{ft}_to_#{tt}") do
|
22
|
+
true
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
@enum_transition_columns.push(column_name).uniq!
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def ensure!
|
31
|
+
self.class.name.constantize.instance_variable_get('@enum_transition_columns').each do |column|
|
32
|
+
if !self.new_record? && self.send("#{column}_changed?")
|
33
|
+
unless self.respond_to? "snm_#{self.send("#{column}_was")}_to_#{self.send("#{column}")}"
|
34
|
+
self.errors.add(:base, "#{column.capitalize} can't be changed from #{self.send("#{column}_was")} to #{self.send("#{column}")}")
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: snm-enum-transition
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- snmmaurya
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-07-16 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.8.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.8.4
|
27
|
+
description: Active Record ENUM based state machine. Whether you can control enum
|
28
|
+
transation from one state to another state. Suppose you have an user model and you
|
29
|
+
want to once user has been rejected he can't be verified, You can achive this job
|
30
|
+
easily using this library.
|
31
|
+
email:
|
32
|
+
- snmspace@gmail.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- MIT-LICENSE
|
38
|
+
- README.md
|
39
|
+
- Rakefile
|
40
|
+
- lib/snm/enum/transition.rb
|
41
|
+
- lib/snm/enum/transition/railtie.rb
|
42
|
+
- lib/snm/enum/transition/version.rb
|
43
|
+
- lib/tasks/snm/enum/transition_tasks.rake
|
44
|
+
homepage: https://github.com/snmmaurya/snm-enum-transition
|
45
|
+
licenses:
|
46
|
+
- MIT
|
47
|
+
metadata:
|
48
|
+
allowed_push_host: https://rubygems.org
|
49
|
+
homepage_uri: https://github.com/snmmaurya/snm-enum-transition
|
50
|
+
source_code_uri: https://github.com/snmmaurya/snm-enum-transition
|
51
|
+
changelog_uri: https://github.com/snmmaurya/snm-enum-transition
|
52
|
+
post_install_message:
|
53
|
+
rdoc_options: []
|
54
|
+
require_paths:
|
55
|
+
- lib
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
requirements: []
|
67
|
+
rubygems_version: 3.3.7
|
68
|
+
signing_key:
|
69
|
+
specification_version: 4
|
70
|
+
summary: Active Record ENUM based state machine
|
71
|
+
test_files: []
|