Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to temporarily disable broadcasting #5

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -16,3 +16,4 @@ test/tmp
test/version_tmp
tmp
spec/db.sqlite3
.DS_STORE
44 changes: 31 additions & 13 deletions lib/wisper/mongoid/publisher.rb
Expand Up @@ -5,32 +5,50 @@ module Publisher
included do
include Wisper::Publisher

after_validation :after_validation_broadcast
after_create :after_create_broadcast, on: :create
after_update :after_update_broadcast, on: :update
after_destroy :after_destroy_broadcast, on: :destroy
after_create :after_create_broadcast
after_save :after_save_broadcast
after_update :after_update_broadcast
after_destroy :after_destroy_broadcast
end

def without_broadcasting(&block)
@broadcast = false
block.call(self)
@broadcast = true
end

private

def after_validation_broadcast
action = new_record? ? 'create' : 'update'
broadcast("#{action}_#{broadcast_model_name_key}_failed", self) unless errors.empty?
def broadcast_event(event_name)
if (!defined?(@broadcast) || @broadcast)
broadcast(event_name, payload)
end
end

def payload
{
id: id.to_s
}
end

def after_create_broadcast
broadcast(:after_create, self)
broadcast("create_#{broadcast_model_name_key}_successful", self)
broadcast_event(:after_create)
broadcast_event("#{broadcast_model_name_key}_created")
end

def after_save_broadcast
broadcast_event(:after_save)
broadcast_event("#{broadcast_model_name_key}_saved")
end

def after_update_broadcast
broadcast(:after_update, self)
broadcast("update_#{broadcast_model_name_key}_successful", self)
broadcast_event(:after_update)
broadcast_event("#{broadcast_model_name_key}_updated")
end

def after_destroy_broadcast
broadcast(:after_destroy, self)
broadcast("destroy_#{broadcast_model_name_key}_successful", self)
broadcast_event(:after_destroy)
broadcast_event("#{broadcast_model_name_key}_destroyed")
end

def broadcast_model_name_key
Expand Down
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -39,6 +39,10 @@

Kernel.srand config.seed

config.before(:each) do |example|
Mongoid.purge! # Clean up collections before each spec
end

config.expect_with :rspec do |expectations|
expectations.syntax = :expect
end
Expand Down
2 changes: 1 addition & 1 deletion spec/support/mongoid.yml
@@ -1,5 +1,5 @@
test:
sessions:
clients:
default:
database: wisper_test
hosts:
Expand Down
81 changes: 34 additions & 47 deletions spec/wisper/mongoid_spec.rb
@@ -1,86 +1,73 @@
describe 'Mongoid' do
let(:listener) { double('Listener') }
let(:model_class) { Meeting }
let(:id) { "1234" }
let(:payload) do
{ id: id }
end

before { Wisper::GlobalListeners.clear }

it '.model returns Mongoid module' do
expect(Wisper.model).to eq(Wisper::Mongoid::Publisher)
describe '.model' do
it 'returns Mongoid module' do
expect(Wisper.model).to eq(Wisper::Mongoid::Publisher)
end
end

describe 'when creating' do
context 'and model is valid' do
it 'publishes create_<model_name>_successful event to listener' do
expect(listener).to receive(:create_meeting_successful).with(instance_of(model_class))
model_class.subscribe(listener)
model_class.create
end
end

context 'and model is not valid' do
it 'publishes create_<model_name>_failed event to listener' do
expect(listener).to receive(:create_meeting_failed).with(instance_of(model_class))
model_class.subscribe(listener)
model_class.create(title: nil)
end
it 'publishes <model_name>_created event to listener' do
expect(listener).to receive(:meeting_created).with(payload)
model_class.subscribe(listener)
model_class.create(id: id)
end
end

describe 'when updating' do
before do
model_class.create!
end

let(:model) { model_class.first }

context 'and model is valid' do
it 'publishes update_<model_name>_successful event to listener' do
expect(listener).to receive(:update_meeting_successful).with(instance_of(model_class))
model_class.subscribe(listener)
model.title = 'foo'
model.save
end
end
let(:model) { model_class.create(id: id) }

context 'and model is not valid' do
it 'publishes update_<model_name>_failed event to listener' do
expect(listener).to receive(:update_meeting_failed).with(instance_of(model_class))
model_class.subscribe(listener)
model.title = nil
model.save
end
it 'publishes <model_name>_updated event to listener' do
expect(listener).to receive(:meeting_updated).with(payload)
model_class.subscribe(listener)
model.title = 'foo'
model.save
end
end

describe 'create' do
it 'publishes an after_create event to listener' do
expect(listener).to receive(:after_create).with(instance_of(model_class))
expect(listener).to receive(:after_create).with(payload)
model_class.subscribe(listener)
model_class.create
model_class.create(id: id)
end
end

describe 'update' do
before { model_class.create! }

let(:model) { model_class.first }
let(:model) { model_class.create(id: id) }

it 'publishes an after_update event to listener' do
expect(listener).to receive(:after_update).with(instance_of(model_class))
expect(listener).to receive(:after_update).with(payload)
model.subscribe(listener)
model.update_attributes(title: 'new title')
end
end

describe 'destroy' do
before { model_class.create! }

let(:model) { model_class.first }
let(:model) { model_class.new(id: id) }

it 'publishes an after_destroy event to listener' do
expect(listener).to receive(:after_destroy).with(instance_of(model_class))
expect(listener).to receive(:after_destroy).with(payload)
model_class.subscribe(listener)
model.destroy
end
end

describe '#without_broadcasting' do
before { model_class.subscribe(listener) }
let(:model) { model_class.new }

it 'by does not broadcast the event' do
expect(listener).not_to receive(:after_save)
model.without_broadcasting { |m| m.save }
end
end
end
2 changes: 1 addition & 1 deletion wisper-mongoid.gemspec
Expand Up @@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
spec.required_ruby_version = '>= 1.9.3'

spec.add_dependency 'wisper', '~> 1.3'
spec.add_dependency 'mongoid', '>= 3.1', '< 5'
spec.add_dependency 'mongoid', '~> 5'
spec.add_development_dependency "rspec"
spec.add_development_dependency "coveralls"
spec.add_development_dependency "simplecov"
Expand Down