eventifier 0.0.7 → 0.0.8
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 +7 -0
- data/.gitignore +1 -0
- data/.travis.yml +7 -8
- data/Gemfile +1 -1
- data/README.textile +44 -3
- data/app/assets/images/eventifier/notifications-bell.png +0 -0
- data/app/assets/images/eventifier/notifications-tower.png +0 -0
- data/app/assets/javascripts/eventifier/notifications.coffee +196 -0
- data/app/assets/javascripts/eventifier/templates/dropdown.hamlc +10 -0
- data/app/assets/javascripts/eventifier/templates/settings.hamlc +11 -0
- data/app/assets/stylesheets/eventifier/notifications.scss +167 -0
- data/app/controllers/eventifier/application_controller.rb +4 -0
- data/app/controllers/eventifier/notifications_controller.rb +36 -0
- data/app/controllers/eventifier/preferences_controller.rb +19 -0
- data/app/helpers/eventifier/notification_helper.rb +87 -0
- data/app/helpers/eventifier/path_helper.rb +15 -0
- data/app/mailers/eventifier/mailer.rb +12 -0
- data/app/models/event_tracking.rb +3 -0
- data/app/models/eventifier/event.rb +22 -0
- data/app/models/eventifier/ghost.rb +21 -0
- data/app/models/eventifier/notification.rb +38 -0
- data/app/models/eventifier/notification_setting.rb +14 -0
- data/app/views/eventifier/_notification.haml +1 -0
- data/app/views/eventifier/email/_notification.haml +2 -0
- data/app/views/eventifier/mailer/notifications.haml +5 -0
- data/app/views/eventifier/notifications/index.jbuilder +6 -0
- data/config/routes.rb +7 -0
- data/config.ru +7 -0
- data/{lib/generators/eventifier/install/templates/migration.rb → db/migrate/1_eventifier_setup.rb} +2 -3
- data/db/migrate/2_eventifier_notification_settings.rb +11 -0
- data/db/migrate/3_notification_sent_status.rb +15 -0
- data/db/migrate/4_event_groupable.rb +19 -0
- data/eventifier.gemspec +12 -15
- data/lib/eventifier/delivery.rb +59 -0
- data/lib/eventifier/{railtie.rb → engine.rb} +4 -2
- data/lib/eventifier/event_subscriber.rb +44 -0
- data/lib/eventifier/event_tracking.rb +5 -58
- data/lib/eventifier/mailers/helpers.rb +70 -0
- data/lib/eventifier/notifier/notification_mapping.rb +34 -0
- data/lib/eventifier/notifier/notification_subscriber.rb +23 -0
- data/lib/eventifier/notifier/notifier.rb +21 -0
- data/lib/eventifier/preferences.rb +44 -0
- data/lib/eventifier/relationship.rb +36 -0
- data/lib/eventifier/trackable_class.rb +74 -0
- data/lib/eventifier/tracker.rb +18 -0
- data/lib/eventifier.rb +35 -11
- data/lib/generators/eventifier/install/install_generator.rb +2 -5
- data/lib/generators/eventifier/install/templates/events.en.yaml +1 -0
- data/lib/tasks/email.rake +8 -0
- data/spec/controllers/eventifier/notifications_controller_spec.rb +24 -0
- data/spec/controllers/eventifier/preferences_controller_spec.rb +56 -0
- data/spec/eventifier/delivery_spec.rb +110 -0
- data/spec/eventifier/preferences_spec.rb +69 -0
- data/spec/eventifier/relationship_spec.rb +33 -0
- data/spec/eventifier_spec.rb +6 -48
- data/spec/fabricators/fabricator.rb +11 -11
- data/spec/helpers/eventifier/notification_helper_spec.rb +169 -0
- data/spec/helpers/eventifier/path_helper_spec.rb +19 -0
- data/spec/integration/eventifier_spec.rb +113 -70
- data/spec/integration/internationalisation_spec.rb +39 -0
- data/spec/internal/app/controllers/application_controller.rb +3 -0
- data/spec/internal/app/lib/event_tracking.rb +12 -0
- data/spec/internal/app/models/category.rb +3 -0
- data/spec/internal/app/models/post.rb +8 -0
- data/spec/internal/app/models/subscription.rb +4 -0
- data/spec/internal/app/models/user.rb +4 -0
- data/spec/internal/app/views/eventifier/dropdown/_awesome_object.haml +0 -0
- data/spec/internal/config/database.yml +4 -0
- data/spec/internal/config/initializers/eventifier.rb +1 -0
- data/spec/internal/config/locales/events.en.yml +20 -0
- data/spec/internal/config/routes.rb +5 -0
- data/spec/internal/db/schema.rb +31 -0
- data/spec/internal/log/.gitignore +1 -0
- data/spec/internal/public/favicon.ico +0 -0
- data/spec/models/eventifier/event_spec.rb +23 -0
- data/spec/{ghost_spec.rb → models/eventifier/ghost_spec.rb} +3 -3
- data/spec/{notification_spec.rb → models/eventifier/notification_spec.rb} +1 -9
- data/spec/notification_mailer_spec.rb +8 -4
- data/spec/notifier/notification_mapping_spec.rb +28 -0
- data/spec/spec_helper.rb +14 -8
- data/spec/support/controller_helpers.rb +14 -0
- metadata +205 -128
- data/Gemfile.lock +0 -97
- data/lib/eventifier/active_record/event.rb +0 -10
- data/lib/eventifier/active_record/event_observer.rb +0 -8
- data/lib/eventifier/active_record/event_tracking.rb +0 -11
- data/lib/eventifier/active_record/ghost.rb +0 -9
- data/lib/eventifier/active_record/notification.rb +0 -13
- data/lib/eventifier/active_record_support.rb +0 -5
- data/lib/eventifier/event_helper.rb +0 -40
- data/lib/eventifier/event_mixin.rb +0 -45
- data/lib/eventifier/event_observer_mixin.rb +0 -44
- data/lib/eventifier/ghost_mixin.rb +0 -27
- data/lib/eventifier/helper_methods.rb +0 -36
- data/lib/eventifier/mongoid/event.rb +0 -16
- data/lib/eventifier/mongoid/event_observer.rb +0 -7
- data/lib/eventifier/mongoid/event_tracking.rb +0 -13
- data/lib/eventifier/mongoid/ghost.rb +0 -15
- data/lib/eventifier/mongoid/notification.rb +0 -19
- data/lib/eventifier/mongoid/user_patch.rb +0 -3
- data/lib/eventifier/mongoid_support.rb +0 -6
- data/lib/eventifier/notification_helper.rb +0 -42
- data/lib/eventifier/notification_mailer.rb +0 -18
- data/lib/eventifier/notification_mixin.rb +0 -50
- data/lib/eventifier/version.rb +0 -3
- data/spec/event_helper_spec.rb +0 -67
- data/spec/event_observer_spec.rb +0 -20
- data/spec/event_spec.rb +0 -39
- data/spec/helper_methods_spec.rb +0 -36
- data/spec/notification_helper_spec.rb +0 -65
- data/spec/support/action_mailer.rb +0 -3
- data/spec/support/database_cleaner.rb +0 -3
- data/spec/test_classes/active_record_support.rb +0 -72
- data/spec/test_classes/mongoid_support.rb +0 -34
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
class Eventifier::Relationship
|
|
2
|
+
def initialize(source, relation)
|
|
3
|
+
@source, @relation = source, relation
|
|
4
|
+
end
|
|
5
|
+
|
|
6
|
+
def key
|
|
7
|
+
key_from relation
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def users
|
|
11
|
+
Array object.send(method)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
private
|
|
15
|
+
|
|
16
|
+
attr_reader :relation, :source
|
|
17
|
+
|
|
18
|
+
def object
|
|
19
|
+
relation.is_a?(Hash) ? source.send(relation.keys.first) : source
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def method
|
|
23
|
+
relation.is_a?(Hash) ? relation.values.first : relation
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def key_from(object)
|
|
27
|
+
case object
|
|
28
|
+
when Hash
|
|
29
|
+
"#{object.keys.first}_#{key_from object.values.first}"
|
|
30
|
+
when Array
|
|
31
|
+
object.join('-')
|
|
32
|
+
else
|
|
33
|
+
object.to_s
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
class Eventifier::TrackableClass
|
|
2
|
+
Eventifier::OBSERVER_CLASS = ActiveRecord::Observer
|
|
3
|
+
|
|
4
|
+
def self.track(klass, klass_methods, attributes)
|
|
5
|
+
self.new(klass, klass_methods, attributes).track
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def initialize(klass, klass_methods, options)
|
|
9
|
+
@klass, @klass_methods = klass, klass_methods
|
|
10
|
+
|
|
11
|
+
@attributes = options.delete(:attributes) || {}
|
|
12
|
+
@owner = options.delete :owner
|
|
13
|
+
@group_by = options.delete :group_by
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def track
|
|
17
|
+
add_relations
|
|
18
|
+
generate_observer_callbacks
|
|
19
|
+
|
|
20
|
+
observer.instance
|
|
21
|
+
|
|
22
|
+
Eventifier::EventSubscriber.subscribe_to_all @klass, @klass_methods
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
private
|
|
26
|
+
|
|
27
|
+
def add_relations
|
|
28
|
+
@klass.class_eval do
|
|
29
|
+
has_many :events, as: :eventable, class_name: 'Eventifier::Event',
|
|
30
|
+
dependent: :destroy
|
|
31
|
+
has_many :notifications, through: :events,
|
|
32
|
+
class_name: 'Eventifier::Notification'
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def generate_observer_callbacks
|
|
37
|
+
methods, attributes, klass, owner, group_by =
|
|
38
|
+
@klass_methods, @attributes, @klass, @owner, @group_by
|
|
39
|
+
|
|
40
|
+
observer.class_eval do
|
|
41
|
+
methods.each do |method|
|
|
42
|
+
define_method "after_#{method}" do |object|
|
|
43
|
+
Rails.logger.debug "||ASN|| Instrument #{method}.#{klass.name.tableize}" if defined?(Rails)
|
|
44
|
+
ActiveSupport::Notifications.instrument(
|
|
45
|
+
"#{method}.#{klass.name.tableize}.event.eventifier",
|
|
46
|
+
event: method.to_sym,
|
|
47
|
+
object: object,
|
|
48
|
+
options: attributes,
|
|
49
|
+
user: owner,
|
|
50
|
+
group_by: group_by
|
|
51
|
+
) if object.changed?
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def observer
|
|
58
|
+
@observer ||= begin
|
|
59
|
+
observer_klass.observe @klass
|
|
60
|
+
|
|
61
|
+
observer_klass
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def observer_klass
|
|
66
|
+
@observer_klass ||= if self.class.const_defined?("#{@klass}Observer")
|
|
67
|
+
self.class.const_get("#{@klass}Observer")
|
|
68
|
+
else
|
|
69
|
+
constant_name = "#{@klass}Observer"
|
|
70
|
+
klass = Class.new(Eventifier::OBSERVER_CLASS)
|
|
71
|
+
self.class.qualified_const_set(constant_name, klass)
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
module Eventifier
|
|
2
|
+
class Tracker
|
|
3
|
+
def initialize klasses, methods, options
|
|
4
|
+
@klasses = klasses
|
|
5
|
+
methods = methods.kind_of?(Array) ? methods : [methods]
|
|
6
|
+
raise 'No events defined to track' if methods.compact.empty?
|
|
7
|
+
|
|
8
|
+
User.class_eval {
|
|
9
|
+
has_many :notifications, class_name: 'Eventifier::Notification'
|
|
10
|
+
} unless User.respond_to?(:notifications)
|
|
11
|
+
|
|
12
|
+
# set up each class with an observer and relationships
|
|
13
|
+
@klasses.each do |target_klass|
|
|
14
|
+
Eventifier::TrackableClass.track target_klass, methods, options
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
data/lib/eventifier.rb
CHANGED
|
@@ -16,27 +16,51 @@
|
|
|
16
16
|
|
|
17
17
|
# class EventTracking
|
|
18
18
|
# include Eventable::EventTracking
|
|
19
|
-
#
|
|
19
|
+
#
|
|
20
20
|
# def initialize
|
|
21
21
|
# events_for Activity,
|
|
22
22
|
# :on => [:create, :update, :destroy],
|
|
23
23
|
# :attributes => { :except => %w(updated_at) }
|
|
24
24
|
# end
|
|
25
|
-
#
|
|
25
|
+
#
|
|
26
26
|
# end
|
|
27
|
+
|
|
28
|
+
require 'multi_json'
|
|
27
29
|
require 'action_mailer'
|
|
30
|
+
require 'haml-rails'
|
|
31
|
+
|
|
32
|
+
module Eventifier
|
|
33
|
+
mattr_accessor :mailer_sender
|
|
34
|
+
self.mailer_sender = nil
|
|
35
|
+
|
|
36
|
+
mattr_accessor :mailer_name
|
|
37
|
+
self.mailer_name = "::Eventifier::Mailer"
|
|
28
38
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
39
|
+
def self.setup
|
|
40
|
+
yield self
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def self.mailer
|
|
44
|
+
ActiveSupport::Dependencies.constantize(@@mailer_name)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def self.tracked_classes
|
|
48
|
+
@tracked_classes ||= []
|
|
49
|
+
end
|
|
33
50
|
end
|
|
34
51
|
|
|
35
|
-
require 'eventifier/
|
|
36
|
-
require 'eventifier/
|
|
37
|
-
require 'eventifier/notification_helper'
|
|
38
|
-
require 'eventifier/event_helper'
|
|
52
|
+
require 'eventifier/tracker'
|
|
53
|
+
require 'eventifier/delivery'
|
|
39
54
|
require 'eventifier/event_tracking'
|
|
55
|
+
require 'eventifier/trackable_class'
|
|
56
|
+
require 'eventifier/event_subscriber'
|
|
57
|
+
require 'eventifier/preferences'
|
|
58
|
+
require 'eventifier/relationship'
|
|
59
|
+
|
|
60
|
+
require 'eventifier/notifier/notification_mapping'
|
|
61
|
+
require 'eventifier/notifier/notification_subscriber'
|
|
62
|
+
require 'eventifier/notifier/notifier'
|
|
40
63
|
|
|
41
|
-
require 'eventifier/
|
|
64
|
+
require 'eventifier/mailers/helpers'
|
|
42
65
|
|
|
66
|
+
require 'eventifier/engine' if defined?(Rails)
|
|
@@ -3,9 +3,6 @@ require 'rails/generators/active_record'
|
|
|
3
3
|
module Eventifier
|
|
4
4
|
module Generators
|
|
5
5
|
class InstallGenerator < Rails::Generators::Base
|
|
6
|
-
include Rails::Generators::Migration
|
|
7
|
-
extend ActiveRecord::Generators::Migration
|
|
8
|
-
|
|
9
6
|
source_root File.expand_path('../templates', __FILE__)
|
|
10
7
|
|
|
11
8
|
def copy_event_tracking
|
|
@@ -16,8 +13,8 @@ module Eventifier
|
|
|
16
13
|
copy_file "events.en.yaml", "config/locales/events.en.yaml"
|
|
17
14
|
end
|
|
18
15
|
|
|
19
|
-
def
|
|
20
|
-
|
|
16
|
+
def add_routes
|
|
17
|
+
route %Q{mount Eventifier::Engine => '/'}
|
|
21
18
|
end
|
|
22
19
|
|
|
23
20
|
end
|
|
@@ -6,6 +6,7 @@ en:
|
|
|
6
6
|
single: "{{user.name}} updated a {{object_type}}"
|
|
7
7
|
multiple: "{{user.name}} made some changes to a {{object_type}}"
|
|
8
8
|
notifications:
|
|
9
|
+
email_subject: "You have received a notification"
|
|
9
10
|
default:
|
|
10
11
|
create: "{{user.name}} created a <strong>{{object_type}}</strong>"
|
|
11
12
|
update:
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe Eventifier::NotificationsController do
|
|
4
|
+
describe '#touch' do
|
|
5
|
+
let(:user) { double 'User', :update_attribute => true }
|
|
6
|
+
|
|
7
|
+
before :each do
|
|
8
|
+
sign_in user
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
it "updates the current user's notifications last read at" do
|
|
12
|
+
user.should_receive(:update_attribute).
|
|
13
|
+
with(:notifications_last_read_at, anything)
|
|
14
|
+
|
|
15
|
+
post :touch
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
it "responds with JSON OK status" do
|
|
19
|
+
post :touch
|
|
20
|
+
|
|
21
|
+
response.body.should == {'status' => 'OK'}.to_json
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe Eventifier::PreferencesController do
|
|
4
|
+
let(:user) { double 'User' }
|
|
5
|
+
|
|
6
|
+
before :each do
|
|
7
|
+
sign_in user
|
|
8
|
+
|
|
9
|
+
Eventifier::Preferences.stub :new => preferences
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
describe '#show' do
|
|
13
|
+
let(:preferences) { double :to_hashes => [{'foo' => 'bar'}] }
|
|
14
|
+
|
|
15
|
+
it "returns the settings hashes" do
|
|
16
|
+
get :show
|
|
17
|
+
|
|
18
|
+
response.body.should == [{'foo' => 'bar'}].to_json
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
describe '#update' do
|
|
23
|
+
let(:preferences) { double to_hashes: [{:key => 'foo'}, {:key => 'bar'}] }
|
|
24
|
+
let(:settings) { double 'Settings', :preferences => {}, :save => true }
|
|
25
|
+
|
|
26
|
+
before :each do
|
|
27
|
+
Eventifier::NotificationSetting.stub :for_user => settings
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
it "updates the user's email preferences" do
|
|
31
|
+
put :update, :preferences => {'foo' => ''}
|
|
32
|
+
|
|
33
|
+
settings.preferences['email']['foo'].should be_true
|
|
34
|
+
settings.preferences['email']['bar'].should be_false
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
it "sets everything to false if no preferences are supplied" do
|
|
38
|
+
put :update
|
|
39
|
+
|
|
40
|
+
settings.preferences['email']['foo'].should be_false
|
|
41
|
+
settings.preferences['email']['bar'].should be_false
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
it 'saves the settings changes' do
|
|
45
|
+
settings.should_receive(:save)
|
|
46
|
+
|
|
47
|
+
put :update, :preferences => {'foo' => ''}
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
it "renders a JSON OK status" do
|
|
51
|
+
put :update, :preferences => {'foo' => ''}
|
|
52
|
+
|
|
53
|
+
response.body.should == {'status' => 'OK'}.to_json
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe Eventifier::Delivery do
|
|
4
|
+
describe '#deliver' do
|
|
5
|
+
let(:delivery) { Eventifier::Delivery.new double, [notification] }
|
|
6
|
+
let(:notification) { double 'Notification', relations: [:subscribers], event: event, update_attribute: true }
|
|
7
|
+
let(:event) { double 'Event', verb: 'create',
|
|
8
|
+
eventable_type: 'Post' }
|
|
9
|
+
let(:email) { double 'Email', deliver: true }
|
|
10
|
+
let(:settings) { double 'Settings', preferences: {} }
|
|
11
|
+
let(:mailer) { double 'Mailer', notifications: email }
|
|
12
|
+
|
|
13
|
+
before :each do
|
|
14
|
+
Eventifier.stub mailer: mailer
|
|
15
|
+
Eventifier::NotificationSetting.stub for_user: settings
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
shared_examples_for 'a delivered email' do
|
|
19
|
+
it "sends the email" do
|
|
20
|
+
email.should_receive(:deliver)
|
|
21
|
+
|
|
22
|
+
delivery.deliver
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
it "updates the sent status of the notification" do
|
|
26
|
+
notification.should_receive(:update_attribute).with(:sent, true)
|
|
27
|
+
|
|
28
|
+
delivery.deliver
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
shared_examples_for 'a blocked email' do
|
|
33
|
+
it "does not send the email" do
|
|
34
|
+
email.should_not_receive(:deliver)
|
|
35
|
+
|
|
36
|
+
delivery.deliver
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
it "updates the sent status of the notification" do
|
|
40
|
+
notification.should_receive(:update_attribute).with(:sent, true)
|
|
41
|
+
|
|
42
|
+
delivery.deliver
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
context 'no settings' do
|
|
47
|
+
before :each do
|
|
48
|
+
settings.preferences['email'] = {}
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
it_should_behave_like 'a delivered email'
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
context "default is set to true" do
|
|
55
|
+
before :each do
|
|
56
|
+
settings.preferences['email'] = {'default' => true}
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
it_should_behave_like 'a delivered email'
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
context "default is set to false" do
|
|
63
|
+
before :each do
|
|
64
|
+
settings.preferences['email'] = {'default' => false}
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
it_should_behave_like 'a blocked email'
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
context "default and post are set to true" do
|
|
71
|
+
before :each do
|
|
72
|
+
settings.preferences['email'] = {
|
|
73
|
+
'default' => true, 'create_posts_notify_subscribers' => true
|
|
74
|
+
}
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
it_should_behave_like 'a delivered email'
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
context "default is set to false but post is set to true" do
|
|
81
|
+
before :each do
|
|
82
|
+
settings.preferences['email'] = {
|
|
83
|
+
'default' => false, 'create_posts_notify_subscribers' => true
|
|
84
|
+
}
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
it_should_behave_like 'a delivered email'
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
context "default is set to true but post is set to false" do
|
|
91
|
+
before :each do
|
|
92
|
+
settings.preferences['email'] = {
|
|
93
|
+
'default' => true, 'create_posts_notify_subscribers' => false
|
|
94
|
+
}
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
it_should_behave_like 'a blocked email'
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
context "default and post are both set to false" do
|
|
101
|
+
before :each do
|
|
102
|
+
settings.preferences['email'] = {
|
|
103
|
+
'default' => false, 'create_posts_notify_subscribers' => false
|
|
104
|
+
}
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
it_should_behave_like 'a blocked email'
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
end
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe Eventifier::Preferences do
|
|
4
|
+
let(:preferences) { Eventifier::Preferences.new double('User') }
|
|
5
|
+
let(:settings) { double 'Settings', :preferences => {} }
|
|
6
|
+
|
|
7
|
+
before :each do
|
|
8
|
+
Eventifier::NotificationMapping.stub :notification_mappings => {
|
|
9
|
+
'create.posts' => [:readers],
|
|
10
|
+
'create.comments' => [{:post => :readers}]
|
|
11
|
+
}
|
|
12
|
+
Eventifier::NotificationSetting.stub :for_user => settings
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
describe '#to_hashes' do
|
|
16
|
+
it "interprets each key" do
|
|
17
|
+
preferences.to_hashes.collect { |hash| hash[:key] }.should == [
|
|
18
|
+
'default',
|
|
19
|
+
'create_posts_notify_readers',
|
|
20
|
+
'create_comments_notify_post_readers'
|
|
21
|
+
]
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
it "sets values to true if unknown" do
|
|
25
|
+
preferences.to_hashes.collect { |hash| hash[:value] }.
|
|
26
|
+
should == [true, true, true]
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
it "sets values to true if known and true" do
|
|
30
|
+
settings.preferences['email'] = {}
|
|
31
|
+
settings.preferences['email']['create_posts_notify_readers'] = true
|
|
32
|
+
|
|
33
|
+
preferences.to_hashes.collect { |hash| hash[:value] }.
|
|
34
|
+
should == [true, true, true]
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
it "sets values to false if known and false" do
|
|
38
|
+
settings.preferences['email'] = {}
|
|
39
|
+
settings.preferences['email']['create_posts_notify_readers'] = false
|
|
40
|
+
|
|
41
|
+
preferences.to_hashes.collect { |hash| hash[:value] }.
|
|
42
|
+
should == [true, false, true]
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
it "returns keys if no translations available for labels" do
|
|
46
|
+
preferences.to_hashes.collect { |hash| hash[:label] }.should == [
|
|
47
|
+
'default',
|
|
48
|
+
'create_posts_notify_readers',
|
|
49
|
+
'create_comments_notify_post_readers'
|
|
50
|
+
]
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
it "matches translations for labels" do
|
|
54
|
+
I18n.backend.reload!
|
|
55
|
+
I18n.backend.store_translations :en, :events => {
|
|
56
|
+
:labels => {
|
|
57
|
+
:preferences => {
|
|
58
|
+
'default' => 'All Events',
|
|
59
|
+
'create_posts_notify_readers' => 'New Posts',
|
|
60
|
+
'create_comments_notify_post_readers' => 'New Comments'
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
preferences.to_hashes.collect { |hash| hash[:label] }.
|
|
66
|
+
should == ['All Events', 'New Posts', 'New Comments']
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe Eventifier::Relationship do
|
|
4
|
+
describe '#key' do
|
|
5
|
+
it "translates symbol relations to strings" do
|
|
6
|
+
Eventifier::Relationship.new(double, :mouse).key.should == 'mouse'
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
it "translates hash relations to period-separated strings" do
|
|
10
|
+
Eventifier::Relationship.new(double, :cat => :mouse).key.
|
|
11
|
+
should == 'cat_mouse'
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
it "translates array relations to hyphen-separated strings" do
|
|
15
|
+
Eventifier::Relationship.new(double, [:cat, :mouse]).key.
|
|
16
|
+
should == 'cat-mouse'
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
describe '#users' do
|
|
21
|
+
it "should call the string as a method when passed a string" do
|
|
22
|
+
object = double('object', :mouse => 5)
|
|
23
|
+
|
|
24
|
+
Eventifier::Relationship.new(object, :mouse).users.should == [5]
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
it "should string the methods when passed a hash" do
|
|
28
|
+
object = double('object', :cat => (cat = double('cat', :mouse => 5)))
|
|
29
|
+
|
|
30
|
+
Eventifier::Relationship.new(object, :cat => :mouse).users.should == [5]
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
data/spec/eventifier_spec.rb
CHANGED
|
@@ -37,8 +37,8 @@ describe Eventifier::EventTracking do
|
|
|
37
37
|
before do
|
|
38
38
|
object.stub(:user).and_return(user)
|
|
39
39
|
event_tracker.events_for test_class,
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
:track_on => [:create, :update, :destroy],
|
|
41
|
+
:attributes => { :except => %w(updated_at) }
|
|
42
42
|
end
|
|
43
43
|
|
|
44
44
|
|
|
@@ -46,7 +46,7 @@ describe Eventifier::EventTracking do
|
|
|
46
46
|
changes = { :foo => 'bar', :bar => 'baz' }
|
|
47
47
|
object.stub(:changes).and_return(changes)
|
|
48
48
|
|
|
49
|
-
Eventifier::Event.should_receive(:create).with(:user => user, :eventable => object, :verb => :create, :change_data => changes)
|
|
49
|
+
Eventifier::Event.should_receive(:create).with(:user => user, :eventable => object, :verb => :create, :change_data => changes, :groupable => object)
|
|
50
50
|
|
|
51
51
|
subject
|
|
52
52
|
end
|
|
@@ -54,39 +54,6 @@ describe Eventifier::EventTracking do
|
|
|
54
54
|
|
|
55
55
|
end
|
|
56
56
|
|
|
57
|
-
describe '#create_event' do
|
|
58
|
-
let(:object) { double('object', :changes => changes, :user => double(User)) }
|
|
59
|
-
let(:changes) { { :foo => 'bar', :bar => 'baz', :bob => 'blah' } }
|
|
60
|
-
let(:options) { { } }
|
|
61
|
-
|
|
62
|
-
subject { Eventifier::Event.create_event(:create, object, options) }
|
|
63
|
-
|
|
64
|
-
describe "exclude" do
|
|
65
|
-
let(:options) { { :except => ['foo'] } }
|
|
66
|
-
|
|
67
|
-
it 'should exclude the right attrs' do
|
|
68
|
-
Eventifier::Event.should_receive(:create) do |attrs|
|
|
69
|
-
attrs[:change_data].should == { :bar => 'baz', :bob => 'blah' }
|
|
70
|
-
end
|
|
71
|
-
subject
|
|
72
|
-
end
|
|
73
|
-
|
|
74
|
-
end
|
|
75
|
-
describe "only" do
|
|
76
|
-
let(:options) { { :only => ['foo'] } }
|
|
77
|
-
|
|
78
|
-
it 'should exclude the right attrs' do
|
|
79
|
-
Eventifier::Event.should_receive(:create) do |attrs|
|
|
80
|
-
attrs[:change_data].should == { :foo => 'bar' }
|
|
81
|
-
end
|
|
82
|
-
subject
|
|
83
|
-
end
|
|
84
|
-
|
|
85
|
-
end
|
|
86
|
-
|
|
87
|
-
end
|
|
88
|
-
|
|
89
|
-
|
|
90
57
|
context "block syntax" do
|
|
91
58
|
|
|
92
59
|
context "tracking" do
|
|
@@ -106,7 +73,7 @@ describe Eventifier::EventTracking do
|
|
|
106
73
|
track_on :update, :attributes => { :except => %w(updated_at) }
|
|
107
74
|
end
|
|
108
75
|
|
|
109
|
-
Eventifier::Event.should_receive(:create).with(:user => user, :eventable => object, :verb => :create, :change_data => changes)
|
|
76
|
+
Eventifier::Event.should_receive(:create).with(:user => user, :eventable => object, :verb => :create, :change_data => changes, :groupable => object)
|
|
110
77
|
|
|
111
78
|
subject
|
|
112
79
|
end
|
|
@@ -121,24 +88,15 @@ describe Eventifier::EventTracking do
|
|
|
121
88
|
end
|
|
122
89
|
|
|
123
90
|
context "notifying" do
|
|
124
|
-
before do
|
|
125
|
-
@event_observer_instance = Eventifier::EventObserver.instance
|
|
126
|
-
Eventifier::EventObserver.any_instance.stub post_path: '/post'
|
|
127
|
-
end
|
|
128
|
-
|
|
129
91
|
describe "#add_notification" do
|
|
130
92
|
|
|
131
93
|
it "should add the notification to the notification hash" do
|
|
132
|
-
@event_observer_instance.should_receive(:add_notification).with(test_class, :user, :create)
|
|
133
|
-
|
|
134
|
-
event_tracker.events_for test_class do
|
|
135
|
-
track_on :create, :attributes => { :except => %w(updated_at) }
|
|
136
|
-
notify :user, :on => :create
|
|
137
|
-
end
|
|
138
94
|
end
|
|
139
95
|
end
|
|
140
96
|
|
|
141
97
|
it "should notify users when passed a hash" do
|
|
98
|
+
pending
|
|
99
|
+
|
|
142
100
|
subscriber = double('user')
|
|
143
101
|
|
|
144
102
|
object.stub :category => double('category', :subscribers => [subscriber])
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
require 'eventifier'
|
|
2
2
|
|
|
3
|
-
|
|
4
3
|
Fabricator(:event, :class_name => Eventifier::Event) do
|
|
5
|
-
user
|
|
6
|
-
eventable
|
|
4
|
+
user
|
|
5
|
+
eventable(:fabricator => :post)
|
|
6
|
+
groupable(:fabricator => :post)
|
|
7
7
|
verb :update
|
|
8
8
|
change_data { { :date => [5.days.ago, 3.days.ago] } }
|
|
9
9
|
end
|
|
@@ -15,17 +15,17 @@ Fabricator(:ghost, :class_name => Eventifier::Ghost) do
|
|
|
15
15
|
end
|
|
16
16
|
|
|
17
17
|
Fabricator(:notification, :class_name => Eventifier::Notification) do
|
|
18
|
-
event
|
|
19
|
-
user
|
|
18
|
+
event { Fabricate(:event) }
|
|
19
|
+
user
|
|
20
20
|
end
|
|
21
21
|
|
|
22
22
|
Fabricator(:post, :class_name => Post) do
|
|
23
|
-
title
|
|
24
|
-
body
|
|
25
|
-
author
|
|
23
|
+
title { "My amazing blog post" }
|
|
24
|
+
body { "A deep and profound analysis of life" }
|
|
25
|
+
author { Fabricate(:user) }
|
|
26
26
|
end
|
|
27
27
|
|
|
28
28
|
Fabricator(:user, :class_name => User) do
|
|
29
|
-
name
|
|
30
|
-
email{ "billy#{sequence(:email, 1)}@email.com" }
|
|
31
|
-
end
|
|
29
|
+
name { "Billy #{sequence(:name, 1)}" }
|
|
30
|
+
email { "billy#{sequence(:email, 1)}@email.com" }
|
|
31
|
+
end
|