added socket renderer spec and slight refactor

This commit is contained in:
maxwell 2010-06-29 11:46:08 -07:00
parent a683a2c1aa
commit 1b2745e788
3 changed files with 59 additions and 1 deletions

View file

@ -6,9 +6,9 @@ module WebSocket
EM.add_timer(0.1) do
@channel = EM::Channel.new
puts @channel.inspect
include SocketRenderer
SocketRenderer.instantiate_view
end
EventMachine::WebSocket.start(:host => "0.0.0.0", :port => 8080, :debug =>true) do |ws|

35
lib/socket_render.rb Normal file
View file

@ -0,0 +1,35 @@
module SocketRenderer
require 'app/helpers/application_helper'
def self.instantiate_view
@view = ActionView::Base.new(ActionController::Base.view_paths, {})
class << @view
include ApplicationHelper
include Rails.application.routes.url_helpers
include ActionController::RequestForgeryProtection::ClassMethods
def protect_against_forgery?
false
end
end
end
def self.view_hash(object)
begin
puts "I be working hard"
v = view_for(object)
puts v.inspect
rescue Exception => e
puts "in failzord " + v.inspect
puts object.inspect
puts e.message
raise e
end
puts "i made it here"
{:class =>object.class.to_s.underscore.pluralize, :html => v}
end
def self.view_for(object)
@view.render @view.type_partial(object), :post => object
end
end

View file

@ -0,0 +1,23 @@
require File.dirname(__FILE__) + '/../spec_helper'
describe SocketRenderer do
before do
SocketRenderer.instantiate_view
@user = Factory.create(:user, :email => "bob@jones.com")
@user.profile = Factory.build(:profile, :person => @user)
end
it 'should render a partial for a status message' do
message = Factory.create(:status_message, :person => @user)
html = SocketRenderer.view_for message
html.include? message.message
end
it 'should prepare a class/view hash' do
message = Factory.create(:status_message, :person => @user)
hash = SocketRenderer.view_hash(message)
hash[:class].should == "status_messages"
end
end