commit
5ca0a44241
@ -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
|
||||
|
@ -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)
|
||||
|
22
lib/rabl-rails/responder.rb
Normal file
22
lib/rabl-rails/responder.rb
Normal 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
|
Loading…
Reference in New Issue
Block a user