2010-07-13 00:46:17 +00:00
|
|
|
module Locomotive
|
2011-11-09 01:35:59 +00:00
|
|
|
class Responder < ::ActionController::Responder
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-07-13 23:37:02 +00:00
|
|
|
include ::Responders::FlashResponder
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2011-11-10 12:29:59 +00:00
|
|
|
# by default flash_now messages if the resource has errors
|
|
|
|
def set_flash_now?
|
|
|
|
super || has_errors?
|
|
|
|
end
|
2010-07-13 00:46:17 +00:00
|
|
|
|
2011-11-10 12:29:59 +00:00
|
|
|
def to_json
|
2010-07-13 00:46:17 +00:00
|
|
|
if get?
|
|
|
|
display resource
|
|
|
|
elsif has_errors?
|
2011-11-25 01:04:42 +00:00
|
|
|
Rails.logger.debug "--> ERRORS #{resource.errors.inspect}" # FIXME: debug purpose
|
|
|
|
with_flash_message(:alert) do |message|
|
2011-11-14 09:13:58 +00:00
|
|
|
display resource.errors, :status => :unprocessable_entity
|
|
|
|
end
|
2010-07-13 00:46:17 +00:00
|
|
|
elsif post?
|
2011-12-02 17:20:59 +00:00
|
|
|
set_flash_message!
|
|
|
|
display resource, :location => api_location
|
2011-11-10 12:29:59 +00:00
|
|
|
elsif put?
|
2011-11-14 09:13:58 +00:00
|
|
|
with_flash_message do |message|
|
2011-11-11 08:39:25 +00:00
|
|
|
display resource, :status => :ok, :location => api_location
|
|
|
|
end
|
2011-11-10 12:29:59 +00:00
|
|
|
elsif has_empty_resource_definition?
|
|
|
|
display empty_resource, :status => :ok
|
2010-07-13 00:46:17 +00:00
|
|
|
else
|
2011-11-14 09:13:58 +00:00
|
|
|
with_flash_message do
|
|
|
|
head :ok
|
|
|
|
end
|
2010-07-13 00:46:17 +00:00
|
|
|
end
|
2011-11-11 08:39:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2011-11-14 09:13:58 +00:00
|
|
|
def with_flash_message(type = :notice)
|
2011-11-11 08:39:25 +00:00
|
|
|
set_flash_message!
|
|
|
|
message = controller.flash[type]
|
|
|
|
|
2011-11-25 01:04:42 +00:00
|
|
|
controller.headers['X-Message'] = message
|
|
|
|
controller.headers['X-Message-Type'] = type
|
2011-11-11 08:39:25 +00:00
|
|
|
|
|
|
|
yield(message) if block_given?
|
|
|
|
|
|
|
|
controller.flash.discard # reset flash messages !
|
2010-07-13 00:46:17 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-07-13 00:46:17 +00:00
|
|
|
end
|
2011-11-09 01:35:59 +00:00
|
|
|
end
|