2012-01-09 14:49:59 +00:00
|
|
|
module Locomotive
|
|
|
|
module ActionController
|
|
|
|
class Responder < ::ActionController::Responder
|
|
|
|
|
|
|
|
include ::Responders::FlashResponder
|
|
|
|
|
|
|
|
# by default flash_now messages if the resource has errors
|
|
|
|
def set_flash_now?
|
|
|
|
super || has_errors?
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_json
|
|
|
|
if get?
|
|
|
|
display resource
|
|
|
|
elsif has_errors?
|
|
|
|
with_flash_message(:alert) do
|
|
|
|
display resource.errors, :status => :unprocessable_entity
|
|
|
|
end
|
|
|
|
elsif post?
|
|
|
|
in_header = controller.request.headers['X-Flash'] == 'true'
|
|
|
|
with_flash_message(:notice, in_header) do
|
|
|
|
display resource, :location => api_location
|
|
|
|
end
|
|
|
|
elsif put?
|
|
|
|
with_flash_message do |message|
|
|
|
|
display resource, :status => :ok, :location => api_location
|
|
|
|
end
|
|
|
|
elsif delete?
|
|
|
|
with_flash_message do |message|
|
|
|
|
display resource, :status => :ok, :location => api_location
|
|
|
|
end
|
|
|
|
elsif has_empty_resource_definition?
|
|
|
|
display empty_resource, :status => :ok
|
|
|
|
else
|
|
|
|
with_flash_message do
|
|
|
|
head :ok
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def with_flash_message(type = :notice, in_header = true)
|
|
|
|
if in_header
|
|
|
|
set_flash_message!
|
|
|
|
message = controller.flash[type]
|
|
|
|
|
2012-01-24 00:00:41 +00:00
|
|
|
unless message.blank?
|
|
|
|
controller.headers['X-Message'] = message
|
2012-02-07 11:19:54 +00:00
|
|
|
controller.headers['X-Message-Type'] = type.to_s
|
2012-01-24 00:00:41 +00:00
|
|
|
end
|
2012-01-09 14:49:59 +00:00
|
|
|
|
|
|
|
yield if block_given?
|
|
|
|
|
|
|
|
controller.flash.discard # reset flash messages !
|
|
|
|
else
|
|
|
|
set_flash_message!
|
|
|
|
|
|
|
|
yield if block_given?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|