Created
March 5, 2013 22:16
-
-
Save jeshuaborges/5094837 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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