Skip to content

Commit

Permalink
Add subchannel specs in.
Browse files Browse the repository at this point in the history
  • Loading branch information
patricksrobertson committed Nov 29, 2011
1 parent 453ee2a commit 4c67f34
Show file tree
Hide file tree
Showing 13 changed files with 124 additions and 60 deletions.
4 changes: 3 additions & 1 deletion lib/backbone_sync-rails/faye/event.rb
Expand Up @@ -49,7 +49,9 @@ def data
end

def subchannel
@subchannel || @model.class.try(:faye_channel) || @model.class.table_name
@subchannel || @model.faye_channel
rescue NoMethodError => e
@model.class.table_name
end
end
end
Expand Down
12 changes: 4 additions & 8 deletions lib/backbone_sync-rails/faye/observer.rb
Expand Up @@ -5,25 +5,25 @@ module Rails
module Faye
module Observer
def after_update(model)
Event.new(model, :update, subchannel(model)).broadcast
Event.new(model, :update).broadcast
rescue *NET_HTTP_EXCEPTIONS => e
handle_net_http_exception(e)
end

def after_create(model)
Event.new(model, :create, subchannel(model)).broadcast
Event.new(model, :create).broadcast
rescue *NET_HTTP_EXCEPTIONS => e
handle_net_http_exception(e)
end

def after_destroy(model)
Event.new(model, :destroy, subchannel(model)).broadcast
Event.new(model, :destroy).broadcast
rescue *NET_HTTP_EXCEPTIONS => e
handle_net_http_exception(e)
end

def after_touch(model)
Event.new(model, :update, subchannel(model)).broadcast
Event.new(model, :update).broadcast
rescue *NET_HTTP_EXCEPTIONS => e
handle_net_http_exception(e)
end
Expand All @@ -36,10 +36,6 @@ def handle_net_http_exception(exception)
::Rails.logger.error(exception.backtrace.join("\n"))
::Rails.logger.error("")
end

def subchannel(model)
nil
end
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions spec/dummy/app/models/backbone_sync_observer.rb
@@ -0,0 +1,4 @@
class BackboneSyncObserver < ActiveRecord::Observer
observe :task, :comment
include BackboneSync::Rails::Faye::Observer
end
7 changes: 7 additions & 0 deletions spec/dummy/app/models/comment.rb
@@ -0,0 +1,7 @@
class Comment < ActiveRecord::Base
belongs_to :task

def faye_channel
"tasks/#{task.id}/comments"
end
end
1 change: 1 addition & 0 deletions spec/dummy/app/models/task.rb
@@ -1,2 +1,3 @@
class Task < ActiveRecord::Base
has_many :comments
end
3 changes: 0 additions & 3 deletions spec/dummy/app/models/task_observer.rb

This file was deleted.

2 changes: 1 addition & 1 deletion spec/dummy/config/application.rb
Expand Up @@ -25,7 +25,7 @@ class Application < Rails::Application
# config.plugins = [ :exception_notification, :ssl_requirement, :all ]

# Activate observers that should always be running.
config.active_record.observers = :task_observer
config.active_record.observers = :backbone_sync_observer

# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
Expand Down
4 changes: 0 additions & 4 deletions spec/dummy/config/initializers/backbone_sync.rb
Expand Up @@ -3,8 +3,4 @@
BackboneSync::Rails::Faye.root_address = {
'test' => 'http://localhost:9293',
}[Rails.env]

def self.faye_channel
"#{table_name}"
end
end
10 changes: 10 additions & 0 deletions spec/dummy/db/migrate/20111128203027_create_comments.rb
@@ -0,0 +1,10 @@
class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
t.text :body
t.integer :task_id

t.timestamps
end
end
end
Binary file modified spec/dummy/db/test.sqlite3
Binary file not shown.
7 changes: 0 additions & 7 deletions spec/dummy_spec.rb

This file was deleted.

