Add custom Rabl reponder
This commit is contained in:
parent
57366b7fd6
commit
49f7fe24ae
|
@ -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
|
||||
|
|
|
@ -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
|
Loading…
Reference in New Issue