eventifier 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -1
- data/.travis.yml +14 -0
- data/Gemfile.lock +17 -3
- data/README.textile +7 -2
- data/Rakefile +1 -8
- data/eventifier.gemspec +7 -3
- data/lib/eventifier/active_record/event.rb +9 -0
- data/lib/eventifier/active_record/event_observer.rb +8 -0
- data/lib/eventifier/active_record/event_tracking.rb +11 -0
- data/lib/eventifier/active_record/ghost.rb +9 -0
- data/lib/eventifier/active_record/notification.rb +13 -0
- data/lib/eventifier/active_record_support.rb +5 -0
- data/lib/eventifier/event_mixin.rb +45 -0
- data/lib/eventifier/event_observer_mixin.rb +44 -0
- data/lib/eventifier/event_tracking.rb +29 -18
- data/lib/eventifier/ghost_mixin.rb +27 -0
- data/lib/eventifier/helper_methods.rb +2 -0
- data/lib/eventifier/mongoid/event.rb +16 -0
- data/lib/eventifier/mongoid/event_observer.rb +7 -0
- data/lib/eventifier/mongoid/event_tracking.rb +13 -0
- data/lib/eventifier/mongoid/ghost.rb +15 -0
- data/lib/eventifier/mongoid/notification.rb +19 -0
- data/lib/eventifier/mongoid/user_patch.rb +3 -0
- data/lib/eventifier/mongoid_support.rb +6 -0
- data/lib/eventifier/notification_helper.rb +4 -13
- data/lib/eventifier/notification_mailer.rb +5 -1
- data/lib/eventifier/notification_mixin.rb +42 -0
- data/lib/eventifier/version.rb +1 -1
- data/lib/eventifier.rb +8 -10
- data/lib/generators/eventifier/install/install_generator.rb +1 -1
- data/spec/event_helper_spec.rb +45 -35
- data/spec/event_observer_spec.rb +1 -6
- data/spec/event_spec.rb +16 -5
- data/spec/event_tracking_spec.rb +20 -0
- data/spec/eventifier_spec.rb +48 -52
- data/spec/fabricators/fabricator.rb +31 -0
- data/spec/ghost_spec.rb +30 -0
- data/spec/helper_methods_spec.rb +2 -2
- data/spec/integration/eventifier_spec.rb +85 -44
- data/spec/notification_helper_spec.rb +13 -6
- data/spec/notification_mailer_spec.rb +6 -1
- data/spec/notification_spec.rb +9 -11
- data/spec/spec_helper.rb +11 -2
- data/spec/support/database_cleaner.rb +3 -0
- data/spec/support/model_helpers.rb +36 -0
- data/spec/{support → test_classes}/active_record_support.rb +9 -0
- data/spec/test_classes/mongoid_support.rb +34 -0
- metadata +94 -24
- data/lib/eventifier/event.rb +0 -35
- data/lib/eventifier/event_observer.rb +0 -35
- data/lib/eventifier/notification.rb +0 -40
- data/spec/support/blueprints.rb +0 -24
data/lib/eventifier.rb
CHANGED
@@ -4,14 +4,10 @@
|
|
4
4
|
# Consider implementing with http://www.ruby-doc.org/stdlib-1.9.3/libdoc/observer/rdoc/Observable.html
|
5
5
|
|
6
6
|
|
7
|
-
|
8
7
|
# init.rb
|
9
8
|
# require 'eventifer'
|
10
9
|
|
11
10
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
11
|
# Todo
|
16
12
|
# - Notifications
|
17
13
|
|
@@ -28,15 +24,17 @@
|
|
28
24
|
# end
|
29
25
|
#
|
30
26
|
# end
|
31
|
-
|
32
|
-
require 'active_record'
|
33
27
|
require 'action_mailer'
|
34
28
|
|
35
|
-
|
29
|
+
if defined? Mongoid
|
30
|
+
require 'eventifier/mongoid_support'
|
31
|
+
elsif defined? ActiveRecord
|
32
|
+
require 'eventifier/active_record_support'
|
33
|
+
end
|
34
|
+
|
36
35
|
require 'eventifier/helper_methods'
|
37
|
-
require 'eventifier/notification'
|
38
36
|
require 'eventifier/notification_mailer'
|
39
37
|
require 'eventifier/notification_helper'
|
40
38
|
require 'eventifier/event_helper'
|
41
|
-
require 'eventifier/
|
42
|
-
|
39
|
+
require 'eventifier/event_tracking'
|
40
|
+
|
data/spec/event_helper_spec.rb
CHANGED
@@ -3,55 +3,65 @@ require 'spec_helper'
|
|
3
3
|
describe Eventifier::EventHelper do
|
4
4
|
|
5
5
|
class TestClass
|
6
|
-
def self.helper_method(*args)
|
6
|
+
def self.helper_method(*args)
|
7
|
+
end
|
8
|
+
|
7
9
|
include Eventifier::EventHelper
|
8
10
|
end
|
9
11
|
|
12
|
+
let(:verb) { "update" }
|
13
|
+
let(:change_data) { { } }
|
14
|
+
let(:event) { double("Event", :verb => verb, :eventable_type => "Object", :change_data => change_data, :user => double("user", :name => "Willy")) }
|
10
15
|
let!(:helper) { TestClass.new }
|
11
16
|
|
12
17
|
before do
|
13
|
-
|
14
|
-
:post => {
|
15
|
-
:create => "{{user.name}} just created a new post - you should check it out",
|
16
|
-
:destroy => "{{user.name}} just deleted a post",
|
17
|
-
:update => {
|
18
|
-
:single => "{{user.name}} made a change to their post",
|
19
|
-
:multiple => "{{user.name}} made some changes to their post"
|
20
|
-
}
|
21
|
-
}
|
22
|
-
}
|
23
|
-
I18n.backend.store_translations :en, :events => @event_strings
|
18
|
+
helper.stub :replace_vars => double("String", :html_safe => true)
|
24
19
|
end
|
25
20
|
|
26
21
|
describe ".event_message" do
|
27
|
-
|
28
|
-
event = Eventifier::Event.make! :eventable => Post.make!, :verb => :create
|
29
|
-
helper.event_message(event).should == "<strong>#{event.user.name}</strong> just created a new post - you should check it out"
|
30
|
-
end
|
22
|
+
subject { helper.event_message event }
|
31
23
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
24
|
+
context "with event verb create" do
|
25
|
+
let(:verb) { "create" }
|
26
|
+
|
27
|
+
it "should hit the I18n verb definition for create & destroy" do
|
28
|
+
I18n.should_receive(:translate).with("events.object.create", :default => :"events.default.create", "user.name" => "Willy", :"event.type" => "Object")
|
36
29
|
|
37
|
-
|
38
|
-
|
39
|
-
|
30
|
+
subject
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should pass the I18n translation to the replace_vars method" do
|
34
|
+
I18n.should_receive(:translate).with("events.object.create", :default => :"events.default.create", "user.name" => "Willy", :"event.type" => "Object").and_return("A message")
|
35
|
+
helper.should_receive(:replace_vars).with "A message", event
|
36
|
+
|
37
|
+
subject
|
38
|
+
end
|
40
39
|
end
|
41
40
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
41
|
+
context "with event verb update" do
|
42
|
+
let(:verb) { "update" }
|
43
|
+
|
44
|
+
context "multiple updates" do
|
45
|
+
let(:change_data) { { :name => ["Lol", "Omg Lol"] } }
|
46
|
+
|
47
|
+
it "should specify single on the verb for the I18n definition on update when there are just a single change" do
|
48
|
+
I18n.should_receive(:translate).with("events.object.update.single", :default => :"events.default.update", "user.name" => "Willy", :"event.type" => "Object")
|
49
|
+
|
50
|
+
subject
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
context "multiple updates" do
|
55
|
+
let(:change_data) { { :name => ["Lol", "Omg Lol"], :address => ["old", "new"] } }
|
56
|
+
|
57
|
+
it "should specify single on the verb for the I18n definition on update when there are just a single change" do
|
58
|
+
I18n.should_receive(:translate).with("events.object.update.multiple", :default => :"events.default.update", "user.name" => "Willy", :"event.type" => "Object")
|
59
|
+
|
60
|
+
subject
|
61
|
+
end
|
62
|
+
|
54
63
|
end
|
55
64
|
end
|
65
|
+
|
56
66
|
end
|
57
67
|
end
|
data/spec/event_observer_spec.rb
CHANGED
@@ -1,14 +1,9 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Eventifier::EventObserver do
|
4
|
-
|
5
|
-
describe "#add_notification" do
|
6
|
-
pending
|
7
|
-
end
|
4
|
+
subject { Eventifier::EventObserver.instance }
|
8
5
|
|
9
6
|
describe "#method_from_relation" do
|
10
|
-
subject { Eventifier::EventObserver.instance }
|
11
|
-
|
12
7
|
it "should call the string as a method when passed a string" do
|
13
8
|
object = double('object', :mouse => 5)
|
14
9
|
|
data/spec/event_spec.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Eventifier::Event do
|
4
|
-
let(:event) {
|
4
|
+
let(:event) { Fabricate(:event) }
|
5
5
|
|
6
6
|
describe "#valid?" do
|
7
7
|
pending
|
@@ -10,14 +10,25 @@ describe Eventifier::Event do
|
|
10
10
|
#it_requires_a :verb
|
11
11
|
end
|
12
12
|
|
13
|
-
describe
|
13
|
+
describe '.add_url' do
|
14
|
+
it "should send add_url to each of it's observers with params" do
|
15
|
+
url_proc = -> activity { [activity.group, activity] }
|
16
|
+
observer = double('Observer')
|
17
|
+
Eventifier::Event.stub :observer_instances => [observer]
|
18
|
+
|
19
|
+
observer.should_receive(:add_url).with(Object, url_proc)
|
20
|
+
|
21
|
+
Eventifier::Event.add_url Object, url_proc
|
22
|
+
end
|
23
|
+
end
|
14
24
|
|
15
|
-
|
16
|
-
let(:
|
25
|
+
describe ".find_all_by_eventable" do
|
26
|
+
let!(:eventable) { Fabricate(:post) }
|
27
|
+
let(:event) { Fabricate(:event, :eventable => eventable) }
|
17
28
|
|
18
29
|
it "should find the associated polymorphic eventable object" do
|
19
30
|
lambda do
|
20
|
-
|
31
|
+
Fabricate(:event, :eventable => Fabricate(:post))
|
21
32
|
event
|
22
33
|
end.should change(Eventifier::Event, :count).by(2)
|
23
34
|
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class Activity; end
|
4
|
+
|
5
|
+
describe Eventifier::EventTracking do
|
6
|
+
let(:event_tracker) { Object.new.extend(Eventifier::EventTracking) }
|
7
|
+
|
8
|
+
describe ".url" do
|
9
|
+
it "should at the url form to a hash" do
|
10
|
+
url_proc = -> activity { [activity.group, activity] }
|
11
|
+
event_tracker.instance_variable_set(:@klasses, [Activity])
|
12
|
+
|
13
|
+
event_tracker.url url_proc
|
14
|
+
|
15
|
+
Eventifier::EventTracking.url_mappings[:activity].should == url_proc
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
data/spec/eventifier_spec.rb
CHANGED
@@ -1,16 +1,11 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
-
|
3
2
|
describe Eventifier::EventTracking do
|
4
3
|
|
5
|
-
let(:user) {
|
4
|
+
let(:user) { Fabricate(:user) }
|
6
5
|
let(:test_class) { Post }
|
7
|
-
let(:object) {
|
6
|
+
let(:object) { Fabricate.build(:post) }
|
8
7
|
let(:event_tracker) { Object.new.extend(Eventifier::EventTracking) }
|
9
8
|
|
10
|
-
after :all do
|
11
|
-
Post.observers.disable :all
|
12
|
-
end
|
13
|
-
|
14
9
|
describe ".initialize" do
|
15
10
|
|
16
11
|
it "should start" do
|
@@ -48,7 +43,7 @@ describe Eventifier::EventTracking do
|
|
48
43
|
|
49
44
|
|
50
45
|
it "should create an event with the relevant info" do
|
51
|
-
changes = {:foo => 'bar', :bar => 'baz'}
|
46
|
+
changes = { :foo => 'bar', :bar => 'baz' }
|
52
47
|
object.stub(:changes).and_return(changes)
|
53
48
|
|
54
49
|
Eventifier::Event.should_receive(:create).with(:user => user, :eventable => object, :verb => :create, :change_data => changes)
|
@@ -61,28 +56,28 @@ describe Eventifier::EventTracking do
|
|
61
56
|
|
62
57
|
describe '#create_event' do
|
63
58
|
let(:object) { double('object', :changes => changes, :user => double(User)) }
|
64
|
-
let(:changes) { {:foo => 'bar', :bar => 'baz', :bob => 'blah'} }
|
65
|
-
let(:options) { {} }
|
59
|
+
let(:changes) { { :foo => 'bar', :bar => 'baz', :bob => 'blah' } }
|
60
|
+
let(:options) { { } }
|
66
61
|
|
67
62
|
subject { Eventifier::Event.create_event(:create, object, options) }
|
68
63
|
|
69
64
|
describe "exclude" do
|
70
|
-
let(:options) { {:except => ['foo']} }
|
65
|
+
let(:options) { { :except => ['foo'] } }
|
71
66
|
|
72
67
|
it 'should exclude the right attrs' do
|
73
68
|
Eventifier::Event.should_receive(:create) do |attrs|
|
74
|
-
attrs[:change_data].should == {:bar => 'baz', :bob => 'blah'}
|
69
|
+
attrs[:change_data].should == { :bar => 'baz', :bob => 'blah' }
|
75
70
|
end
|
76
71
|
subject
|
77
72
|
end
|
78
73
|
|
79
74
|
end
|
80
75
|
describe "only" do
|
81
|
-
let(:options) { {:only => ['foo']} }
|
76
|
+
let(:options) { { :only => ['foo'] } }
|
82
77
|
|
83
78
|
it 'should exclude the right attrs' do
|
84
79
|
Eventifier::Event.should_receive(:create) do |attrs|
|
85
|
-
attrs[:change_data].should == {:foo => 'bar'}
|
80
|
+
attrs[:change_data].should == { :foo => 'bar' }
|
86
81
|
end
|
87
82
|
subject
|
88
83
|
end
|
@@ -104,11 +99,11 @@ describe Eventifier::EventTracking do
|
|
104
99
|
end
|
105
100
|
|
106
101
|
it "should create an event with the relevant info" do
|
107
|
-
changes = {:foo => 'bar', :bar => 'baz'}
|
102
|
+
changes = { :foo => 'bar', :bar => 'baz' }
|
108
103
|
object.stub(:changes).and_return(changes)
|
109
104
|
event_tracker.events_for test_class do
|
110
|
-
track_on [:create, :destroy], :attributes => {:except => %w(updated_at)}
|
111
|
-
track_on :update, :attributes => {:except => %w(updated_at)}
|
105
|
+
track_on [:create, :destroy], :attributes => { :except => %w(updated_at) }
|
106
|
+
track_on :update, :attributes => { :except => %w(updated_at) }
|
112
107
|
end
|
113
108
|
|
114
109
|
Eventifier::Event.should_receive(:create).with(:user => user, :eventable => object, :verb => :create, :change_data => changes)
|
@@ -120,60 +115,61 @@ describe Eventifier::EventTracking do
|
|
120
115
|
event_tracker.should_receive(:track_on).exactly(2).times
|
121
116
|
|
122
117
|
event_tracker.events_for test_class do
|
123
|
-
track_on [:create, :destroy], :attributes => {:except => %w(updated_at)}
|
124
|
-
track_on :update, :attributes => {:except => %w(updated_at)}
|
118
|
+
track_on [:create, :destroy], :attributes => { :except => %w(updated_at) }
|
119
|
+
track_on :update, :attributes => { :except => %w(updated_at) }
|
125
120
|
end
|
126
121
|
end
|
127
|
-
end
|
128
122
|
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
123
|
+
context "notifying" do
|
124
|
+
before do
|
125
|
+
@event_observer_instance = Eventifier::EventObserver.instance
|
126
|
+
Eventifier::EventObserver.any_instance.stub post_path: '/post'
|
127
|
+
end
|
133
128
|
|
134
|
-
|
129
|
+
describe "#add_notification" do
|
135
130
|
|
136
|
-
|
137
|
-
|
131
|
+
it "should add the notification to the notification hash" do
|
132
|
+
@event_observer_instance.should_receive(:add_notification).with(test_class, :user, :create)
|
138
133
|
|
139
|
-
|
140
|
-
|
141
|
-
|
134
|
+
event_tracker.events_for test_class do
|
135
|
+
track_on :create, :attributes => { :except => %w(updated_at) }
|
136
|
+
notify :user, :on => :create
|
137
|
+
end
|
142
138
|
end
|
143
139
|
end
|
144
|
-
end
|
145
140
|
|
146
|
-
|
147
|
-
|
141
|
+
it "should notify users when passed a hash" do
|
142
|
+
subscriber = double('user')
|
148
143
|
|
149
|
-
|
144
|
+
object.stub :category => double('category', :subscribers => [subscriber])
|
150
145
|
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
146
|
+
Eventifier::Notification.should_receive(:create) do |args|
|
147
|
+
args[:user].should == subscriber
|
148
|
+
args[:event].eventable.should == object
|
149
|
+
end
|
155
150
|
|
156
|
-
|
157
|
-
|
158
|
-
|
151
|
+
event_tracker.events_for test_class do
|
152
|
+
track_on :create, :attributes => { :except => %w(updated_at) }
|
153
|
+
notify :category => :subscribers, :on => :create
|
154
|
+
end
|
155
|
+
|
156
|
+
object.save
|
159
157
|
end
|
160
158
|
|
161
|
-
object.save
|
162
|
-
end
|
163
159
|
|
160
|
+
it "should create a notification" do
|
161
|
+
Eventifier::Notification.should_receive(:create)
|
162
|
+
object.stub(:readers => [Fabricate.build(:user)])
|
163
|
+
event_tracker.events_for test_class do
|
164
|
+
track_on :create, :attributes => { :except => %w(updated_at) }
|
165
|
+
notify :readers, :on => :create
|
166
|
+
end
|
164
167
|
|
165
|
-
|
166
|
-
Eventifier::Notification.should_receive(:create)
|
167
|
-
object.stub(:users => [User.make])
|
168
|
-
event_tracker.events_for test_class do
|
169
|
-
track_on :create, :attributes => {:except => %w(updated_at)}
|
170
|
-
notify :readers, :on => :create
|
168
|
+
object.save
|
171
169
|
end
|
172
170
|
|
173
|
-
object.save
|
174
171
|
end
|
175
|
-
|
176
172
|
end
|
177
|
-
end
|
178
173
|
|
174
|
+
end
|
179
175
|
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'eventifier'
|
2
|
+
|
3
|
+
|
4
|
+
Fabricator(:event, :class_name => Eventifier::Event) do
|
5
|
+
user!
|
6
|
+
eventable!(:fabricator => :post)
|
7
|
+
verb :update
|
8
|
+
change_data { { :date => [5.days.ago, 3.days.ago] } }
|
9
|
+
end
|
10
|
+
|
11
|
+
Fabricator(:ghost, :class_name => Eventifier::Ghost) do
|
12
|
+
ghost_class { "Post" }
|
13
|
+
ghost_id { 123 }
|
14
|
+
data_hash { Fabricate.attributes_for(:post) }
|
15
|
+
end
|
16
|
+
|
17
|
+
Fabricator(:notification, :class_name => Eventifier::Notification) do
|
18
|
+
event! { Fabricate(:event) }
|
19
|
+
user!
|
20
|
+
end
|
21
|
+
|
22
|
+
Fabricator(:post, :class_name => Post) do
|
23
|
+
title { "My amazing blog post" }
|
24
|
+
body { "A deep and profound analysis of life" }
|
25
|
+
author! { Fabricate(:user) }
|
26
|
+
end
|
27
|
+
|
28
|
+
Fabricator(:user, :class_name => User) do
|
29
|
+
name { "Billy #{sequence(:name, 1)}" }
|
30
|
+
email{ "billy#{sequence(:email, 1)}@email.com" }
|
31
|
+
end
|
data/spec/ghost_spec.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Eventifier::Ghost do
|
4
|
+
|
5
|
+
it_requires_a :ghost_class
|
6
|
+
it_requires_a :ghost_id
|
7
|
+
it_requires_a :data_hash
|
8
|
+
|
9
|
+
describe ".create_from_object" do
|
10
|
+
subject { Eventifier::Ghost }
|
11
|
+
let(:post) { Fabricate(:post) }
|
12
|
+
|
13
|
+
it "should create a ghost when passed an object" do
|
14
|
+
subject.should_receive(:create).with(:ghost_class => "Post", :ghost_id => post.id, :data_hash => post.serializable_hash)
|
15
|
+
|
16
|
+
subject.create_from_object(post)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "#ghost" do
|
21
|
+
let(:post) { Fabricate.build(:post, :id => 123) }
|
22
|
+
subject { Fabricate.build(:ghost, :data_hash => post.serializable_hash) }
|
23
|
+
|
24
|
+
it "should be an object with the attributes of the undeleted object" do
|
25
|
+
subject.ghost.class.should == Post
|
26
|
+
subject.ghost.attributes.except("_type").should == post.serializable_hash
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
data/spec/helper_methods_spec.rb
CHANGED
@@ -10,7 +10,7 @@ describe Eventifier::EventHelper do
|
|
10
10
|
let!(:helper) { TestClass.new }
|
11
11
|
|
12
12
|
describe ".replace_vars" do
|
13
|
-
let(:event) {
|
13
|
+
let(:event) { Fabricate.build(:event)}
|
14
14
|
|
15
15
|
it "should replace {{stuff}} with awesome" do
|
16
16
|
message = "I'm really loving {{eventable.title}}"
|
@@ -26,7 +26,7 @@ describe Eventifier::EventHelper do
|
|
26
26
|
describe ".load_event_for_template" do
|
27
27
|
|
28
28
|
it "should add some handy methods to an event instance" do
|
29
|
-
event =
|
29
|
+
event = Fabricate(:event)
|
30
30
|
event = helper.load_event_for_template event
|
31
31
|
event.object.should == event.eventable
|
32
32
|
event.object_type.should == event.eventable_type
|
@@ -1,67 +1,108 @@
|
|
1
|
+
require 'spec_helper'
|
1
2
|
|
2
3
|
describe Eventifier do
|
3
|
-
let(:post) {
|
4
|
-
let(:
|
5
|
-
|
6
|
-
let(:
|
7
|
-
let(:member) { double('member') }
|
8
|
-
|
9
|
-
before :each do
|
10
|
-
Eventifier::Notification.stub :create => true
|
11
|
-
end
|
4
|
+
let(:post) { Fabricate(:post, :author => owner) }
|
5
|
+
let(:owner) { Fabricate(:user) }
|
6
|
+
let(:reader1) { Fabricate(:user) }
|
7
|
+
let(:reader2) { Fabricate(:user) }
|
12
8
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
9
|
+
|
10
|
+
let(:event_tracker) { Object.new.extend(Eventifier::EventTracking) }
|
11
|
+
|
12
|
+
|
13
|
+
before do
|
14
|
+
Eventifier::NotificationMailer.any_instance.stub main_app: double('app', url_for: true)
|
15
|
+
post.stub(:readers => [owner, reader1, reader2])
|
16
|
+
|
17
|
+
event_tracker.events_for Post do
|
18
|
+
track_on [:create, :update], :attributes => { :except => %w(updated_at) }
|
19
|
+
notify :readers, :on => [:create, :update]
|
23
20
|
end
|
21
|
+
end
|
24
22
|
|
25
|
-
|
26
|
-
|
27
|
-
with(:user => owner, :event => event)
|
23
|
+
context 'a new post' do
|
24
|
+
let(:post) { Fabricate.build(:post, :author => owner) }
|
28
25
|
|
29
|
-
|
26
|
+
it "notifies only the readers of the post" do
|
27
|
+
Eventifier::Notification.should_receive(:create).twice do |args|
|
28
|
+
args[:event].verb.should == :create
|
29
|
+
[reader1, reader2].should include(args[:user])
|
30
|
+
end
|
31
|
+
post.save
|
30
32
|
end
|
31
33
|
end
|
32
34
|
|
33
35
|
context 'an existing post' do
|
36
|
+
let(:post) { Fabricate(:post, :author => owner) }
|
34
37
|
let(:event) { Eventifier::Event.new :eventable => post, :verb => :update,
|
35
|
-
|
36
|
-
let(:guest) { double('guest') }
|
38
|
+
:user => owner }
|
37
39
|
|
38
|
-
|
39
|
-
|
40
|
+
it "notifies the readers of the post" do
|
41
|
+
Eventifier::Notification.should_receive(:create).twice do |args|
|
42
|
+
args[:event].verb.should == :update
|
43
|
+
[reader1, reader2].should include(args[:user])
|
44
|
+
end
|
45
|
+
post.update_attribute(:title, 'something else')
|
40
46
|
end
|
41
47
|
|
42
|
-
it "
|
43
|
-
|
44
|
-
with(:user => guest, :event => event)
|
45
|
-
ActiveRecord::Observer.with_observers(Eventifier::EventObserver) do
|
46
|
-
event.save
|
47
|
-
end
|
48
|
+
it "should create a notification for readers of a post when it's changed" do
|
49
|
+
lambda { post.update_attribute(:title, 'somethang') }.should change(reader1.notifications, :count).by(1)
|
48
50
|
end
|
51
|
+
end
|
49
52
|
|
50
|
-
|
51
|
-
Eventifier::Notification.should_not_receive(:create).
|
52
|
-
with(:user => owner, :event => event)
|
53
|
+
context "helper method" do
|
53
54
|
|
54
|
-
|
55
|
-
|
55
|
+
class TestClass
|
56
|
+
def self.helper_method(*args)
|
56
57
|
end
|
58
|
+
|
59
|
+
include Eventifier::EventHelper
|
60
|
+
end
|
61
|
+
|
62
|
+
let!(:helper) { TestClass.new }
|
63
|
+
before do
|
64
|
+
@event_strings = {
|
65
|
+
:post => {
|
66
|
+
:create => "{{user.name}} just created a new post - you should check it out",
|
67
|
+
:destroy => "{{user.name}} just deleted a post",
|
68
|
+
:update => {
|
69
|
+
:single => "{{user.name}} made a change to their post",
|
70
|
+
:multiple => "{{user.name}} made some changes to their post"
|
71
|
+
}
|
72
|
+
}
|
73
|
+
}
|
74
|
+
I18n.backend.store_translations :en, :events => @event_strings
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should return the I18n message for that event" do
|
78
|
+
event = Fabricate(:event, :eventable => Fabricate(:post), :verb => :create)
|
79
|
+
helper.event_message(event).should == "<strong>#{event.user.name}</strong> just created a new post - you should check it out"
|
57
80
|
end
|
58
|
-
end
|
59
|
-
|
60
|
-
it "should create a notification for users of a post when it's changed" do
|
61
|
-
post = event.eventable
|
62
|
-
user = User.make!
|
63
81
|
|
64
|
-
|
82
|
+
it "should return a message specific to a single change if only 1 change has been made" do
|
83
|
+
event = Fabricate(:event, :eventable => Fabricate(:post), :verb => :update, :change_data => { :name => ["Fred", "Mike"] })
|
84
|
+
helper.event_message(event).should == "<strong>#{event.user.name}</strong> made a change to their post"
|
85
|
+
end
|
86
|
+
|
87
|
+
it "should return a message specific to multiple changes if more than 1 change has been made" do
|
88
|
+
event = Fabricate(:event, :eventable => Fabricate(:post), :verb => :update, :change_data => { :name => ["Fred", "Mike"], :age => [55, 65] })
|
89
|
+
helper.event_message(event).should == "<strong>#{event.user.name}</strong> made some changes to their post"
|
90
|
+
end
|
91
|
+
|
92
|
+
it "should return the default I18n message if one doesn't exist" do
|
93
|
+
I18n.backend.reload!
|
94
|
+
@event_strings = {
|
95
|
+
:default => {
|
96
|
+
:create => "{{user.name}} created a {{eventable_type}}",
|
97
|
+
:update => "{{user.name}} updated a {{eventable_type}}"
|
98
|
+
}
|
99
|
+
}
|
100
|
+
I18n.backend.store_translations :test, :events => @event_strings
|
101
|
+
I18n.with_locale("test") do
|
102
|
+
event = Fabricate(:event, :eventable => Fabricate(:post), :verb => :create)
|
103
|
+
helper.event_message(event).should == "<strong>#{event.user.name}</strong> created a <strong>Post</strong>"
|
104
|
+
end
|
105
|
+
end
|
65
106
|
end
|
66
107
|
|
67
108
|
end
|