transitions_listener 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cf5ad9460a9d232ee046520eb1e29b5d6a6f529571ba88d518e941dc5662abb5
4
- data.tar.gz: be6081d137e8e47c995225f2e32951c0abd45d8c53b61ae180e75b1aa9ca5275
3
+ metadata.gz: 2c3f3e1d28ec79312f01059542a713c41a340f86d93815cb0133d301227d8452
4
+ data.tar.gz: 205feba4db11d7698d00a8a7f7ebebed649d15856a5939c7505edbfdcbc4eaa8
5
5
  SHA512:
6
- metadata.gz: 2de3e33e1d5a4684a99d23837c40ff2207ae6f4d0f3e267aed8c2de5197ff7948574748fee538828120eabee12bb10d03525c7e3c2607ed9d89494657da6f5ef
7
- data.tar.gz: 842e02b54c22ed9439b7ba355ca08b3e9c13830e36eb651e42c4ac6008e6101b5b98bc6e692c3204d453891420237055fd161bd11133df2ea4b8c3e1768bb93e
6
+ metadata.gz: 24f849cae23acb3f0bcc5926b7d40554c428c7b07098601bab30089a43313c8430d98ece45dbb889ca6ebba4c9f1b42995fc2785ebf6b931816aa1ba51958810
7
+ data.tar.gz: e3df05226b1b4349e63230fe65aeef29636014cab2d2382b31140c8914aa5e3935e742914fbc98d7c6eba53f7fcb297f2a39507448d7c81f904b362687087819
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # Release 0.2.0 (April 23, 2020)
2
+ - Model method as a callback listener
3
+
4
+ # Release 0.1.0
5
+ - Initial release
data/Gemfile.lock CHANGED
@@ -1,25 +1,24 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- transitions_listener (0.1.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 (5.2.4.2)
12
- activesupport (= 5.2.4.2)
13
- activerecord (5.2.4.2)
14
- activemodel (= 5.2.4.2)
15
- activesupport (= 5.2.4.2)
16
- arel (>= 9.0)
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
- arel (9.0.0)
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
 
@@ -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 |transition|
29
- transition[:block].call(model, c_transition.merge(transition))
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:)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TransitionsListener
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
5
5
  end
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.1.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-17 00:00:00.000000000 Z
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
- rubyforge_project:
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