dallal 0.0.1
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/MIT-LICENSE +20 -0
- data/README.rdoc +3 -0
- data/Rakefile +33 -0
- data/app/assets/javascripts/dallal/application.js +13 -0
- data/app/assets/stylesheets/dallal/application.css +15 -0
- data/app/controllers/dallal/application_controller.rb +5 -0
- data/app/helpers/dallal/application_helper.rb +4 -0
- data/app/jobs/dallal/dallal_job.rb +13 -0
- data/app/mailers/dallal/mailer.rb +36 -0
- data/app/views/layouts/dallal/application.html.erb +14 -0
- data/app/views/layouts/mailer.html.erb +5 -0
- data/app/views/layouts/mailer.text.erb +1 -0
- data/config/routes.rb +2 -0
- data/lib/dallal/abstract_interface.rb +30 -0
- data/lib/dallal/configuration.rb +54 -0
- data/lib/dallal/engine.rb +22 -0
- data/lib/dallal/events/event_publisher.rb +8 -0
- data/lib/dallal/events/event_subscriber.rb +33 -0
- data/lib/dallal/events/events.rb +5 -0
- data/lib/dallal/events/observer.rb +57 -0
- data/lib/dallal/events/publisher.rb +44 -0
- data/lib/dallal/events/subscriber.rb +16 -0
- data/lib/dallal/file_loader.rb +15 -0
- data/lib/dallal/notification.rb +79 -0
- data/lib/dallal/notifiers/email_notifier.rb +28 -0
- data/lib/dallal/notifiers/notifier.rb +33 -0
- data/lib/dallal/notifiers/sms_notifier.rb +15 -0
- data/lib/dallal/result.rb +21 -0
- data/lib/dallal/version.rb +3 -0
- data/lib/dallal.rb +41 -0
- data/lib/generators/dallal/install/USAGE +2 -0
- data/lib/generators/dallal/install/install_generator.rb +11 -0
- data/lib/generators/dallal/install/templates/dallal.rb +16 -0
- data/lib/generators/dallal/notification/USAGE +2 -0
- data/lib/generators/dallal/notification/notification_generator.rb +44 -0
- data/lib/generators/dallal/notification/templates/migration.rb +10 -0
- data/lib/generators/dallal/notification/templates/model.rb +2 -0
- data/lib/generators/dallal/notifiers/USAGE +2 -0
- data/lib/generators/dallal/notifiers/notifiers_generator.rb +56 -0
- data/lib/generators/dallal/notifiers/templates/email_notifier.rb +2 -0
- data/lib/generators/dallal/notifiers/templates/email_notifier_migration.rb +7 -0
- data/lib/generators/dallal/notifiers/templates/sms_notifier.rb +2 -0
- data/lib/generators/dallal/notifiers/templates/sms_notifier_migration.rb +7 -0
- data/lib/tasks/user_notification_tasks.rake +4 -0
- data/spec/dallal/configuration_spec.rb +56 -0
- data/spec/dallal/dallal_spec.rb +23 -0
- data/spec/dallal/events/event_publisher_spec.rb +7 -0
- data/spec/dallal/events/event_subscriber_spec.rb +92 -0
- data/spec/dallal/events/observer_spec.rb +117 -0
- data/spec/dallal/events/publisher_spec.rb +14 -0
- data/spec/dallal/events/subscriber_spec.rb +4 -0
- data/spec/dallal/notification_spec.rb +152 -0
- data/spec/dallal/notifiers/notifier_spec.rb +27 -0
- data/spec/dummy/README.rdoc +28 -0
- data/spec/dummy/Rakefile +6 -0
- data/spec/dummy/app/assets/javascripts/application.js +13 -0
- data/spec/dummy/app/assets/stylesheets/application.css +15 -0
- data/spec/dummy/app/controllers/application_controller.rb +5 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/models/post.rb +4 -0
- data/spec/dummy/app/models/user.rb +4 -0
- data/spec/dummy/app/notifiers/comment_notifier.rb +0 -0
- data/spec/dummy/app/notifiers/post_notifier.rb +9 -0
- data/spec/dummy/app/notifiers/user_notifier.rb +2 -0
- data/spec/dummy/app/views/dallal/mailer/posts/register.html.erb +5 -0
- data/spec/dummy/app/views/dallal/mailer/posts/register_subject.html.erb +1 -0
- data/spec/dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/bin/bundle +3 -0
- data/spec/dummy/bin/rails +4 -0
- data/spec/dummy/bin/rake +4 -0
- data/spec/dummy/bin/setup +29 -0
- data/spec/dummy/config/application.rb +32 -0
- data/spec/dummy/config/boot.rb +5 -0
- data/spec/dummy/config/database.yml +25 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +41 -0
- data/spec/dummy/config/environments/production.rb +79 -0
- data/spec/dummy/config/environments/test.rb +42 -0
- data/spec/dummy/config/initializers/assets.rb +11 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/dummy/config/initializers/inflections.rb +16 -0
- data/spec/dummy/config/initializers/mime_types.rb +4 -0
- data/spec/dummy/config/initializers/session_store.rb +3 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +23 -0
- data/spec/dummy/config/routes.rb +4 -0
- data/spec/dummy/config/secrets.yml +22 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/db/development.sqlite3 +0 -0
- data/spec/dummy/db/migrate/20160801191053_create_users.rb +10 -0
- data/spec/dummy/db/migrate/20160803174036_create_posts.rb +11 -0
- data/spec/dummy/db/schema.rb +38 -0
- data/spec/dummy/db/test.sqlite3 +0 -0
- data/spec/dummy/log/test.log +2057 -0
- data/spec/dummy/public/404.html +67 -0
- data/spec/dummy/public/422.html +67 -0
- data/spec/dummy/public/500.html +66 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/spec/factories/posts.rb +7 -0
- data/spec/dummy/spec/factories/users.rb +6 -0
- data/spec/dummy/spec/models/post_spec.rb +5 -0
- data/spec/dummy/spec/models/user_spec.rb +5 -0
- data/spec/factories/posts.rb +7 -0
- data/spec/factories/users.rb +6 -0
- data/spec/jobs/dallal/dallal_job_spec.rb +14 -0
- data/spec/mailers/dallal/mailer_spec.rb +7 -0
- data/spec/mailers/previews/dallal/mailer_preview.rb +6 -0
- data/spec/rails_helper.rb +26 -0
- data/spec/spec_helper.rb +92 -0
- metadata +295 -0
data/lib/dallal.rb
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
require "dallal/engine" if defined?(Rails)
|
|
2
|
+
require 'dallal/notification'
|
|
3
|
+
require 'dallal/notifiers/notifier'
|
|
4
|
+
require 'dallal/events/events'
|
|
5
|
+
require 'dallal/configuration'
|
|
6
|
+
require 'dallal/events/event_publisher'
|
|
7
|
+
|
|
8
|
+
module Dallal
|
|
9
|
+
extend Configuration
|
|
10
|
+
# Add here include Notifiable and move all methods below to
|
|
11
|
+
# Notifiable Module
|
|
12
|
+
|
|
13
|
+
def self.included(base)
|
|
14
|
+
base.include(InstanceMethods)
|
|
15
|
+
base.extend(ClassMethods)
|
|
16
|
+
|
|
17
|
+
base.class_eval do
|
|
18
|
+
include Dallal::Events::Publisher
|
|
19
|
+
|
|
20
|
+
after_create :publish_notification_created
|
|
21
|
+
after_update :publish_notification_updated
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
module ClassMethods
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
module InstanceMethods
|
|
29
|
+
def publish_notification_created
|
|
30
|
+
Dallal::Events::EventPublisher.broadcast({
|
|
31
|
+
class: self.class.name, id: self.id, event: :create
|
|
32
|
+
})
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def publish_notification_updated
|
|
36
|
+
Dallal::Events::EventPublisher.broadcast({
|
|
37
|
+
class: self.class.name, id: self.id, event: :update
|
|
38
|
+
})
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
module Dallal
|
|
2
|
+
module Generators
|
|
3
|
+
class InstallGenerator < ::Rails::Generators::Base
|
|
4
|
+
source_root File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
|
|
5
|
+
|
|
6
|
+
def copy_initializer
|
|
7
|
+
template 'dallal.rb', 'config/initializers/dallal.rb'
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
Dallal.configure do |config|
|
|
2
|
+
# Set the user class name of your app
|
|
3
|
+
config.user_class_name = 'User'
|
|
4
|
+
|
|
5
|
+
# Set up user notification class name
|
|
6
|
+
config.dallal_class_name = 'Dallal'
|
|
7
|
+
|
|
8
|
+
# Enable / Disable notifications globaly
|
|
9
|
+
config.enabled = true
|
|
10
|
+
|
|
11
|
+
#
|
|
12
|
+
# config.available_notifications = [:email]
|
|
13
|
+
|
|
14
|
+
#
|
|
15
|
+
# config.auto_deliver = true
|
|
16
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
require 'rails/generators/active_record'
|
|
2
|
+
|
|
3
|
+
module Dallal
|
|
4
|
+
module Generators
|
|
5
|
+
class NotificationGenerator < ActiveRecord::Generators::Base
|
|
6
|
+
source_root File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
|
|
7
|
+
|
|
8
|
+
def notifications_table_name
|
|
9
|
+
"#{table_name.singularize}_notifications"
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def copy_migration
|
|
13
|
+
migration_template "migration.rb", "db/migrate/create_#{notifications_table_name}.rb"
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def copy_files
|
|
17
|
+
copy_file "model.rb", model_path
|
|
18
|
+
|
|
19
|
+
if self.behavior == :invoke
|
|
20
|
+
inject_into_class(model_path, model_class_name, model_content)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
private
|
|
25
|
+
def model_class_name
|
|
26
|
+
"#{table_name.singularize}_notification".classify
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def user_model_name
|
|
30
|
+
Dallal.configuration.user_class_name.downcase
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def model_path
|
|
34
|
+
"app/models/#{table_name.singularize}_notification.rb"
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def model_content
|
|
38
|
+
<<-TEXT
|
|
39
|
+
belongs_to #{user_model_name}
|
|
40
|
+
TEXT
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
class Create<%= notifications_table_name.camelize %> < ActiveRecord::Migration
|
|
2
|
+
def change
|
|
3
|
+
create_table :<%= notifications_table_name %> do |t|
|
|
4
|
+
t.references :<%= user_model_name %>, index: true, foreign_key: true, null: false
|
|
5
|
+
t.timestamp :sent_at
|
|
6
|
+
t.timestamp :deliver_at
|
|
7
|
+
t.timestamps
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
end
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
require 'rails/generators/active_record'
|
|
2
|
+
|
|
3
|
+
module Dallal
|
|
4
|
+
module Generators
|
|
5
|
+
class NotifiersGenerator < ActiveRecord::Generators::Base
|
|
6
|
+
source_root File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
|
|
7
|
+
|
|
8
|
+
def copy_migration
|
|
9
|
+
migration_template "#{notifier_name}_notifier_migration.rb", "db/migrate/create_#{notifier_name}_notifiers.rb"
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
# TODO Refactor and DRY this or better DRY 3 step generation process into 1 or 2.
|
|
13
|
+
def copy_files
|
|
14
|
+
copy_file "#{notifier_name}_notifier.rb", model_path
|
|
15
|
+
|
|
16
|
+
if self.behavior == :invoke
|
|
17
|
+
inject_into_class(model_path, model_class_name, model_content)
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def dallal_class_name
|
|
22
|
+
Dallal.configuration.dallal_class_name.underscore
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
private
|
|
26
|
+
def notifications_table_name
|
|
27
|
+
"#{notifier_name}_notifiers"
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# A quick and dirty patch for sms -> sm singularization
|
|
31
|
+
def notifier_name
|
|
32
|
+
case table_name
|
|
33
|
+
when 'sms'
|
|
34
|
+
# TODO Revisit this one. sms name [singularizes as sm]
|
|
35
|
+
'sms'
|
|
36
|
+
else
|
|
37
|
+
table_name.singularize
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def model_class_name
|
|
42
|
+
"#{notifier_name}_notifier".classify
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def model_content
|
|
46
|
+
<<-RUBY
|
|
47
|
+
belongs_to #{dallal_class_name}
|
|
48
|
+
RUBY
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def model_path
|
|
52
|
+
"app/models/#{notifier_name}_notifier.rb"
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
require 'rails_helper'
|
|
2
|
+
|
|
3
|
+
describe Dallal::Configuration do
|
|
4
|
+
subject do
|
|
5
|
+
class DummClass
|
|
6
|
+
extend Dallal::Configuration
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
DummClass
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
describe '.configure' do
|
|
13
|
+
it 'returns the configuration when a block is given' do
|
|
14
|
+
subject.configure do |config|
|
|
15
|
+
expect(config).to be_a Dallal::Configuration::Configuration
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
it { expect(subject.configure).to be nil }
|
|
20
|
+
|
|
21
|
+
context "Configuration settings" do
|
|
22
|
+
before do
|
|
23
|
+
subject.configure do |config|
|
|
24
|
+
config.user_class_name = 'UserClassName'
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
it 'should have set confg attributes correctly' do
|
|
29
|
+
expect(subject.configuration.user_class_name).to eq 'UserClassName'
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
describe '.options' do
|
|
35
|
+
before do
|
|
36
|
+
subject.configure do |config|
|
|
37
|
+
config.user_class_name = 'UserClass'
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
it 'should return all options' do
|
|
42
|
+
expect(subject.options).to eq({
|
|
43
|
+
user_class_name: 'UserClass',
|
|
44
|
+
dallal_class_name: 'Dallal',
|
|
45
|
+
enabled: true,
|
|
46
|
+
email_layout: 'mailer',
|
|
47
|
+
from_email: 'foo@bar.xyz',
|
|
48
|
+
from_name: 'just a name'
|
|
49
|
+
})
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
describe 'Default Config Values' do
|
|
54
|
+
pending
|
|
55
|
+
end
|
|
56
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
require 'rails_helper'
|
|
2
|
+
|
|
3
|
+
describe Dallal do
|
|
4
|
+
context "callbacks" do
|
|
5
|
+
subject { FactoryGirl.build(:user) }
|
|
6
|
+
it 'should broadcast a created event' do
|
|
7
|
+
allow(subject).to receive(:id).and_return(1)
|
|
8
|
+
|
|
9
|
+
expect(Dallal::Events::EventPublisher).to receive(:broadcast).
|
|
10
|
+
with(class: subject.class.name, id: subject.id, event: :create).and_call_original
|
|
11
|
+
|
|
12
|
+
subject.save!
|
|
13
|
+
end
|
|
14
|
+
it 'should broadcase an updated event' do
|
|
15
|
+
subject.save
|
|
16
|
+
|
|
17
|
+
expect(Dallal::Events::EventPublisher).to receive(:broadcast).
|
|
18
|
+
with(class: subject.class.name, id: subject.id, event: :update).and_call_original
|
|
19
|
+
|
|
20
|
+
subject.save!
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
require 'rails_helper'
|
|
2
|
+
|
|
3
|
+
describe Dallal::Events::EventSubscriber do
|
|
4
|
+
let(:publisher) { Dallal::Events::EventPublisher }
|
|
5
|
+
let(:payload) { { a: 1, b: 2 } }
|
|
6
|
+
let(:user) { FactoryGirl.build(:user) }
|
|
7
|
+
|
|
8
|
+
before { allow(user).to receive(:id).and_return(12) }
|
|
9
|
+
|
|
10
|
+
context 'when a create event is broadcasted' do
|
|
11
|
+
it 'creates a new instance and exeutes' do
|
|
12
|
+
base_event = double('Dallal::Events::EventSubscriber')
|
|
13
|
+
expect(Dallal::Events::EventSubscriber).to receive(:new).with(class: user.class.name, id: user.id, event: :create).and_return(base_event)
|
|
14
|
+
expect(base_event).to receive(:execute)
|
|
15
|
+
user.save!
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
context 'when a create event is broadcasted' do
|
|
20
|
+
it 'creates a new instance and executes' do
|
|
21
|
+
# save the user
|
|
22
|
+
user.save!
|
|
23
|
+
|
|
24
|
+
base_event = double('Dallal::Events::EventSubscriber')
|
|
25
|
+
expect(Dallal::Events::EventSubscriber).to receive(:new).with(class: user.class.name, id: user.id, event: :update).and_return(base_event)
|
|
26
|
+
expect(base_event).to receive(:execute)
|
|
27
|
+
|
|
28
|
+
# update user
|
|
29
|
+
user.save!
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# Instance spec
|
|
34
|
+
let(:payload) do
|
|
35
|
+
{
|
|
36
|
+
class: 'User',
|
|
37
|
+
id: 1,
|
|
38
|
+
event: :create
|
|
39
|
+
}
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
subject { Dallal::Events::EventSubscriber.new(payload) }
|
|
43
|
+
|
|
44
|
+
describe '#execute' do
|
|
45
|
+
context 'when notifications are supported' do
|
|
46
|
+
before { expect(subject).to receive(:should_create_notifications?).and_return(true) }
|
|
47
|
+
|
|
48
|
+
it 'triggers a bg job using ActiveJob' do
|
|
49
|
+
expect(Dallal::DallalJob).to receive(:perform_later).with(payload[:class], payload[:id], payload[:event].to_s).and_call_original
|
|
50
|
+
subject.execute
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
context 'when notifications are not supported' do
|
|
54
|
+
before { expect(subject).to receive(:should_create_notifications?).and_return(false) }
|
|
55
|
+
it 'does not trigger any job' do
|
|
56
|
+
expect(Dallal::DallalJob).to_not receive(:perform_later).with(payload[:class], payload[:id], payload[:event].to_s).and_call_original
|
|
57
|
+
subject.execute
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
describe '#should_create_notifications?' do
|
|
63
|
+
context 'when notifications are enabled' do
|
|
64
|
+
before { Dallal.configuration.enabled = true }
|
|
65
|
+
context 'when class is included in notifiables' do
|
|
66
|
+
before { expect(Dallal::Events::Observer::NOTIFIABLES.include?(payload[:class])).to be_truthy }
|
|
67
|
+
|
|
68
|
+
it { expect(subject.send(:should_create_notifications?)).to be_truthy }
|
|
69
|
+
end
|
|
70
|
+
context 'when subject is not a notifiable' do
|
|
71
|
+
before { payload[:class] = 'SomeNonNotifableClass' }
|
|
72
|
+
before { expect(Dallal::Events::Observer::NOTIFIABLES.include?(payload[:class])).to be_falsey }
|
|
73
|
+
|
|
74
|
+
it { expect(subject.send(:should_create_notifications?)).to be_falsey }
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
context 'when notifications are disabled' do
|
|
78
|
+
before { Dallal.configuration.enabled = false }
|
|
79
|
+
context 'when class is included in notifiables' do
|
|
80
|
+
before { expect(Dallal::Events::Observer::NOTIFIABLES.include?(payload[:class])).to be_truthy }
|
|
81
|
+
|
|
82
|
+
it { expect(subject.send(:should_create_notifications?)).to be_falsey }
|
|
83
|
+
end
|
|
84
|
+
context 'when subject ia not a notifiable' do
|
|
85
|
+
before { payload[:class] = 'SomeNonNotifableClass' }
|
|
86
|
+
before { expect(Dallal::Events::Observer::NOTIFIABLES.include?(payload[:class])).to be_falsey }
|
|
87
|
+
it { expect(subject.send(:should_create_notifications?)).to be_falsey }
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
end
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
require 'rails_helper'
|
|
2
|
+
|
|
3
|
+
describe Dallal::Events::Observer do
|
|
4
|
+
subject { Dallal::Events::Observer }
|
|
5
|
+
it { expect(Dallal::Events::Observer.callbacks).to eq [] }
|
|
6
|
+
describe '.inherited' do
|
|
7
|
+
context "when a class inherites from observer" do
|
|
8
|
+
let(:klass) do
|
|
9
|
+
class DummyNotifier < Dallal::Events::Observer
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
it 'should validate that model exists and include the model name in NOTIFABLES' do
|
|
14
|
+
expect(Dallal::Events::Observer).to receive(:validate_model_exists!).with('Dummy').and_call_original
|
|
15
|
+
klass
|
|
16
|
+
expect(Dallal::Events::Observer::NOTIFIABLES).to include('Dummy')
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
describe '.on' do
|
|
22
|
+
it 'should append args, options and block to callbacks' do
|
|
23
|
+
expect(subject.callbacks.size).to eq 0
|
|
24
|
+
executed = false
|
|
25
|
+
|
|
26
|
+
subject.on :create, a: 1, b: 2 do
|
|
27
|
+
executed = true
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
expect(executed).to be false
|
|
31
|
+
expect(subject.callbacks.size).to eq 1
|
|
32
|
+
callback = subject.callbacks[0]
|
|
33
|
+
expect(callback[:on]).to eq([:create])
|
|
34
|
+
expect(callback[:opts]).to eq({a: 1, b: 2})
|
|
35
|
+
expect(callback[:block]).to be_a(Proc)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
describe '.create_notification' do
|
|
40
|
+
subject { UserNotifier }
|
|
41
|
+
before do
|
|
42
|
+
@user = FactoryGirl.create(:user)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
context 'when no callbacks are available' do
|
|
46
|
+
before do
|
|
47
|
+
subject.instance_variable_set(:@__notification_callbacks, [])
|
|
48
|
+
expect(subject.callbacks.size).to eq 0
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
it 'does not sipatch any notification' do
|
|
52
|
+
expect_any_instance_of(Dallal::Notification).to_not receive(:dispatch!)
|
|
53
|
+
subject.create_notification(id: 1, event: :create)
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# TODO Refactor these specs
|
|
58
|
+
context 'when a callbacks is defined' do
|
|
59
|
+
context "create event" do
|
|
60
|
+
before { subject.instance_variable_set(:@__notification_callbacks, []) }
|
|
61
|
+
it 'dipatches a notification' do
|
|
62
|
+
@create_block_executed = false
|
|
63
|
+
|
|
64
|
+
subject.on :create, a: 1, b: 2 do
|
|
65
|
+
@create_block_executed = true
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
notification = double('Dallal::Notification')
|
|
69
|
+
expect(Dallal::Notification).to receive(:new).with(event: :create, model_class: 'User', opts: { a: 1, b: 2 }, _object: @user).and_return(notification)
|
|
70
|
+
expect(notification).to receive(:dispatch!)
|
|
71
|
+
subject.create_notification(id: @user.id, event: :create)
|
|
72
|
+
|
|
73
|
+
expect(notification.user).to eq @user
|
|
74
|
+
# TODO Fix this
|
|
75
|
+
#expect(@create_block_executed).to be true
|
|
76
|
+
#expect(@update_block_executed).to be true
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
context "update event" do
|
|
80
|
+
before { subject.instance_variable_set(:@__notification_callbacks, []) }
|
|
81
|
+
it 'dispatches an update event' do
|
|
82
|
+
@update_block_executed = false
|
|
83
|
+
subject.on :update, a: 1, b: 2 do
|
|
84
|
+
@update_block_executed = true
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
notification = double('Dallal::Notification')
|
|
88
|
+
expect(Dallal::Notification).to receive(:new).with(event: :update, model_class: 'User', opts: { a: 1, b: 2 }, _object: @user).and_return(notification)
|
|
89
|
+
expect(notification).to receive(:dispatch!)
|
|
90
|
+
subject.create_notification(id: @user.id, event: :update)
|
|
91
|
+
|
|
92
|
+
expect(notification.user).to eq @user
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
context 'when called with an event that is not defined' do
|
|
98
|
+
before { subject.instance_variable_set(:@__notification_callbacks, []) }
|
|
99
|
+
it 'does not emit a notification' do
|
|
100
|
+
subject.on :create do
|
|
101
|
+
|
|
102
|
+
end
|
|
103
|
+
expect_any_instance_of(Dallal::Notification).to_not receive(:dispatch!)
|
|
104
|
+
subject.create_notification(id: @user.id, event: :non_existing)
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
describe '.models_class' do
|
|
110
|
+
subject { UserNotifier }
|
|
111
|
+
it { expect(subject.send(:model_class)).to eq 'User' }
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
describe '.validate_model_exists!' do
|
|
115
|
+
pending
|
|
116
|
+
end
|
|
117
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
require 'rails_helper'
|
|
2
|
+
|
|
3
|
+
describe Dallal::Events::Publisher do
|
|
4
|
+
subject { Dallal::Events::EventPublisher }
|
|
5
|
+
|
|
6
|
+
describe '.broadcast' do
|
|
7
|
+
it 'brodcasts an event to with ActiveSupport::Notification.instrument' do
|
|
8
|
+
payload = { a: 1, b: 2 }
|
|
9
|
+
expect(ActiveSupport::Notifications).to receive(:instrument).with(
|
|
10
|
+
subject.pub_sub_namespace, payload).and_call_original
|
|
11
|
+
subject.broadcast(payload)
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|