Module: Convenient::Controller
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/convenient-actionpack/controller.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#on_action_error(error) ⇒ Object
This is problematic and respond_with seems to function mostly normally.
- #wrap ⇒ Object
Instance Method Details
#on_action_error(error) ⇒ Object
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.: github.com/rails/rails/issues/2798 def respond(*args)
#puts "respond args=#{args.inspect}"
= args.
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!()) 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
[:location] = ([:status] == :accepted || params[:action] == :create) && ![:location] ? show_uri(data) : [: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, 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
53 54 55 56 57 58 59 60 |
# File 'lib/convenient-actionpack/controller.rb', line 53 def on_action_error(error) case params[:format].to_sym when :json respond_with({errors: [error.]}, {location: nil, status: :internal_server_error}) else raise error # should include prior backtrace end end |
#wrap ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/convenient-actionpack/controller.rb', line 8 def wrap begin yield rescue on_action_error($!) end end |