Issue with rendering MessagePack
masterkain opened this issue · 3 comments
Hello,
I followed the instruction of the wiki in order to add MessagePack support.
I added the corresponding entry in config/initializers/mime_types.rb
Mime::Type.register "application/messagepack", :mpac
Then in controller I have this (Rails 3.2, git master, relevant bits):
class InternalsController < ApplicationController
respond_to :json, :xml, :mpac
self.responder = ActsAsApi::Responder
def user_info
respond_with(@owner, api_template: :v2_private)
end
The problem is that Rails is trying to render a messagepack view.
config/initializers/acts_as_api.rb
# Tell acts_as_api that it should accept an additional API format.
ActsAsApi::Config.accepted_api_formats << :mpac
# Add MessagePack as a Rails renderer.
ActionController::Renderers.add :mpac do |object, options|
self.content_type ||= Mime::MPAC # differs from the wiki, using this can result in a potential error due to loading order, let's go on for now though
self.response_body = MessagePack.pack(object)
end
# MessagePack doesn't come with the ability to serialize a TimeWithZone, monkey patch.
module ActiveSupport
class TimeWithZone
def to_msgpack(out = '')
self.to_s.to_msgpack(out)
end
end
end
RSpec tests fails:
Failure/Error: get 'user_info', format: 'mpac', k: string
ActionView::MissingTemplate:
Missing template internals/user_info, application/user_info with {:locale=>[:en, :en], :formats=>[:mpac], :handlers=>[:erb, :builder, :coffee, :haml]}. Searched in:
* "#<RSpec::Rails::ViewRendering::EmptyTemplatePathSetDecorator:0x007fcbe04a7f28>"
Am I missing something? Thanks!
Update: browsing the Rails source code in git I noticed that the ActionController::Renderers.add method never sets respond_body within the block, but just returns a string. So I tried this in the initializer:
ActionController::Renderers.add :mpac do |mpac, options|
self.content_type ||= Mime::MPAC
mpac.respond_to?(:to_msgpack) ? mpac.to_msgpack(options) : mpac
end
However rspec still fails with the same error. I can tell that mpac is loaded in renderers:
1.9.3p0 :001 > ActionController::Renderers::RENDERERS
=> #<Set: {:json, :js, :xml, :acts_as_api_jsonp, :mpac}>
So you are using it with Rails 3.2?
I'm not sure if anyone tried combining ActsAsApi::Responder
with MessagePack
so maybe there is also a bug :/
MessagePack is also the reason why I can't try out Rubinius, so I might think of dropping it.
In any case I'll try to play with this issue a little more if I have time, will report findings.
Closing this for now. Please re-open if issue is still present.