state-notifier 0.0.1 → 0.0.2
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/lib/state/notifier/version.rb +5 -0
- data/lib/state/notifier.rb +48 -0
- data/state-notifier.gemspec +2 -2
- metadata +3 -3
- data/lib/state_notifier/version.rb +0 -3
- data/lib/state_notifier.rb +0 -46
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5a486acbfdf6f0523762786f7883f6dbe4daf2e8
|
4
|
+
data.tar.gz: 129146b1d147f4b21977629bf00d78eb83a5e56e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 600c88655af39a66129dc332e0eb27c44448bfba8af3811e660df0eb0ffad3584374a46c1d5c85fdd3f36514dc7b95b65b11f27ba280b74f11450b81ef660cf6
|
7
|
+
data.tar.gz: a5118a3efe600f89e52e464501df31c8856a21315d9cd773e5bb95c21cc98b01b28634426797b0a64bc0b3093457b4b758c3d8132963baf439cae8cbf4b8e7bf
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require "state/notifier/version"
|
2
|
+
|
3
|
+
module State
|
4
|
+
module Notifier
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
def notify_targets(event)
|
8
|
+
name = self.class.name.underscore
|
9
|
+
method = [name, event] * '_'
|
10
|
+
|
11
|
+
targets = self.class.notification_targets.map {|m| send(m)}.flatten
|
12
|
+
Rails.logger.debug "StateNotifier: notifying #{method}, #{targets.size} targets"
|
13
|
+
|
14
|
+
targets.each do |t|
|
15
|
+
t.send(method, self) if t.respond_to?(method)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def notify_transition(transition)
|
20
|
+
notify_targets transition.event
|
21
|
+
notify_targets transition.to unless transition.to == transition.from
|
22
|
+
end
|
23
|
+
|
24
|
+
included do
|
25
|
+
after_create {notify_targets :created}
|
26
|
+
after_update {notify_targets :updated unless (changed - self.class.reserved_attributes).empty?}
|
27
|
+
if respond_to?(:state_machine)
|
28
|
+
state_machine do
|
29
|
+
after_transition { |record, transition| record.notify_transition transition }
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
module ClassMethods
|
35
|
+
def notification_targets(*values)
|
36
|
+
if values.present?
|
37
|
+
@notification_targets = values
|
38
|
+
else
|
39
|
+
@notification_targets
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def reserved_attributes
|
44
|
+
%w[created_at updated_at state]
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/state-notifier.gemspec
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
# coding: utf-8
|
2
2
|
lib = File.expand_path('../lib', __FILE__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require '
|
4
|
+
require 'state/notifier/version'
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.name = "state-notifier"
|
8
|
-
spec.version =
|
8
|
+
spec.version = State::Notifier::VERSION
|
9
9
|
spec.authors = ["Vitaly Kushner"]
|
10
10
|
spec.email = ["vitaly@astrails.com"]
|
11
11
|
spec.description = %q{Simple gem to notify 'subscribers' about record state changes.}
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: state-notifier
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vitaly Kushner
|
@@ -64,8 +64,8 @@ files:
|
|
64
64
|
- LICENSE.txt
|
65
65
|
- README.md
|
66
66
|
- Rakefile
|
67
|
-
- lib/
|
68
|
-
- lib/
|
67
|
+
- lib/state/notifier.rb
|
68
|
+
- lib/state/notifier/version.rb
|
69
69
|
- state-notifier.gemspec
|
70
70
|
homepage: https://github.com/vitaly/state-notifier
|
71
71
|
licenses:
|
data/lib/state_notifier.rb
DELETED
@@ -1,46 +0,0 @@
|
|
1
|
-
require "state_notifier/version"
|
2
|
-
|
3
|
-
module StateNotifier
|
4
|
-
extend ActiveSupport::Concern
|
5
|
-
|
6
|
-
def notify_targets(event)
|
7
|
-
name = self.class.name.underscore
|
8
|
-
method = [name, event] * '_'
|
9
|
-
|
10
|
-
targets = self.class.notification_targets.map {|m| send(m)}.flatten
|
11
|
-
Rails.logger.debug "StateNotifier: notifying #{method}, #{targets.size} targets"
|
12
|
-
|
13
|
-
targets.each do |t|
|
14
|
-
t.send(method, self) if t.respond_to?(method)
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
def notify_transition(transition)
|
19
|
-
notify_targets transition.event
|
20
|
-
notify_targets transition.to unless transition.to == transition.from
|
21
|
-
end
|
22
|
-
|
23
|
-
included do
|
24
|
-
after_create {notify_targets :created}
|
25
|
-
after_update {notify_targets :updated unless (changed - self.class.reserved_attributes).empty?}
|
26
|
-
if respond_to?(:state_machine)
|
27
|
-
state_machine do
|
28
|
-
after_transition { |record, transition| record.notify_transition transition }
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
module ClassMethods
|
34
|
-
def notification_targets(*values)
|
35
|
-
if values.present?
|
36
|
-
@notification_targets = values
|
37
|
-
else
|
38
|
-
@notification_targets
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
def reserved_attributes
|
43
|
-
%w[created_at updated_at state]
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|