module Convenient::Controller

Public Instance Methods

on_action_error(error) click to toggle source
This is problematic and respond_with seems to function mostly normally.
TODO: how to get around needing to specify location, as mentioned here, without interfering with existing functionality for html format, etc.: https://github.com/rails/rails/issues/2798

def respond(*args)

#puts "respond args=#{args.inspect}"
options = args.extract_options!
raise ArgumentError.new 'respond takes one resource and an optional array of options' if args.size != 1
data = args[0]
if data.respond_to?(:errors) && data.try(:errors) && !(data.errors.respond_to?(:size) && data.errors.size == 0)
  # was setting status: :unprocessable_entity but that is done by respond_with already
  respond_with({errors: data.errors}, {location: nil}.merge!(options)) do
    yield if block_given?
  end
else
  # respond_with requires location to be nil if not required, or it can be specified in options, in which case, we'll take it.
  case params[:format].to_sym
  when :json
    options[:location] = (options[:status] == :accepted || params[:action] == :create) && !options[:location] ? show_uri(data) : options[:location]
  end

  # respond_with should do this already
  #options[:status] = :created if params[:action] == :create && !options[:status]

  #puts "GOING TO respond_with #{data.inspect}, #{options.inspect}"

  respond_with data, options do
    # respond_with should do this already
    #head :no_content if params[:action] == :destroy && !block_given?

    yield if block_given?
  end
end

end

def show_uri(data)

send("show_#{data.class.name.underscore}_path", data)

end

# File lib/convenient-actionpack/controller.rb, line 53
def on_action_error(error)
  case params[:format].to_sym
  when :json
    respond_with({errors: [error.message]}, {location: nil, status: :internal_server_error})
  else
    raise error # should include prior backtrace
  end
end
wrap() { || ... } click to toggle source
# File lib/convenient-actionpack/controller.rb, line 8
def wrap
  begin
    yield
  rescue
    on_action_error($!)
  end
end