transitions_listener 0.1.0 → 0.2.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 +4 -4
- data/CHANGELOG.md +5 -0
- data/Gemfile.lock +9 -9
- data/README.md +13 -5
- data/lib/transitions_listener.rb +6 -2
- data/lib/transitions_listener/listener.rb +4 -4
- data/lib/transitions_listener/version.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2c3f3e1d28ec79312f01059542a713c41a340f86d93815cb0133d301227d8452
|
4
|
+
data.tar.gz: 205feba4db11d7698d00a8a7f7ebebed649d15856a5939c7505edbfdcbc4eaa8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 24f849cae23acb3f0bcc5926b7d40554c428c7b07098601bab30089a43313c8430d98ece45dbb889ca6ebba4c9f1b42995fc2785ebf6b931816aa1ba51958810
|
7
|
+
data.tar.gz: e3df05226b1b4349e63230fe65aeef29636014cab2d2382b31140c8914aa5e3935e742914fbc98d7c6eba53f7fcb297f2a39507448d7c81f904b362687087819
|
data/CHANGELOG.md
ADDED
data/Gemfile.lock
CHANGED
@@ -1,25 +1,24 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
transitions_listener (0.
|
4
|
+
transitions_listener (0.2.0)
|
5
5
|
activerecord
|
6
6
|
activesupport
|
7
7
|
|
8
8
|
GEM
|
9
9
|
remote: https://rubygems.org/
|
10
10
|
specs:
|
11
|
-
activemodel (
|
12
|
-
activesupport (=
|
13
|
-
activerecord (
|
14
|
-
activemodel (=
|
15
|
-
activesupport (=
|
16
|
-
|
17
|
-
activesupport (5.2.4.2)
|
11
|
+
activemodel (6.0.2.2)
|
12
|
+
activesupport (= 6.0.2.2)
|
13
|
+
activerecord (6.0.2.2)
|
14
|
+
activemodel (= 6.0.2.2)
|
15
|
+
activesupport (= 6.0.2.2)
|
16
|
+
activesupport (6.0.2.2)
|
18
17
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
19
18
|
i18n (>= 0.7, < 2)
|
20
19
|
minitest (~> 5.1)
|
21
20
|
tzinfo (~> 1.1)
|
22
|
-
|
21
|
+
zeitwerk (~> 2.2)
|
23
22
|
ast (2.4.0)
|
24
23
|
concurrent-ruby (1.1.6)
|
25
24
|
diff-lcs (1.3)
|
@@ -60,6 +59,7 @@ GEM
|
|
60
59
|
tzinfo (1.2.7)
|
61
60
|
thread_safe (~> 0.1)
|
62
61
|
unicode-display_width (1.7.0)
|
62
|
+
zeitwerk (2.3.0)
|
63
63
|
|
64
64
|
PLATFORMS
|
65
65
|
ruby
|
data/README.md
CHANGED
@@ -32,6 +32,12 @@ class Article < ActiveRecord::Base
|
|
32
32
|
before_transition any => :deleted do |article, transition|
|
33
33
|
# article.errors.add(:base, "not possible") if article.active_carts.any?
|
34
34
|
end
|
35
|
+
|
36
|
+
before_transition({ any => any }, :any_to_any_callback)
|
37
|
+
end
|
38
|
+
|
39
|
+
def any_to_any_callback(_transition)
|
40
|
+
puts "any to any callback called"
|
35
41
|
end
|
36
42
|
end
|
37
43
|
```
|
@@ -40,13 +46,15 @@ end
|
|
40
46
|
- ````listen_transitions(attr_name){block}```` permit to define state transitions listener for a specific model attribute
|
41
47
|
- ````before_transition(states){block}```` permit to listen transitions before the new state is saved (Before update)
|
42
48
|
- ````after_transition(states){block}```` permit to listen transitions after the new state was saved (After update)
|
49
|
+
- ````before_transition(states, :callback_name)```` model method to listen transition callbacks
|
50
|
+
- ````after_transition(states, :callback_name)```` model method to listen transition callbacks
|
43
51
|
|
44
52
|
States can be defined as the following:
|
45
|
-
- ```before_transition(any => any)``` block will be called when attr value is changed from any value to any value
|
46
|
-
- ```before_transition(any => :active)``` block will be called when attr value is changed from any value to :active
|
47
|
-
- ```before_transition(:active => any)``` block will be called when attr value is changed from :active value to any value
|
48
|
-
- ```before_transition(%i[active inactive] => %i[deleted cancelled])``` block will be called when attr value is changed from :active or inactive to :deleted or :cancelled
|
49
|
-
- ```before_transition(active: :inactive, inactive: :deleted)``` block will be called when attr value is changed from :active to :inactive or :inactive to :deleted
|
53
|
+
- ```before_transition(any => any){}``` block will be called when attr value is changed from any value to any value
|
54
|
+
- ```before_transition(any => :active){}``` block will be called when attr value is changed from any value to :active
|
55
|
+
- ```before_transition(:active => any){}``` block will be called when attr value is changed from :active value to any value
|
56
|
+
- ```before_transition(%i[active inactive] => %i[deleted cancelled]){}``` block will be called when attr value is changed from :active or inactive to :deleted or :cancelled
|
57
|
+
- ```before_transition(active: :inactive, inactive: :deleted){}``` block will be called when attr value is changed from :active to :inactive or :inactive to :deleted
|
50
58
|
|
51
59
|
## Development
|
52
60
|
|
data/lib/transitions_listener.rb
CHANGED
@@ -25,8 +25,12 @@ module TransitionsListener
|
|
25
25
|
def perform_transition_listeners(model, kind = :before)
|
26
26
|
transition_listeners.each do |listener|
|
27
27
|
c_transition = current_transition_for(model, listener.attr)
|
28
|
-
listener.filter_transitions(kind, c_transition).each do |
|
29
|
-
|
28
|
+
listener.filter_transitions(kind, c_transition).each do |trans|
|
29
|
+
if trans[:block].is_a?(Proc)
|
30
|
+
trans[:block].call(model, c_transition.merge(trans))
|
31
|
+
else # method name
|
32
|
+
model.public_send(trans[:block], c_transition.merge(trans))
|
33
|
+
end
|
30
34
|
end
|
31
35
|
end
|
32
36
|
end
|
@@ -8,12 +8,12 @@ module TransitionsListener
|
|
8
8
|
instance_eval(&block)
|
9
9
|
end
|
10
10
|
|
11
|
-
def before_transition(states, &block)
|
12
|
-
before_transitions(states: states, block: block)
|
11
|
+
def before_transition(states, callback_method = nil, &block)
|
12
|
+
before_transitions(states: states, block: callback_method || block)
|
13
13
|
end
|
14
14
|
|
15
|
-
def after_transition(states, &block)
|
16
|
-
after_transitions(states: states, block: block)
|
15
|
+
def after_transition(states, callback_method = nil, &block)
|
16
|
+
after_transitions(states: states, block: callback_method || block)
|
17
17
|
end
|
18
18
|
|
19
19
|
def filter_transitions(kind, from:, to:)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: transitions_listener
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Owen
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-04-
|
11
|
+
date: 2020-04-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -91,6 +91,7 @@ files:
|
|
91
91
|
- ".gitignore"
|
92
92
|
- ".rspec"
|
93
93
|
- ".rubocop.yml"
|
94
|
+
- CHANGELOG.md
|
94
95
|
- CODE_OF_CONDUCT.md
|
95
96
|
- Gemfile
|
96
97
|
- Gemfile.lock
|
@@ -126,8 +127,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
126
127
|
- !ruby/object:Gem::Version
|
127
128
|
version: '0'
|
128
129
|
requirements: []
|
129
|
-
|
130
|
-
rubygems_version: 2.7.7
|
130
|
+
rubygems_version: 3.0.8
|
131
131
|
signing_key:
|
132
132
|
specification_version: 4
|
133
133
|
summary: Permit to listen all transition movements
|