From bca30e74f093e824ab33f42a8e5197354d184d28 Mon Sep 17 00:00:00 2001 From: patricia mitchell Date: Mon, 3 Nov 2014 17:08:55 -0500 Subject: [PATCH] Finish user edit, update, index and destroy actions --- Gemfile | 3 ++ Gemfile.lock | 8 +++ app/assets/stylesheets/custom.css.scss | 12 +++++ app/controllers/sessions_controller.rb | 2 +- app/controllers/users_controller.rb | 50 +++++++++++++++++++ app/helpers/sessions_helper.rb | 16 ++++++ app/helpers/users_helper.rb | 5 +- app/models/user.rb | 2 +- app/views/layouts/_header.html.erb | 4 +- app/views/users/_user.html.erb | 8 +++ app/views/users/edit.html.erb | 29 +++++++++++ app/views/users/index.html.erb | 10 ++++ .../20141103214952_add_admin_to_users.rb | 5 ++ db/schema.rb | 7 +-- db/seeds.rb | 15 ++++++ test/controllers/users_controller_test.rb | 50 ++++++++++++++++++- test/fixtures/users.yml | 23 +++++++++ test/integration/users_edit_test.rb | 36 +++++++++++++ test/integration/users_index_test.rb | 30 +++++++++++ 19 files changed, 304 insertions(+), 11 deletions(-) create mode 100644 app/views/users/_user.html.erb create mode 100644 app/views/users/edit.html.erb create mode 100644 app/views/users/index.html.erb create mode 100644 db/migrate/20141103214952_add_admin_to_users.rb create mode 100644 test/integration/users_edit_test.rb create mode 100644 test/integration/users_index_test.rb diff --git a/Gemfile b/Gemfile index f34ca9a..dcd085b 100644 --- a/Gemfile +++ b/Gemfile @@ -3,6 +3,9 @@ ruby '2.1.1' gem 'rails', '4.2.0.beta2' gem 'bcrypt', '3.1.7' +gem 'faker', '1.4.2' +gem 'will_paginate', '3.0.7' +gem 'bootstrap-will_paginate', '0.0.10' gem 'sass-rails' # '~> 4.0.3' gem 'bootstrap-sass' diff --git a/Gemfile.lock b/Gemfile.lock index f50af0b..2af145f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -43,6 +43,8 @@ GEM debug_inspector (>= 0.0.1) bootstrap-sass (3.2.0.2) sass (~> 3.2) + bootstrap-will_paginate (0.0.10) + will_paginate builder (3.2.2) byebug (3.4.0) columnize (~> 0.8) @@ -63,6 +65,8 @@ GEM debugger-linecache (1.2.0) erubis (2.7.0) execjs (2.2.2) + faker (1.4.2) + i18n (~> 0.5) ffi (1.9.6) formatador (0.2.5) globalid (0.3.0) @@ -199,6 +203,7 @@ GEM binding_of_caller (= 0.7.3.pre1) railties (~> 4.0) sprockets-rails (>= 2.0, < 4.0) + will_paginate (3.0.7) PLATFORMS ruby @@ -206,8 +211,10 @@ PLATFORMS DEPENDENCIES bcrypt (= 3.1.7) bootstrap-sass + bootstrap-will_paginate (= 0.0.10) byebug (= 3.4.0) coffee-rails (= 4.0.1) + faker (= 1.4.2) guard-minitest (= 2.3.1) jbuilder (= 2.2.3) jquery-rails (= 4.0.0.beta2) @@ -225,3 +232,4 @@ DEPENDENCIES uglifier (= 2.5.3) unicorn (= 4.8.3) web-console (= 2.0.0.beta3) + will_paginate (= 3.0.7) diff --git a/app/assets/stylesheets/custom.css.scss b/app/assets/stylesheets/custom.css.scss index ab8ef56..fa8badc 100644 --- a/app/assets/stylesheets/custom.css.scss +++ b/app/assets/stylesheets/custom.css.scss @@ -217,6 +217,18 @@ input { width: auto; margin-left: 0; } + +/* Users index */ + +.users { + list-style: none; + margin: 0; + li { + overflow: auto; + padding: 10px 0; + border-bottom: 1px solid $gray-lighter; + } +} /* miscellaneous */ diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 4293f0b..3191b24 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -8,7 +8,7 @@ def create if user && user.authenticate(params[:session][:password]) log_in user params[:session][:remember_me] == '1' ? remember(user) : forget(user) - redirect_to user + redirect_back_or user else flash.now[:danger] = 'Invalid email/password combination' # Not quite right! render 'new' diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 8b874ce..d485662 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,4 +1,11 @@ class UsersController < ApplicationController + before_action :logged_in_user, only: [:index, :edit, :update, :destroy] + before_action :correct_user, only: [:edit, :update] + before_action :admin_user, only: :destroy + + def index + @users = User.paginate(page: params[:page]) + end def show @user = User.find(params[:id]) @@ -20,10 +27,53 @@ def create end end + def edit + # @user = User.find(params[:id]) + end + + def update + # @user = User.find(params[:id]) + if @user.update_attributes(user_params) + # Handle a successful update. + flash[:success] = "Profile updated" + redirect_to @user + else + render 'edit' + end + end + + def destroy + User.find(params[:id]).destroy + flash[:success] = "User deleted" + redirect_to users_url + end + private def user_params params.require(:user).permit(:name, :email, :password, :password_confirmation) end + + # Before filters + + # Confirms a logged-in user. + def logged_in_user + unless logged_in? + store_location + flash[:danger] = "Please log in." + redirect_to login_url + end + end + + # Confirms the correct user. + def correct_user + @user = User.find(params[:id]) + redirect_to(root_url) unless current_user?(@user) + end + + # Confirms an admin user. + def admin_user + redirect_to(root_url) unless current_user.admin? + end end diff --git a/app/helpers/sessions_helper.rb b/app/helpers/sessions_helper.rb index 6b4d3e1..d642017 100644 --- a/app/helpers/sessions_helper.rb +++ b/app/helpers/sessions_helper.rb @@ -12,6 +12,11 @@ def remember(user) cookies.permanent[:remember_token] = user.remember_token end + # Returns true if the given user is the current user. + def current_user?(user) + user == current_user + end + # Returns the user corresponding to the remember token cookie. def current_user if (user_id = session[:user_id]) @@ -44,5 +49,16 @@ def log_out @current_user = nil end + # Redirects to stored location (or to the default). + def redirect_back_or(default) + redirect_to(session[:forwarding_url] || default) + session.delete(:forwarding_url) + end + + # Stores the URL trying to be accessed. + def store_location + session[:forwarding_url] = request.url if request.get? + end + end \ No newline at end of file diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb index a1ac53e..670685f 100644 --- a/app/helpers/users_helper.rb +++ b/app/helpers/users_helper.rb @@ -1,9 +1,10 @@ module UsersHelper # Returns the Gravatar for the given user. - def gravatar_for(user) + def gravatar_for(user, options = { size: 80 }) gravatar_id = Digest::MD5::hexdigest(user.email.downcase) - gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}" + size = options[:size] + gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}?s=#{size}" image_tag(gravatar_url, alt: user.name, class: "gravatar") end end diff --git a/app/models/user.rb b/app/models/user.rb index 62aa3b9..71dece2 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -7,7 +7,7 @@ class User < ActiveRecord::Base format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false } has_secure_password - validates :password, length: { minimum: 6 } + validates :password, length: { minimum: 6 }, allow_blank: true # Returns the hash digest of the given string. def User.digest(string) diff --git a/app/views/layouts/_header.html.erb b/app/views/layouts/_header.html.erb index f582396..4a5657d 100644 --- a/app/views/layouts/_header.html.erb +++ b/app/views/layouts/_header.html.erb @@ -6,14 +6,14 @@
  • <%= link_to "Home", root_path %>
  • <%= link_to "Help", help_path %>
  • <% if logged_in? %> -
  • <%= link_to "Users", '#' %>
  • +
  • <%= link_to "Users", users_path %>