wisper-activerecord 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 +4 -4
- data/README.md +27 -47
- data/lib/wisper/active_record/publisher.rb +23 -15
- data/lib/wisper/active_record/version.rb +1 -1
- data/spec/wisper/activerecord_spec.rb +1 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 666f55caf67abe6ebccc3a5670316f32c4ff8406
|
4
|
+
data.tar.gz: 10583078a56cb785deeb6c9548de3a09499d4e8d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2212d5f93e3ecd82c01369f52396e6ee0e41df1a320c95496c76d3e50d0e8409a2d508c22b31b606bfd847264b65ce80743676a22b68b75e8a54fdb165cc6d24
|
7
|
+
data.tar.gz: 5072defbcaf59d3df9eaf8ee6b8cfb01107b4be2793d53e24167d7ff3e9aa37747c6dbe6e7d19bdbd91e6e50009e1de3309d0e839e71afa9468259b6841af210
|
data/README.md
CHANGED
@@ -16,7 +16,7 @@ gem 'wisper-activerecord'
|
|
16
16
|
|
17
17
|
## Usage
|
18
18
|
|
19
|
-
###
|
19
|
+
### Setup a publisher
|
20
20
|
|
21
21
|
```ruby
|
22
22
|
class Meeting < ActiveRecord::Base
|
@@ -26,43 +26,47 @@ class Meeting < ActiveRecord::Base
|
|
26
26
|
end
|
27
27
|
```
|
28
28
|
|
29
|
-
If you wish all models to broadcast events without having explicitly include
|
30
|
-
`Wisper.model`
|
29
|
+
If you wish all models to broadcast events without having to explicitly include
|
30
|
+
`Wisper.model` add the following to an initializer:
|
31
31
|
|
32
32
|
```ruby
|
33
|
-
# config/initializers/wisper.rb
|
34
33
|
Wisper::ActiveRecord.extend_all
|
35
34
|
```
|
36
35
|
|
37
|
-
|
36
|
+
### Subscribing
|
38
37
|
|
39
|
-
Subscribe a listener to instances
|
38
|
+
Subscribe a listener to model instances:
|
40
39
|
|
41
40
|
```ruby
|
42
41
|
meeting = Meeting.new
|
43
42
|
meeting.subscribe(Auditor.new)
|
44
43
|
```
|
45
44
|
|
46
|
-
Subscribe a
|
45
|
+
Subscribe a block to model instances:
|
47
46
|
|
48
47
|
```ruby
|
49
|
-
|
48
|
+
meeting.on(:create_meeting_successful) { |meeting| ... }
|
50
49
|
```
|
51
50
|
|
52
|
-
|
51
|
+
Subscribe a listener to _all_ instances of a model:
|
53
52
|
|
54
|
-
|
53
|
+
```ruby
|
54
|
+
Meeting.subscribe(Auditor.new)
|
55
|
+
```
|
55
56
|
|
56
|
-
|
57
|
-
* `after_{create, update, destroy}`
|
57
|
+
Please refer to the [Wisper README](https://github.com/krisleech/wisper) for full details about subscribing.
|
58
58
|
|
59
|
-
|
59
|
+
The events which are automatically broadcast are:
|
60
60
|
|
61
|
-
|
62
|
-
|
63
|
-
|
61
|
+
* `after_create`
|
62
|
+
* `after_create`
|
63
|
+
* `after_destroy`
|
64
|
+
* `create_<model_name>_{successful, failed}`
|
65
|
+
* `update_<model_name>_{successful, failed}`
|
66
|
+
* `destroy_<model_name>_successful`
|
67
|
+
* `after_rollback`
|
64
68
|
|
65
|
-
### Reacting to
|
69
|
+
### Reacting to Events
|
66
70
|
|
67
71
|
To receive an event the listener must implement a method matching the name of
|
68
72
|
the event with a single argument, the instance of the model.
|
@@ -73,33 +77,9 @@ def create_meeting_successful(meeting)
|
|
73
77
|
end
|
74
78
|
```
|
75
79
|
|
76
|
-
|
77
|
-
|
78
|
-
To have event broadcast for success and failure of create and
|
79
|
-
update you must use the `commit` method.
|
80
|
-
|
81
|
-
```ruby
|
82
|
-
meeting.title = 'My Meeting'
|
83
|
-
meeting.commit
|
84
|
-
```
|
85
|
-
|
86
|
-
You can also pass attributes directly to `commit`:
|
87
|
-
|
88
|
-
```ruby
|
89
|
-
meeting.commit(title: 'My Meeting')
|
90
|
-
```
|
91
|
-
|
92
|
-
And use the class method for creating new records:
|
93
|
-
|
94
|
-
```ruby
|
95
|
-
Meeting.commit(title: 'My Meeting')
|
96
|
-
```
|
97
|
-
|
98
|
-
In addition the the regular events broadcast the following events are also broadcast:
|
99
|
-
|
100
|
-
* `{create, update}_<model_name>_{successful, failed}`
|
80
|
+
## Example
|
101
81
|
|
102
|
-
###
|
82
|
+
### Controller
|
103
83
|
|
104
84
|
```ruby
|
105
85
|
class MeetingsController < ApplicationController
|
@@ -112,7 +92,7 @@ class MeetingsController < ApplicationController
|
|
112
92
|
@meeting.subscribe(Auditor.instance)
|
113
93
|
@meeting.on(:meeting_create_successful) { redirect_to meeting_path }
|
114
94
|
@meeting.on(:meeting_create_failed) { render action: :new }
|
115
|
-
@meeting.
|
95
|
+
@meeting.save
|
116
96
|
end
|
117
97
|
|
118
98
|
def edit
|
@@ -124,14 +104,14 @@ class MeetingsController < ApplicationController
|
|
124
104
|
@meeting.subscribe(Auditor.instance)
|
125
105
|
@meeting.on(:meeting_update_successful) { redirect_to meeting_path }
|
126
106
|
@meeting.on(:meeting_update_failed) { render :action => :edit }
|
127
|
-
@meeting.
|
107
|
+
@meeting.update_attributes(params[:meeting])
|
128
108
|
end
|
129
109
|
end
|
130
110
|
```
|
131
111
|
|
132
|
-
|
112
|
+
### Listener
|
133
113
|
|
134
|
-
**Which simply
|
114
|
+
**Which simply records an audit in memory**
|
135
115
|
|
136
116
|
```ruby
|
137
117
|
class Auditor
|
@@ -6,43 +6,51 @@ module Wisper
|
|
6
6
|
|
7
7
|
include Wisper::Publisher
|
8
8
|
|
9
|
-
|
10
|
-
after_commit
|
11
|
-
after_commit
|
9
|
+
after_validation :after_validation_broadcast
|
10
|
+
after_commit :after_create_broadcast, on: :create
|
11
|
+
after_commit :after_update_broadcast, on: :update
|
12
|
+
after_commit :after_destroy_broadcast, on: :destroy
|
13
|
+
after_rollback :after_rollback_broadcast
|
12
14
|
end
|
13
15
|
|
14
16
|
def commit(_attributes = nil)
|
15
|
-
|
17
|
+
warn "[DEPRECATED] use save, create, update_attributes as usual"
|
16
18
|
assign_attributes(_attributes) if _attributes.present?
|
17
|
-
|
18
|
-
if result
|
19
|
-
broadcast("#{_action}_#{self.class.model_name.param_key}_successful", self)
|
20
|
-
else
|
21
|
-
broadcast("#{_action}_#{self.class.model_name.param_key}_failed", self)
|
22
|
-
end
|
23
|
-
result
|
19
|
+
save
|
24
20
|
end
|
25
21
|
|
26
22
|
module ClassMethods
|
27
23
|
def commit(_attributes = nil)
|
28
|
-
|
24
|
+
warn "[DEPRECATED] use save, create, update_attributes as usual"
|
25
|
+
new(_attributes).save
|
29
26
|
end
|
30
27
|
end
|
31
28
|
|
32
29
|
private
|
33
30
|
|
34
|
-
def
|
31
|
+
def after_validation_broadcast
|
32
|
+
action = new_record? ? 'create' : 'update'
|
33
|
+
broadcast("#{action}_#{self.class.model_name.param_key}_failed", self) unless errors.empty?
|
34
|
+
end
|
35
|
+
|
36
|
+
def after_create_broadcast
|
35
37
|
broadcast(:after_create, self)
|
38
|
+
broadcast("create_#{self.class.model_name.param_key}_successful", self)
|
36
39
|
end
|
37
40
|
|
38
|
-
def
|
41
|
+
def after_update_broadcast
|
39
42
|
broadcast(:after_update, self)
|
43
|
+
broadcast("update_#{self.class.model_name.param_key}_successful", self)
|
40
44
|
end
|
41
45
|
|
42
|
-
def
|
46
|
+
def after_destroy_broadcast
|
43
47
|
broadcast(:after_destroy, self)
|
44
48
|
broadcast("destroy_#{self.class.model_name.param_key}_successful", self)
|
45
49
|
end
|
50
|
+
|
51
|
+
def after_rollback_broadcast
|
52
|
+
broadcast(:after_rollback, self)
|
53
|
+
end
|
46
54
|
end
|
47
55
|
end
|
48
56
|
end
|
@@ -9,10 +9,8 @@ describe 'ActiveRecord' do
|
|
9
9
|
expect(Wisper.model).to eq Wisper::ActiveRecord::Publisher
|
10
10
|
end
|
11
11
|
|
12
|
-
describe '.commit' do
|
13
|
-
|
12
|
+
describe '.commit' do # DEPRECATED
|
14
13
|
describe 'when creating' do
|
15
|
-
|
16
14
|
context 'and model is valid' do
|
17
15
|
it 'publishes create_<model_name>_successful event to listener' do
|
18
16
|
expect(listener).to receive(:create_meeting_successful).with(instance_of(model_class))
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wisper-activerecord
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kris Leech
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-10-
|
11
|
+
date: 2014-10-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: wisper
|