engine/lib/locomotive/responder.rb

52 lines
1.3 KiB
Ruby
Raw Normal View History

module Locomotive
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
Rails.logger.debug "TO JSON !!!!"
if get?
display resource
elsif has_errors?
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
elsif post?
2011-11-14 09:13:58 +00:00
with_flash_message do |message|
display resource, :location => api_location
end
elsif put?
2011-11-14 09:13:58 +00:00
with_flash_message do |message|
display resource, :status => :ok, :location => api_location
end
elsif has_empty_resource_definition?
display empty_resource, :status => :ok
else
2011-11-14 09:13:58 +00:00
with_flash_message do
head :ok
end
end
end
protected
2011-11-14 09:13:58 +00:00
def with_flash_message(type = :notice)
set_flash_message!
message = controller.flash[type]
controller.headers['X-Message'] = message
controller.headers['X-Message-Type'] = type
yield(message) if block_given?
controller.flash.discard # reset flash messages !
end
end
end