Reuse #superadmin? in Delayed Job Web route

This moves #superadmin? to the user decorator so it can be reused
outside FeatureFlags.
This commit is contained in:
Pau Perez
2019-04-04 15:51:37 +02:00
parent bf20376667
commit aa3f0ac577
5 changed files with 31 additions and 13 deletions

View File

@@ -11,25 +11,17 @@ class FeatureFlags
#
# @return [Boolean]
def product_import_enabled?
superadmin?
user.superadmin?
end
# Checks whether the "Enterprise Fee Summary" is enabled for the specified user
#
# @return [Boolean]
def enterprise_fee_summary_enabled?
superadmin?
user.superadmin?
end
private
attr_reader :user
# Checks whether the specified user is a superadmin, with full control of the
# instance
#
# @return [Boolean]
def superadmin?
user.has_spree_role?('admin')
end
end

View File

@@ -74,6 +74,14 @@ Spree.user_class.class_eval do
credit_cards.where(is_default: true).first
end
# Checks whether the specified user is a superadmin, with full control of the
# instance
#
# @return [Boolean]
def superadmin?
has_spree_role?('admin')
end
private
def limit_owned_enterprises

View File

@@ -1,7 +1,7 @@
Openfoodnetwork::Application.routes.draw do
namespace :admin do
authenticated :spree_user, -> user { user.has_spree_role?('admin') } do
authenticated :spree_user, -> user { user.superadmin? } do
mount DelayedJobWeb, at: '/delayed_job'
end

View File

@@ -7,7 +7,7 @@ describe FeatureFlags do
describe '#product_import_enabled?' do
context 'when the user is superadmin' do
before do
allow(user).to receive(:has_spree_role?).with('admin') { true }
allow(user).to receive(:superadmin?) { true }
end
it 'returns true' do
@@ -17,7 +17,7 @@ describe FeatureFlags do
context 'when the user is not superadmin' do
before do
allow(user).to receive(:has_spree_role?).with('admin') { false }
allow(user).to receive(:superadmin?) { false }
end
it 'returns false' do

View File

@@ -162,4 +162,22 @@ describe Spree.user_class do
end
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
end
context 'when the user does not have an admin spree role' do
it 'returns false' do
expect(user.superadmin?).to eq(false)
end
end
end
end