Skip to content

Instantly share code, notes, and snippets.

@cheald
Forked from aaronmcadam/admin.rb
Created November 1, 2012 22:58
Show Gist options
  • Save cheald/3997300 to your computer and use it in GitHub Desktop.
Save cheald/3997300 to your computer and use it in GitHub Desktop.
class Admin < APIWrapper
custom_post :login
collection_path 'admins'
resource_path 'admins/get/:id'
def self.get_admin(superadmin_id, admin_id)
get("admins/get/#{superadmin_id}/#{admin_id}")
end
def self.login(email, password)
post_raw('admins/login',
{ email: email, password: password }) { |data| parse_data(data) }
end
end
class APIWrapper
include Her::Model
uses_api $primary_api
def self.parse_data(data)
if data[:errors]
parsed_data = data[:errors]
else
parsed_data = data[:data]
end
if parsed_data.is_a?(Array)
new_collection(parsed_data)
else
new(parsed_data)
end
end
end
class SessionsController < ApplicationController
def new
redirect_to projects_path if signed_in?
end
def create
api_response = Admin.login(params[:session][:email].downcase, params[:session][:password])
begin
errors = api_response.code
rescue NoMethodError
errors = nil
end
if !errors.nil?
flash.now[:error] = "Invalid email/password combination"
render 'new'
else
sign_in api_response
redirect_back_or projects_path
end
end
def destroy
sign_out
redirect_to root_url
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment