Skip to content

Instantly share code, notes, and snippets.

@jeshuaborges
Created March 5, 2013 22:16
Show Gist options
  • Save jeshuaborges/5094837 to your computer and use it in GitHub Desktop.
Save jeshuaborges/5094837 to your computer and use it in GitHub Desktop.
class SessionsController < ApplicationController
rescue_from Mutations::ValidationException, with: :validation_handler
def create
with_param :session do |params|
session[:user_id] = SessionCreator.run! params
end
render_success
end
def new
@auth_action = request[:auth_action] || root_path
end
def destroy
session[:user_id] = nil
redirect_to root_path, notice: 'You have been signed out.'
end
def validation_handler(exception)
error_hash = Hash[exception.errors.map{|(k,v)| [k, v.message]}]
error_hash = {@current_param_key => error_hash} if @current_param_key
respond_to do |format|
format.json do
render json: {errors: error_hash}, status: :bad_request
end
format.all { raise }
end
end
def render_success
render json: {message: 'Success.'}
end
def with_param(key)
@current_param_key = key
yield params[key]
@current_param_key = nil
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment