cony 1.3.0 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/README.md +3 -1
- data/VERSION +1 -1
- data/lib/cony/active_record.rb +30 -20
- data/spec/cony/active_record_spec.rb +9 -4
- metadata +4 -4
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.
|
1
|
+
1.4.0
|
data/lib/cony/active_record.rb
CHANGED
@@ -9,48 +9,46 @@ module Cony
|
|
9
9
|
extend ActiveSupport::Concern
|
10
10
|
|
11
11
|
included do
|
12
|
-
after_create :
|
13
|
-
after_update :
|
14
|
-
after_destroy :
|
12
|
+
after_create :cony_save_create_notify_data
|
13
|
+
after_update :cony_save_update_notify_data
|
14
|
+
after_destroy :cony_save_destroy_notify_data
|
15
|
+
after_commit :cony_publish
|
15
16
|
end
|
16
17
|
|
17
|
-
def
|
18
|
-
|
18
|
+
def cony_save_create_notify_data
|
19
|
+
cony_prepare_notify :created, cony_changes_created
|
19
20
|
end
|
20
21
|
|
21
|
-
def
|
22
|
-
|
22
|
+
def cony_save_update_notify_data
|
23
|
+
cony_prepare_notify :updated, cony_changes_updated
|
23
24
|
end
|
24
25
|
|
25
|
-
def
|
26
|
-
|
26
|
+
def cony_save_destroy_notify_data
|
27
|
+
cony_prepare_notify :destroyed, cony_changes_destroyed
|
27
28
|
end
|
28
29
|
|
29
|
-
|
30
|
-
private
|
31
|
-
def publish(type, cony_changes)
|
30
|
+
def cony_publish
|
32
31
|
return if Cony.config.test_mode
|
33
|
-
|
34
|
-
{id: self.id, changes: cony_changes, model: self.class.name, event: type},
|
35
|
-
"#{self.class.name.underscore}.mutation.#{type}")
|
32
|
+
cony_amqp_connection.publish(cony_notify_hash, cony_notify_routing_key)
|
36
33
|
end
|
37
34
|
|
38
|
-
|
39
|
-
|
35
|
+
private
|
36
|
+
def cony_amqp_connection
|
37
|
+
@cony_amqp_connection ||= Cony::AMQPConnectionHandler.new(Cony.config.amqp)
|
40
38
|
end
|
41
39
|
|
42
|
-
def
|
40
|
+
def cony_mapped_changes
|
43
41
|
changes.map do |name, change|
|
44
42
|
{name => {old: change.first, new: change.last}}
|
45
43
|
end
|
46
44
|
end
|
47
45
|
|
48
46
|
def cony_changes_created
|
49
|
-
|
47
|
+
cony_mapped_changes
|
50
48
|
end
|
51
49
|
|
52
50
|
def cony_changes_updated
|
53
|
-
|
51
|
+
cony_mapped_changes
|
54
52
|
end
|
55
53
|
|
56
54
|
def cony_changes_destroyed
|
@@ -58,5 +56,17 @@ module Cony
|
|
58
56
|
{name => {old: value, new: nil}}
|
59
57
|
end
|
60
58
|
end
|
59
|
+
|
60
|
+
def cony_notify_hash
|
61
|
+
{id: self.id, changes: @cony_notify[:changes], model: self.class.name, event: @cony_notify[:event]}
|
62
|
+
end
|
63
|
+
|
64
|
+
def cony_notify_routing_key
|
65
|
+
"#{self.class.name.underscore}.mutation.#{@cony_notify[:event]}"
|
66
|
+
end
|
67
|
+
|
68
|
+
def cony_prepare_notify(event, changes)
|
69
|
+
@cony_notify = { event: event, changes: changes }
|
70
|
+
end
|
61
71
|
end
|
62
72
|
end
|
@@ -24,6 +24,7 @@ describe Cony::ActiveRecord do
|
|
24
24
|
def self.after_create(callback); end
|
25
25
|
def self.after_update(callback); end
|
26
26
|
def self.after_destroy(callback); end
|
27
|
+
def self.after_commit(callback); end
|
27
28
|
def self.name; "Anonymaus::Klass"; end
|
28
29
|
def id; #{id}; end
|
29
30
|
def changes; #{active_record_changes}; end
|
@@ -44,7 +45,8 @@ describe Cony::ActiveRecord do
|
|
44
45
|
let(:event) { :created }
|
45
46
|
it 'uses the amqp connection to send the notify' do
|
46
47
|
amqp_connection.should_receive(:publish).with(expected_payload, 'anonymaus/klass.mutation.created')
|
47
|
-
subject.
|
48
|
+
subject.cony_save_create_notify_data
|
49
|
+
subject.cony_publish
|
48
50
|
end
|
49
51
|
end
|
50
52
|
|
@@ -52,7 +54,8 @@ describe Cony::ActiveRecord do
|
|
52
54
|
let(:event) { :updated }
|
53
55
|
it 'uses the amqp connection to send the notify' do
|
54
56
|
amqp_connection.should_receive(:publish).with(expected_payload, 'anonymaus/klass.mutation.updated')
|
55
|
-
subject.
|
57
|
+
subject.cony_save_update_notify_data
|
58
|
+
subject.cony_publish
|
56
59
|
end
|
57
60
|
end
|
58
61
|
|
@@ -61,7 +64,8 @@ describe Cony::ActiveRecord do
|
|
61
64
|
let(:cony_changes) { [{name: {old: 'value', new: nil}}] }
|
62
65
|
it 'uses the amqp connection to send the notify' do
|
63
66
|
amqp_connection.should_receive(:publish).with(expected_payload, 'anonymaus/klass.mutation.destroyed')
|
64
|
-
subject.
|
67
|
+
subject.cony_save_destroy_notify_data
|
68
|
+
subject.cony_publish
|
65
69
|
end
|
66
70
|
end
|
67
71
|
|
@@ -71,7 +75,8 @@ describe Cony::ActiveRecord do
|
|
71
75
|
end
|
72
76
|
it 'does not send the message' do
|
73
77
|
expect(Cony::AMQPConnectionHandler).to_not receive(:new)
|
74
|
-
subject.
|
78
|
+
subject.cony_save_create_notify_data
|
79
|
+
subject.cony_publish
|
75
80
|
end
|
76
81
|
end
|
77
82
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cony
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.4.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-04-
|
12
|
+
date: 2014-04-23 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
@@ -120,7 +120,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
120
120
|
version: '0'
|
121
121
|
segments:
|
122
122
|
- 0
|
123
|
-
hash:
|
123
|
+
hash: 1642476899976507358
|
124
124
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
125
125
|
none: false
|
126
126
|
requirements:
|
@@ -129,7 +129,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
129
129
|
version: '0'
|
130
130
|
segments:
|
131
131
|
- 0
|
132
|
-
hash:
|
132
|
+
hash: 1642476899976507358
|
133
133
|
requirements: []
|
134
134
|
rubyforge_project:
|
135
135
|
rubygems_version: 1.8.23
|