From f28241cc5e290c84fb59aab59900aeedb86b7c16 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Wed, 19 Aug 2020 09:53:08 +0100 Subject: [PATCH] Merge duplicate Spree::User#superadmin? into existing Spree::admin? --- app/models/spree/user.rb | 10 +--------- config/routes/admin.rb | 2 +- spec/models/spree/user_spec.rb | 25 +++++-------------------- 3 files changed, 7 insertions(+), 30 deletions(-) diff --git a/app/models/spree/user.rb b/app/models/spree/user.rb index 83f64748aa..059b26a2de 100644 --- a/app/models/spree/user.rb +++ b/app/models/spree/user.rb @@ -53,11 +53,11 @@ module Spree spree_roles.where(name: role_in_question.to_s).any? end + # Checks whether the specified user is a superadmin, with full control of the instance def admin? has_spree_role?('admin') end - # handle_asynchronously will define send_reset_password_instructions_with_delay. # If handle_asynchronously is called twice, we get an infinite job loop. handle_asynchronously :send_reset_password_instructions unless method_defined? :send_reset_password_instructions_with_delay @@ -120,14 +120,6 @@ module Spree end end - # Checks whether the specified user is a superadmin, with full control of the - # instance - # - # @return [Boolean] - def superadmin? - has_spree_role?('admin') - end - def generate_spree_api_key! self.spree_api_key = SecureRandom.hex(24) save! diff --git a/config/routes/admin.rb b/config/routes/admin.rb index 931015e302..5b4a5b3479 100644 --- a/config/routes/admin.rb +++ b/config/routes/admin.rb @@ -1,7 +1,7 @@ Openfoodnetwork::Application.routes.draw do namespace :admin do - authenticated :spree_user, -> user { user.superadmin? } do + authenticated :spree_user, -> user { user.admin? } do mount DelayedJobWeb, at: '/delayed_job' end diff --git a/spec/models/spree/user_spec.rb b/spec/models/spree/user_spec.rb index fc89796e3c..abd34364c3 100644 --- a/spec/models/spree/user_spec.rb +++ b/spec/models/spree/user_spec.rb @@ -167,31 +167,16 @@ describe Spree.user_class do end end - describe '#superadmin?' do - let(:user) { create(:user) } - - context 'when the user has an admin spree role' do - before { user.spree_roles << Spree::Role.create(name: 'admin') } - - it 'returns true' do - expect(user.superadmin?).to eq(true) - end + describe '#admin?' do + it 'returns true when the user has an admin spree role' do + expect(create(:admin_user).admin?).to be_truthy end - context 'when the user does not have an admin spree role' do - it 'returns false' do - expect(user.superadmin?).to eq(false) - end + it 'returns false when the user does not have an admin spree role' do + expect(create(:user).admin?).to eq(false) end end - before(:all) { Spree::Role.create name: 'admin' } - - it '#admin?' do - expect(create(:admin_user).admin?).to be_truthy - expect(create(:user).admin?).to be_falsey - end - context '#destroy' do it 'can not delete if it has completed orders' do order = build(:order, completed_at: Time.zone.now)