Merge pull request #4 from ccocchi/responder

Add custom responder
This commit is contained in:
Christopher Cocchi-Perrier 2012-09-11 02:53:15 -07:00
commit 5ca0a44241
3 changed files with 40 additions and 5 deletions

View File

@ -1,5 +1,8 @@
# CHANGELOG
## 0.1.3 (unreleased)
* Add custom Responder
## 0.1.2
* Add RablRails#render method (see README or source code)
* Fix fail when JSON engine is not found. Now fallback to MultiJson.default_adapter

View File

@ -18,6 +18,8 @@ require 'multi_json'
module RablRails
extend Renderer
autoload :Responder, 'rabl-rails/responder'
mattr_accessor :cache_templates
@@cache_templates = true
@ -27,8 +29,16 @@ module RablRails
mattr_reader :json_engine
@@json_engine = :yajl
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 self.json_engine=(name)

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