From df80ce906205f360ac0e971c03e233cc5d386846 Mon Sep 17 00:00:00 2001 From: Andrew Tomaka Date: Thu, 2 Oct 2014 16:35:55 -0400 Subject: [PATCH] User Controllers --- app/assets/javascripts/home.js.coffee | 3 + app/assets/javascripts/sessions.js.coffee | 3 + app/assets/stylesheets/home.css.scss | 3 + app/assets/stylesheets/sessions.css.scss | 3 + app/controllers/application_controller.rb | 7 ++- app/controllers/home_controller.rb | 4 ++ app/controllers/sessions_controller.rb | 14 +++++ app/helpers/home_helper.rb | 2 + app/helpers/sessions_helper.rb | 2 + app/views/home/show.html.erb | 2 + app/views/sessions/create.html.erb | 2 + app/views/sessions/destroy.html.erb | 2 + config/routes.rb | 58 ++------------------ test/controllers/home_controller_test.rb | 9 +++ test/controllers/sessions_controller_test.rb | 14 +++++ test/helpers/home_helper_test.rb | 4 ++ test/helpers/sessions_helper_test.rb | 4 ++ 17 files changed, 82 insertions(+), 54 deletions(-) create mode 100644 app/assets/javascripts/home.js.coffee create mode 100644 app/assets/javascripts/sessions.js.coffee create mode 100644 app/assets/stylesheets/home.css.scss create mode 100644 app/assets/stylesheets/sessions.css.scss create mode 100644 app/controllers/home_controller.rb create mode 100644 app/controllers/sessions_controller.rb create mode 100644 app/helpers/home_helper.rb create mode 100644 app/helpers/sessions_helper.rb create mode 100644 app/views/home/show.html.erb create mode 100644 app/views/sessions/create.html.erb create mode 100644 app/views/sessions/destroy.html.erb create mode 100644 test/controllers/home_controller_test.rb create mode 100644 test/controllers/sessions_controller_test.rb create mode 100644 test/helpers/home_helper_test.rb create mode 100644 test/helpers/sessions_helper_test.rb diff --git a/app/assets/javascripts/home.js.coffee b/app/assets/javascripts/home.js.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/home.js.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/javascripts/sessions.js.coffee b/app/assets/javascripts/sessions.js.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/sessions.js.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/stylesheets/home.css.scss b/app/assets/stylesheets/home.css.scss new file mode 100644 index 0000000..f0ddc68 --- /dev/null +++ b/app/assets/stylesheets/home.css.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the home controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/assets/stylesheets/sessions.css.scss b/app/assets/stylesheets/sessions.css.scss new file mode 100644 index 0000000..ccb1ed2 --- /dev/null +++ b/app/assets/stylesheets/sessions.css.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the Sessions controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index d83690e..543296f 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,5 +1,8 @@ class ApplicationController < ActionController::Base - # Prevent CSRF attacks by raising an exception. - # For APIs, you may want to use :null_session instead. protect_from_forgery with: :exception + helper_method :current_user + + def current_user + @current_user ||= User.find(session[:user_id]) if session[:user_id] + end end diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb new file mode 100644 index 0000000..89ff5b1 --- /dev/null +++ b/app/controllers/home_controller.rb @@ -0,0 +1,4 @@ +class HomeController < ApplicationController + def show + end +end diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb new file mode 100644 index 0000000..55c83cc --- /dev/null +++ b/app/controllers/sessions_controller.rb @@ -0,0 +1,14 @@ +class SessionsController < ApplicationController + def create + user = User.from_omniauth(env['omniauth.auth']) + session[:user_id] = user.id + + redirect_to root_path + end + + def destroy + session[:user_id] = nil + + redirect_to root_path + end +end diff --git a/app/helpers/home_helper.rb b/app/helpers/home_helper.rb new file mode 100644 index 0000000..23de56a --- /dev/null +++ b/app/helpers/home_helper.rb @@ -0,0 +1,2 @@ +module HomeHelper +end diff --git a/app/helpers/sessions_helper.rb b/app/helpers/sessions_helper.rb new file mode 100644 index 0000000..309f8b2 --- /dev/null +++ b/app/helpers/sessions_helper.rb @@ -0,0 +1,2 @@ +module SessionsHelper +end diff --git a/app/views/home/show.html.erb b/app/views/home/show.html.erb new file mode 100644 index 0000000..70bd6f9 --- /dev/null +++ b/app/views/home/show.html.erb @@ -0,0 +1,2 @@ +

