valid_transitions 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/.travis.yml +6 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +48 -0
- data/LICENSE.txt +21 -0
- data/README.md +164 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/active_model/validations/helper_methods.rb +30 -0
- data/lib/active_model/validations/transition_validator.rb +112 -0
- data/lib/valid_transitions/input_validator.rb +38 -0
- data/lib/valid_transitions/invalid_input_error.rb +4 -0
- data/lib/valid_transitions/version.rb +3 -0
- data/lib/valid_transitions.rb +6 -0
- data/valid_transitions.gemspec +33 -0
- metadata +118 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 7da7a8922dcbc335d8b2b1c5ddbfa01850a01746ab99d16934edf2b49d47ed0b
|
4
|
+
data.tar.gz: e989dde7dc196f73c69ac96e5548eab873ae7f51601a5979a7ffef1489657bf2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7007a032f8b9822d480680a5cbf8449c4fe8f0029442dc3fe523cc3f02811b03d07806939fc8b57f808112068fc2d0f29603954e14f4ee53aef225604ba62988
|
7
|
+
data.tar.gz: 2276dec3bf9f1522441eb5264f4bd21e538585313117d65daf0183976f306a3328a1a2ee54c64083a16467613c47d2096d46b15d90f90335199bbab837fc27ce
|
data/.gitignore
ADDED
data/.travis.yml
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 tedmaloney@me.com. 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/Gemfile.lock
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
valid_transitions (0.1.0)
|
5
|
+
activerecord (>= 3.0.0)
|
6
|
+
activesupport (>= 3.0.0)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
activemodel (6.1.4.1)
|
12
|
+
activesupport (= 6.1.4.1)
|
13
|
+
activerecord (6.1.4.1)
|
14
|
+
activemodel (= 6.1.4.1)
|
15
|
+
activesupport (= 6.1.4.1)
|
16
|
+
activesupport (6.1.4.1)
|
17
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
18
|
+
i18n (>= 1.6, < 2)
|
19
|
+
minitest (>= 5.1)
|
20
|
+
tzinfo (~> 2.0)
|
21
|
+
zeitwerk (~> 2.3)
|
22
|
+
coderay (1.1.3)
|
23
|
+
concurrent-ruby (1.1.9)
|
24
|
+
i18n (1.8.10)
|
25
|
+
concurrent-ruby (~> 1.0)
|
26
|
+
method_source (1.0.0)
|
27
|
+
minitest (5.14.4)
|
28
|
+
pry (0.14.1)
|
29
|
+
coderay (~> 1.1)
|
30
|
+
method_source (~> 1.0)
|
31
|
+
rake (12.3.3)
|
32
|
+
sqlite3 (1.4.2)
|
33
|
+
tzinfo (2.0.4)
|
34
|
+
concurrent-ruby (~> 1.0)
|
35
|
+
zeitwerk (2.4.2)
|
36
|
+
|
37
|
+
PLATFORMS
|
38
|
+
ruby
|
39
|
+
|
40
|
+
DEPENDENCIES
|
41
|
+
minitest (~> 5.0)
|
42
|
+
pry
|
43
|
+
rake (~> 12.0)
|
44
|
+
sqlite3
|
45
|
+
valid_transitions!
|
46
|
+
|
47
|
+
BUNDLED WITH
|
48
|
+
2.1.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2021 ted
|
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,164 @@
|
|
1
|
+
# ValidTransitions
|
2
|
+
|
3
|
+
## Installation
|
4
|
+
|
5
|
+
Add this line to your application's Gemfile:
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
gem 'valid_transitions'
|
9
|
+
```
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle install
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install valid_transitions
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
`valid_transitions` helps you define ActiveRecord attribute validation to prevent invalid data from going to the database.
|
21
|
+
|
22
|
+
As an example, let's say you have this `Car` model:
|
23
|
+
```ruby
|
24
|
+
class Car < ActiveRecord::Base
|
25
|
+
validates :state, inclusion: { in: %w[reverse parked 1st_gear 2nd_gear 3rd_gear] }
|
26
|
+
validates :doors, inclusion: { in: %w[opened closed] }
|
27
|
+
validates :condition, inclusion: { in: %w[working requires_service broken] }
|
28
|
+
validate_transitions :state, transitions: [
|
29
|
+
{ from: %w[reverse], to: %w[parked 1st_gear] },
|
30
|
+
{ from: %w[parked], to: %w[reverse 1st_gear] },
|
31
|
+
{ from: %w[1st_gear], to: %w[parked 2nd_gear] },
|
32
|
+
{ from: %w[2nd_gear], to: %w[1st_gear 3rd_gear] },
|
33
|
+
{ from: %w[3rd_gear], to: %w[2nd_gear] }
|
34
|
+
]
|
35
|
+
end
|
36
|
+
```
|
37
|
+
|
38
|
+
When a car is updated from `reverse` to `3rd_gear` using `.update`, the record will fail to persist due to validation.
|
39
|
+
```ruby
|
40
|
+
car = Car.create(state: 'reverse', doors: 'closed', brand: 'bmw')
|
41
|
+
car.update(state: '3rd_gear')
|
42
|
+
=> false
|
43
|
+
|
44
|
+
car.errors.messages
|
45
|
+
{:state=>["state cannot transition from reverse to 3rd_gear"]}
|
46
|
+
car.save!
|
47
|
+
ActiveRecord::RecordInvalid: Validation failed: State state cannot transition from reverse to 3rd_gear
|
48
|
+
```
|
49
|
+
#### Options
|
50
|
+
- `requires:` allows you to run conditional validation on your state transitions. For an example:
|
51
|
+
```ruby
|
52
|
+
class Car < ActiveRecord::Base
|
53
|
+
validates :state, inclusion: { in: %w[reverse parked 1st_gear 2nd_gear 3rd_gear] }
|
54
|
+
validates :doors, inclusion: { in: %w[opened closed] }
|
55
|
+
validate_transitions :state, transitions: [
|
56
|
+
{ from: %w[reverse], to: %w[parked 1st_gear] },
|
57
|
+
{ from: %w[parked], to: %w[reverse 1st_gear] },
|
58
|
+
{ from: %w[1st_gear], to: %w[parked 2nd_gear] },
|
59
|
+
{ from: %w[2nd_gear], to: %w[1st_gear 3rd_gear] },
|
60
|
+
{ from: %w[3rd_gear], to: %w[2nd_gear] }
|
61
|
+
]
|
62
|
+
|
63
|
+
validate_transitions :doors,
|
64
|
+
transitions: [
|
65
|
+
{
|
66
|
+
from: %w[closed opened],
|
67
|
+
to: %w[closed opened],
|
68
|
+
requires: { state: 'parked' }
|
69
|
+
}
|
70
|
+
]
|
71
|
+
end
|
72
|
+
```
|
73
|
+
`validate_transitions :doors` will fail to persist unless state is `parked`
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
car = Car.create(state: '1st_gear', brand: 'tesla', doors: 'closed')
|
77
|
+
car.update(doors: 'opened')
|
78
|
+
=> false
|
79
|
+
car.errors.messages
|
80
|
+
=> {:doors=>["doors cannot transition from closed to opened while state is 1st_gear"]}
|
81
|
+
```
|
82
|
+
|
83
|
+
- `when:` allows you to only run validations when another condition is met. For an example:
|
84
|
+
```ruby
|
85
|
+
class Car < ActiveRecord::Base
|
86
|
+
validates :state, inclusion: { in: %w[reverse parked 1st_gear 2nd_gear 3rd_gear]}
|
87
|
+
validates :doors, inclusion: { in: %w[opened closed]}
|
88
|
+
validate_transitions :state, transitions: [
|
89
|
+
{ from: %w[reverse], to: %w[parked 1st_gear] },
|
90
|
+
{ from: %w[parked], to: %w[reverse 1st_gear] },
|
91
|
+
{ from: %w[1st_gear], to: %w[parked 2nd_gear] },
|
92
|
+
{ from: %w[2nd_gear], to: %w[1st_gear 3rd_gear] },
|
93
|
+
{ from: %w[3rd_gear], to: %w[2nd_gear] }
|
94
|
+
]
|
95
|
+
|
96
|
+
validate_transitions :doors,
|
97
|
+
transitions: [
|
98
|
+
{
|
99
|
+
from: %w[closed opened],
|
100
|
+
to: %w[closed opened],
|
101
|
+
requires: { state: 'parked' }
|
102
|
+
}
|
103
|
+
],
|
104
|
+
when: { brand: %w[porsche tesla] }
|
105
|
+
end
|
106
|
+
|
107
|
+
```
|
108
|
+
`validate_transitions :doors` will only be executed when brand is `tesla` or `porsche`.
|
109
|
+
|
110
|
+
```ruby
|
111
|
+
car = Car.create(state: '1st_gear', brand: 'toyota', doors: 'closed')
|
112
|
+
car.update(doors: 'opened')
|
113
|
+
=> true
|
114
|
+
```
|
115
|
+
- `inclusive:` allows you to only allow transitions you've included in the list. By default this is `true`.
|
116
|
+
```ruby
|
117
|
+
class Car < ActiveRecord::Base
|
118
|
+
#...
|
119
|
+
validate_transitions :condition,
|
120
|
+
transitions: [
|
121
|
+
{
|
122
|
+
from: %w[working requires_service],
|
123
|
+
to: %w[working requires_service broken]
|
124
|
+
}
|
125
|
+
],
|
126
|
+
inclusive: false
|
127
|
+
end
|
128
|
+
|
129
|
+
```
|
130
|
+
|
131
|
+
By passing `inclusive:` as `false`, updating `condition` from a value that is not included in the list will pass validation.
|
132
|
+
```ruby
|
133
|
+
car = Car.create(condition: 'broken', state: '1st_gear', brand: 'toyota', doors: 'closed')
|
134
|
+
car.update(condition: 'working')
|
135
|
+
=> true
|
136
|
+
```
|
137
|
+
|
138
|
+
When inclusive is `true`, the default value, an error will occur:
|
139
|
+
```ruby
|
140
|
+
class Car < ActiveRecord::Base
|
141
|
+
#...
|
142
|
+
validate_transitions :condition,
|
143
|
+
transitions: [
|
144
|
+
{
|
145
|
+
from: %w[working requires_service],
|
146
|
+
to: %w[working requires_service broken]
|
147
|
+
}
|
148
|
+
],
|
149
|
+
inclusive: true
|
150
|
+
end
|
151
|
+
|
152
|
+
car = Car.create(condition: 'broken', state: '1st_gear', brand: 'toyota', doors: 'closed')
|
153
|
+
car.update(condition: 'working')
|
154
|
+
=> false
|
155
|
+
car.errors.messages
|
156
|
+
=> {:condition=>["condition cannot transition from broken to working."]}
|
157
|
+
```
|
158
|
+
## License
|
159
|
+
|
160
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
161
|
+
|
162
|
+
## Code of Conduct
|
163
|
+
|
164
|
+
Everyone interacting in the ValidTransitions project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/ted/valid_transitions/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "valid_transitions"
|
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,30 @@
|
|
1
|
+
require 'active_model/validations'
|
2
|
+
require 'valid_transitions/input_validator'
|
3
|
+
module ActiveModel
|
4
|
+
module Validations
|
5
|
+
|
6
|
+
module HelperMethods
|
7
|
+
|
8
|
+
def validate_transitions(column, options = {})
|
9
|
+
options = options.with_indifferent_access
|
10
|
+
|
11
|
+
ValidTransitions::InputValidator.validate_options(options)
|
12
|
+
|
13
|
+
transitions = options[:transitions]
|
14
|
+
only_when = options[:when]
|
15
|
+
inclusive = if options[:inclusive] == false
|
16
|
+
false
|
17
|
+
else
|
18
|
+
true
|
19
|
+
end
|
20
|
+
validates_with ActiveModel::Validations::TransitionValidator, {
|
21
|
+
column: column,
|
22
|
+
valid_transitions: transitions,
|
23
|
+
when_validations: only_when,
|
24
|
+
inclusive: inclusive
|
25
|
+
}
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,112 @@
|
|
1
|
+
module ActiveModel
|
2
|
+
module Validations
|
3
|
+
class TransitionValidator < ActiveModel::Validator
|
4
|
+
def initialize(options)
|
5
|
+
super
|
6
|
+
@column = options[:column].to_s
|
7
|
+
valid_transitions = options[:valid_transitions]
|
8
|
+
@when_validations = options[:when_validations]
|
9
|
+
@inclusive = options[:inclusive]
|
10
|
+
@allowed_transitions = {}
|
11
|
+
|
12
|
+
build_valid_transition_lookup(valid_transitions)
|
13
|
+
end
|
14
|
+
|
15
|
+
def build_valid_transition_lookup(valid_transitions)
|
16
|
+
valid_transitions.each do |transition|
|
17
|
+
[transition[:from]].flatten.each do |from|
|
18
|
+
[transition[:to]].flatten.each do |to|
|
19
|
+
requires_validation = transition[:requires].present?
|
20
|
+
requires_when = transition[:when].present?
|
21
|
+
@allowed_transitions[from] ||= []
|
22
|
+
next @allowed_transitions[from] << { to => { requires_validation: false } } unless requires_validation
|
23
|
+
|
24
|
+
@allowed_transitions[from] << {
|
25
|
+
to => {
|
26
|
+
requires_validation: true,
|
27
|
+
requires_when: requires_when,
|
28
|
+
requires: transition[:requires].map do |key, value|
|
29
|
+
{
|
30
|
+
key => [value].flatten
|
31
|
+
}
|
32
|
+
end
|
33
|
+
}
|
34
|
+
}
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def validate(record)
|
41
|
+
return true if @when_validations && when_condition_unmet?(record)
|
42
|
+
return true unless record.persisted?
|
43
|
+
|
44
|
+
changes = record.changes
|
45
|
+
return true unless changes.has_key?(@column)
|
46
|
+
|
47
|
+
column_change = changes[@column]
|
48
|
+
original_value = column_change[0]
|
49
|
+
new_value = column_change[1]
|
50
|
+
valid_transitions = @allowed_transitions[original_value]
|
51
|
+
|
52
|
+
if valid_transitions.nil?
|
53
|
+
add_inclusion_errors(record, original_value, new_value) if @inclusive
|
54
|
+
return true
|
55
|
+
end
|
56
|
+
|
57
|
+
transition = valid_transitions.find { |allowed_transition| allowed_transition[new_value] }
|
58
|
+
return invalid_transition_error(record, original_value, new_value) unless transition
|
59
|
+
|
60
|
+
requires_validation = transition[new_value][:requires_validation]
|
61
|
+
return true unless requires_validation
|
62
|
+
|
63
|
+
requires_validation_requirements = transition[new_value][:requires]
|
64
|
+
validate_requires_field(requires_validation_requirements, original_value, new_value, record)
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
|
69
|
+
def add_inclusion_errors(record, original_value, new_value)
|
70
|
+
record.errors.add(
|
71
|
+
@column.to_sym,
|
72
|
+
"#{@column} cannot transition from #{original_value} to #{new_value}."
|
73
|
+
)
|
74
|
+
end
|
75
|
+
|
76
|
+
def when_condition_unmet?(record)
|
77
|
+
met_conditions = @when_validations.map do |attribute, when_values|
|
78
|
+
current_value = record.read_attribute(attribute)
|
79
|
+
[when_values].flatten.include?(current_value)
|
80
|
+
end
|
81
|
+
|
82
|
+
!met_conditions.all?(true)
|
83
|
+
end
|
84
|
+
|
85
|
+
def validate_requires_field(all_requires, original_value, new_value, record)
|
86
|
+
all_requires.each do |requirement|
|
87
|
+
requirement.map do |attribute, allowed_values|
|
88
|
+
current_value = record.read_attribute(attribute)
|
89
|
+
next if allowed_values.include?(current_value)
|
90
|
+
|
91
|
+
add_requires_error(record, original_value, new_value, attribute, current_value)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def invalid_transition_error(record, original_value, new_value)
|
97
|
+
record.errors.add(
|
98
|
+
@column.to_sym,
|
99
|
+
"#{@column} cannot transition from #{original_value} to #{new_value}"
|
100
|
+
)
|
101
|
+
end
|
102
|
+
|
103
|
+
def add_requires_error(record, original_value, new_value, required_column_name, current_required_value)
|
104
|
+
record.errors.add(
|
105
|
+
@column.to_sym,
|
106
|
+
"#{@column} cannot transition from #{original_value} to #{new_value} while #{required_column_name} is #{current_required_value}"
|
107
|
+
)
|
108
|
+
end
|
109
|
+
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'valid_transitions/invalid_input_error'
|
2
|
+
|
3
|
+
module ValidTransitions
|
4
|
+
module InputValidator
|
5
|
+
extend self
|
6
|
+
|
7
|
+
def validate_options(options)
|
8
|
+
transitions = options[:transitions]
|
9
|
+
validate_transition_type_and_presence(transitions)
|
10
|
+
validate_transition_content(transitions)
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def validate_transition_type_and_presence(transitions)
|
16
|
+
raise_error("validates_with called without :transitions") unless transitions
|
17
|
+
raise_error("validates_with called with :transitions as an #{transitions.class}, expected Array") unless transitions.is_a?(Array)
|
18
|
+
end
|
19
|
+
|
20
|
+
def validate_transition_content(transitions)
|
21
|
+
transitions.each do |transition|
|
22
|
+
raise_error("transition #{transition} missing key :to") unless transition.has_key?('to')
|
23
|
+
raise_error("transition #{transition} missing key :from") unless transition.has_key?('from')
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def validate_when(options)
|
28
|
+
return true unless options[:when]
|
29
|
+
return true if options[:when].is_a?(Hash)
|
30
|
+
|
31
|
+
raise_error(":when is #{options[:when]}, which should be a Hash")
|
32
|
+
end
|
33
|
+
|
34
|
+
def raise_error(message)
|
35
|
+
raise ValidTransitions::InvalidInputError, message
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require_relative 'lib/valid_transitions/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "valid_transitions"
|
5
|
+
spec.version = ValidTransitions::VERSION
|
6
|
+
spec.authors = ["ted"]
|
7
|
+
spec.email = ["tedmaloney@me.com"]
|
8
|
+
|
9
|
+
spec.summary = %q{Adds valid transition validations to AR.}
|
10
|
+
spec.homepage = "https://github.com/ted/valid_transitions"
|
11
|
+
spec.license = "MIT"
|
12
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
13
|
+
|
14
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
|
15
|
+
|
16
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
17
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
18
|
+
# spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
|
19
|
+
|
20
|
+
# Specify which files should be added to the gem when it is released.
|
21
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
22
|
+
spec.add_runtime_dependency "activerecord", ">= 3.0.0"
|
23
|
+
spec.add_runtime_dependency "activesupport", ">= 3.0.0"
|
24
|
+
spec.add_development_dependency "sqlite3"
|
25
|
+
spec.add_development_dependency "pry"
|
26
|
+
|
27
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
28
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
29
|
+
end
|
30
|
+
spec.bindir = "exe"
|
31
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
32
|
+
spec.require_paths = ["lib"]
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: valid_transitions
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- ted
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-08-25 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: 3.0.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 3.0.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 3.0.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 3.0.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: sqlite3
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- tedmaloney@me.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".travis.yml"
|
78
|
+
- CODE_OF_CONDUCT.md
|
79
|
+
- Gemfile
|
80
|
+
- Gemfile.lock
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- bin/console
|
85
|
+
- bin/setup
|
86
|
+
- lib/active_model/validations/helper_methods.rb
|
87
|
+
- lib/active_model/validations/transition_validator.rb
|
88
|
+
- lib/valid_transitions.rb
|
89
|
+
- lib/valid_transitions/input_validator.rb
|
90
|
+
- lib/valid_transitions/invalid_input_error.rb
|
91
|
+
- lib/valid_transitions/version.rb
|
92
|
+
- valid_transitions.gemspec
|
93
|
+
homepage: https://github.com/ted/valid_transitions
|
94
|
+
licenses:
|
95
|
+
- MIT
|
96
|
+
metadata:
|
97
|
+
homepage_uri: https://github.com/ted/valid_transitions
|
98
|
+
source_code_uri: https://github.com/ted/valid_transitions
|
99
|
+
post_install_message:
|
100
|
+
rdoc_options: []
|
101
|
+
require_paths:
|
102
|
+
- lib
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: 2.3.0
|
108
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
requirements: []
|
114
|
+
rubygems_version: 3.0.3
|
115
|
+
signing_key:
|
116
|
+
specification_version: 4
|
117
|
+
summary: Adds valid transition validations to AR.
|
118
|
+
test_files: []
|