Add custom Rabl reponder

This commit is contained in:
ccocchi 2012-07-03 17:45:23 +02:00
parent 57366b7fd6
commit 49f7fe24ae
2 changed files with 33 additions and 5 deletions

View File

@ -14,19 +14,25 @@ require 'rabl-rails/library'
require 'rabl-rails/handler'
require 'rabl-rails/railtie'
module RablRails
extend self
autoload :Responder, 'rabl-rails/responder'
mattr_accessor :cache_templates
@@cache_templates = true
def configure
mattr_accessor :use_custom_responder
@@use_custom_responder = false
mattr_accessor :responder_default_template
@@responder_default_template = 'show'
def self.configure
yield self
ActionController::Base.responder = Responder if self.use_custom_responder
end
def cache_templates?
def self.cache_templates?
ActionController::Base.perform_caching && @@cache_templates
end
end

View File

@ -0,0 +1,22 @@
module RablRails
#
# Override default responder's api behavior to not
# user to_format methods on a resource as a default
# representation but instead use a rabl template
#
class Responder < ActionController::Responder
protected
def api_behavior(error)
rabl_options = options.merge(template: RablRails.responder_default_template)
if get?
controller.default_render rabl_options
elsif post?
controller.default_render rabl_options.merge!(status: :created, location: api_location)
else
head :no_content
end
end
end
end