125 changes: 89 additions & 36 deletions spec/faye/observer_spec.rb
@@ -1,50 +1,103 @@
require 'spec_helper'

describe BackboneSync::Rails::Faye::Observer do
it 'broadcasts updates over a Faye channel' do
task = Factory(:task, title: 'Old Title')
new_attributes = Factory.attributes_for(:task, title: 'New Title')
task.update_attributes(new_attributes)

expected_attributes = JSON.parse(task.to_json)

FakeFayeServer.messages.should include({
"channel" => "/sync/tasks",
"data" => {
"update" => {
task.id.to_s => expected_attributes
context 'Default model subchannel settings' do
it 'broadcasts updates over a Faye channel' do
task = Factory(:task, title: 'Old Title')
new_attributes = Factory.attributes_for(:task, title: 'New Title')
task.update_attributes(new_attributes)

expected_attributes = JSON.parse(task.to_json)

FakeFayeServer.messages.should include({
"channel" => "/sync/tasks",
"data" => {
"update" => {
task.id.to_s => expected_attributes
}
}
}
})
end
})
end

it 'broadcasts destroys over a Faye channel' do
task = Factory(:task, title: 'Old Title')
task.destroy

it 'broadcasts destroys over a Faye channel' do
task = Factory(:task, title: 'Old Title')
task.destroy
expected_attributes = JSON.parse(task.to_json)

expected_attributes = JSON.parse(task.to_json)
FakeFayeServer.messages.should include({
"channel" => "/sync/tasks",
"data" => {
"destroy" => {
task.id.to_s => expected_attributes
}
}
})
end

it 'broadcasts creates over a Faye channel' do
task = Factory(:task)
expected_attributes = JSON.parse(task.to_json)

FakeFayeServer.messages.should include({
"channel" => "/sync/tasks",
"data" => {
"destroy" => {
task.id.to_s => expected_attributes
FakeFayeServer.messages.should include({
"channel" => "/sync/tasks",
"data" => {
"create" => {
task.id.to_s => expected_attributes
}
}
}
})
})
end
end

it 'broadcasts creates over a Faye channel' do
task = Factory(:task)
expected_attributes = JSON.parse(task.to_json)
context 'Model that is set in a child relationship' do
let(:task) { Factory(:task) }

it 'broadcasts updates over a Faye channel' do
comment = Factory(:comment, task: task, body: 'this body isn\'t too great')
new_attributes = Factory.attributes_for(:comment, body: 'I like this body')
comment.update_attributes(new_attributes)

expected_attributes = JSON.parse(comment.to_json)

FakeFayeServer.messages.should include({
"channel" => "/sync/tasks/#{task.id}/comments",
"data" => {
"update" => {
comment.id.to_s => expected_attributes
}
}
})
end

it 'broadcasts destroys over a Faye channel' do
comment = Factory(:comment, task: task)
comment.destroy

expected_attributes = JSON.parse(comment.to_json)

FakeFayeServer.messages.should include({
"channel" => "/sync/tasks/#{task.id}/comments",
"data" => {
"destroy" => {
comment.id.to_s => expected_attributes
}
}
})
end

it 'broadcasts creates over a Faye channel' do
comment = Factory(:comment, task: task)
expected_attributes = JSON.parse(comment.to_json)

FakeFayeServer.messages.should include({
"channel" => "/sync/tasks",
"data" => {
"create" => {
task.id.to_s => expected_attributes
FakeFayeServer.messages.should include({
"channel" => "/sync/tasks/#{task.id}/comments",
"data" => {
"create" => {
comment.id.to_s => expected_attributes
}
}
}
})
})
end
end
end
5 changes: 5 additions & 0 deletions spec/support/factories.rb
Expand Up @@ -3,4 +3,9 @@
title "An entirely relevent task"
completed false
end

factory :comment do
body "This body is hot."
association :task
end
end

0 comments on commit 4c67f34

Please sign in to comment.