From a0f4e053a66fbc5b959c89512f06b79dafbc0cee Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Tue, 18 Nov 2014 08:37:42 +1100 Subject: [PATCH] Section 7.4.7: Add ability to edit and update users --- .../app/controllers/admin/users_controller.rb | 26 +++++++++++++++ ticketee/app/views/admin/users/edit.html.erb | 3 ++ ticketee/app/views/admin/users/show.html.erb | 3 ++ .../spec/features/admin/editing_users_spec.rb | 33 +++++++++++++++++++ 4 files changed, 65 insertions(+) create mode 100644 ticketee/app/views/admin/users/edit.html.erb create mode 100644 ticketee/app/views/admin/users/show.html.erb create mode 100644 ticketee/spec/features/admin/editing_users_spec.rb diff --git a/ticketee/app/controllers/admin/users_controller.rb b/ticketee/app/controllers/admin/users_controller.rb index 9fc81b8..bf80c28 100644 --- a/ticketee/app/controllers/admin/users_controller.rb +++ b/ticketee/app/controllers/admin/users_controller.rb @@ -1,4 +1,6 @@ class Admin::UsersController < Admin::BaseController + before_action :set_user, only: [:show, :edit, :update, :destroy] + def index @users = User.order(:email) end @@ -21,7 +23,29 @@ def create end end + def edit + end + + def update + if params[:user][:password].blank? + params[:user].delete(:password) + params[:user].delete(:password_confirmation) + end + if @user.update(user_params) + flash[:notice] = "User has been updated." + redirect_to admin_users_path + else + flash[:alert] = "User has not been updated." + render "edit" + end + end + private + + def set_user + @user = User.find(params[:id]) + end + def user_params params.require(:user).permit(:name, :email, @@ -29,4 +53,6 @@ def user_params :password_confirmation, :admin) end + + end diff --git a/ticketee/app/views/admin/users/edit.html.erb b/ticketee/app/views/admin/users/edit.html.erb new file mode 100644 index 0000000..540c656 --- /dev/null +++ b/ticketee/app/views/admin/users/edit.html.erb @@ -0,0 +1,3 @@ +

Editing a User

+ +<%= render "form" %> diff --git a/ticketee/app/views/admin/users/show.html.erb b/ticketee/app/views/admin/users/show.html.erb new file mode 100644 index 0000000..3aa5356 --- /dev/null +++ b/ticketee/app/views/admin/users/show.html.erb @@ -0,0 +1,3 @@ +

<%= @user %>

+ +<%= link_to "Edit User", edit_admin_user_path(@user) %> diff --git a/ticketee/spec/features/admin/editing_users_spec.rb b/ticketee/spec/features/admin/editing_users_spec.rb new file mode 100644 index 0000000..83f99d1 --- /dev/null +++ b/ticketee/spec/features/admin/editing_users_spec.rb @@ -0,0 +1,33 @@ +require "rails_helper" + +feature "Editing a user" do + let!(:admin_user) { FactoryGirl.create(:admin_user) } + let!(:user) { FactoryGirl.create(:user) } + + before do + login_as(admin_user) + visit "/" + click_link "Admin" + click_link "Users" + click_link user.email + click_link "Edit User" + end + + scenario "Updating a user's details" do + fill_in "Email", with: "newguy@example.com" + click_button "Update User" + + expect(page).to have_content("User has been updated.") + + expect(page).to have_content("newguy@example.com") + expect(page).to_not have_content(user.email) + end + + scenario "Toggling user's admin ability" do + check "Is an admin?" + click_button "Update User" + + expect(page).to have_content("User has been updated.") + expect(page).to have_content("#{user.email} (Admin)") + end +end