Home#show

+

Find me in app/views/home/show.html.erb

diff --git a/app/views/sessions/create.html.erb b/app/views/sessions/create.html.erb new file mode 100644 index 0000000..c251174 --- /dev/null +++ b/app/views/sessions/create.html.erb @@ -0,0 +1,2 @@ +

Sessions#create

+

Find me in app/views/sessions/create.html.erb

diff --git a/app/views/sessions/destroy.html.erb b/app/views/sessions/destroy.html.erb new file mode 100644 index 0000000..d75237d --- /dev/null +++ b/app/views/sessions/destroy.html.erb @@ -0,0 +1,2 @@ +

Sessions#destroy

+

Find me in app/views/sessions/destroy.html.erb

diff --git a/config/routes.rb b/config/routes.rb index 3f66539..dd160c6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,56 +1,10 @@ Rails.application.routes.draw do - # The priority is based upon order of creation: first created -> highest priority. - # See how all your routes lay out with "rake routes". + get 'auth/:provider/callback', to: 'sessions#create' + get 'auth/failure', to: redirect('/') + get 'logout', to: 'sessions#destroy', as: 'logout' - # You can have the root of your site routed with "root" - # root 'welcome#index' + resources :sessions, only: [ :create, :destroy ] + resources :home, only: [ :show ] - # Example of regular route: - # get 'products/:id' => 'catalog#view' - - # Example of named route that can be invoked with purchase_url(id: product.id) - # get 'products/:id/purchase' => 'catalog#purchase', as: :purchase - - # Example resource route (maps HTTP verbs to controller actions automatically): - # resources :products - - # Example resource route with options: - # resources :products do - # member do - # get 'short' - # post 'toggle' - # end - # - # collection do - # get 'sold' - # end - # end - - # Example resource route with sub-resources: - # resources :products do - # resources :comments, :sales - # resource :seller - # end - - # Example resource route with more complex sub-resources: - # resources :products do - # resources :comments - # resources :sales do - # get 'recent', on: :collection - # end - # end - - # Example resource route with concerns: - # concern :toggleable do - # post 'toggle' - # end - # resources :posts, concerns: :toggleable - # resources :photos, concerns: :toggleable - - # Example resource route within a namespace: - # namespace :admin do - # # Directs /admin/products/* to Admin::ProductsController - # # (app/controllers/admin/products_controller.rb) - # resources :products - # end + root to: 'home#show' end diff --git a/test/controllers/home_controller_test.rb b/test/controllers/home_controller_test.rb new file mode 100644 index 0000000..1ca387c --- /dev/null +++ b/test/controllers/home_controller_test.rb @@ -0,0 +1,9 @@ +require 'test_helper' + +class HomeControllerTest < ActionController::TestCase + test "should get show" do + get :show + assert_response :success + end + +end diff --git a/test/controllers/sessions_controller_test.rb b/test/controllers/sessions_controller_test.rb new file mode 100644 index 0000000..9a35cf1 --- /dev/null +++ b/test/controllers/sessions_controller_test.rb @@ -0,0 +1,14 @@ +require 'test_helper' + +class SessionsControllerTest < ActionController::TestCase + test "should get create" do + get :create + assert_response :success + end + + test "should get destroy" do + get :destroy + assert_response :success + end + +end diff --git a/test/helpers/home_helper_test.rb b/test/helpers/home_helper_test.rb new file mode 100644 index 0000000..4740a18 --- /dev/null +++ b/test/helpers/home_helper_test.rb @@ -0,0 +1,4 @@ +require 'test_helper' + +class HomeHelperTest < ActionView::TestCase +end diff --git a/test/helpers/sessions_helper_test.rb b/test/helpers/sessions_helper_test.rb new file mode 100644 index 0000000..7d44e09 --- /dev/null +++ b/test/helpers/sessions_helper_test.rb @@ -0,0 +1,4 @@ +require 'test_helper' + +class SessionsHelperTest < ActionView::TestCase +end