From 8917734174af3723bb4262695c26e95591b7dfef Mon Sep 17 00:00:00 2001 From: Becky Robran Date: Tue, 21 Jan 2020 19:41:13 -0700 Subject: [PATCH 1/2] Fixes user update --- app/controllers/admin/users_controller.rb | 11 ++++++- app/views/admin/users/_role.html.erb | 6 +++- .../admin/user/updates_role_status.rb | 30 +++++++++++++++++-- 3 files changed, 42 insertions(+), 5 deletions(-) diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index 92a28fa4d..e3281b83c 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -1,6 +1,7 @@ class Admin::UsersController < Admin::BaseController def index @users = User.unscoped.all + end def show @@ -26,7 +27,15 @@ def update def update_role @user = User.find(params[:user_id]) - @user.update(user_role_params) + @merchant = Merchant.where(name: params[:user][:merchant]).first + @user.update(user_role_params) + if params[:user][:role] == "merchant_employee" + @user.merchant_id = @merchant.id + @user.save + elsif params[:user][:role] != "merchant_employee" && @user.merchant_id != nil + @user.merchant_id = nil + @user.save + end flash[:success] = "The user role has been updated to #{@user.role}." redirect_to admin_dash_users_path end diff --git a/app/views/admin/users/_role.html.erb b/app/views/admin/users/_role.html.erb index 8f97c123d..2f523f765 100644 --- a/app/views/admin/users/_role.html.erb +++ b/app/views/admin/users/_role.html.erb @@ -3,7 +3,11 @@
<%= form_for [:admin, user], url: "/admin/users/#{user.id}/role", :html => { :style => 'background-color: #17BEBB;' } do |f| %> <%= f.label :role%> - <%= f.select :role, ['user', 'merchant_employee', 'merchant_admin', 'admin_user']%> + <%= f.select :role, ['user', 'merchant_employee', 'admin_user']%> + + <%= f.label :merchant %> + <%= f.select :merchant, Merchant.all.map {|merchant| [merchant.name]}, :prompt => 'Select a Merchant'%> + <%= f.submit %> <% end %>
diff --git a/spec/features/admin/user/updates_role_status.rb b/spec/features/admin/user/updates_role_status.rb index db513c325..d69a089b2 100644 --- a/spec/features/admin/user/updates_role_status.rb +++ b/spec/features/admin/user/updates_role_status.rb @@ -6,7 +6,7 @@ user = create(:random_user) user_2 = create(:random_user) user_3 = create(:random_user) - + merchant = create(:ray_merchant) allow_any_instance_of(ApplicationController).to receive(:current_user).and_return(admin) visit admin_dash_users_path @@ -15,12 +15,36 @@ within "#user-#{user.id}" do expect(page).to have_content('Role') - select "merchant_admin", :from => "Role" + select "merchant_employee", :from => "Role" + select("Vintage Vampire", from: "Merchant").select_option click_on "Update" + end + + user.reload + expect(user.role).to eq("merchant_employee") + expect(user.merchant_id).to eq(merchant.id) + expect(page).to have_content("The user role has been updated to #{user.role}.") + end + + it "won't update a merchant id if its not a merchant employee" do + admin = create(:admin_user) + user = create(:merchant_employee) + merchant = create(:ray_merchant) + allow_any_instance_of(ApplicationController).to receive(:current_user).and_return(admin) + + visit admin_dash_users_path + + expect(user.role).to eq("merchant_employee") + within "#user-#{user.id}" do + select "user", :from => "Role" + select("Vintage Vampire", from: "Merchant").select_option + click_on "Update" end + user.reload - expect(user.role).to eq("merchant_admin") + expect(user.role).to eq("user") + expect(user.merchant_id).to eq(nil) expect(page).to have_content("The user role has been updated to #{user.role}.") end end From 027755863bbb72f525739f8ea5650fe0e467ec75 Mon Sep 17 00:00:00 2001 From: Becky Robran Date: Sat, 25 Jan 2020 08:56:20 -0700 Subject: [PATCH 2/2] Adds testing to user role update --- app/controllers/admin/users_controller.rb | 14 ++++++++---- .../admin/user/updates_role_status.rb | 22 +++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index e3281b83c..455c49a42 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -28,16 +28,22 @@ def update def update_role @user = User.find(params[:user_id]) @merchant = Merchant.where(name: params[:user][:merchant]).first - @user.update(user_role_params) - if params[:user][:role] == "merchant_employee" + if params[:user][:role] == "merchant_employee" && @merchant + @user.update(user_role_params) @user.merchant_id = @merchant.id @user.save elsif params[:user][:role] != "merchant_employee" && @user.merchant_id != nil + @user.update(user_role_params) @user.merchant_id = nil @user.save end - flash[:success] = "The user role has been updated to #{@user.role}." - redirect_to admin_dash_users_path + if @user.update + flash[:success] = "The user role has been updated to #{@user.role}." + redirect_to admin_dash_users_path + else + flash[:error] = "Please try again. You may need to choose a merchant." + redirect_to admin_dash_users_path + end end def toggle_active diff --git a/spec/features/admin/user/updates_role_status.rb b/spec/features/admin/user/updates_role_status.rb index d69a089b2..10506933b 100644 --- a/spec/features/admin/user/updates_role_status.rb +++ b/spec/features/admin/user/updates_role_status.rb @@ -47,4 +47,26 @@ expect(user.merchant_id).to eq(nil) expect(page).to have_content("The user role has been updated to #{user.role}.") end + + it "won't update if no merchant is given for a merchant employee" do + admin = create(:admin_user) + user = create(:random_user) + merchant = create(:ray_merchant) + allow_any_instance_of(ApplicationController).to receive(:current_user).and_return(admin) + + visit admin_dash_users_path + + expect(user.role).to eq("user") + + within "#user-#{user.id}" do + select "merchant_employee", :from => "Role" + click_on "Update" + end + + user.reload + expect(user.role).to eq("user") + expect(user.merchant_id).to eq(nil) +save_and_open_page + expect(page).to have_content("Please try again. You may need to choose a merchant.") + end end