Add action Settings::RolesController#destroy
This commit is contained in:
parent
9a9eff4f08
commit
ec32b8cf54
4 changed files with 56 additions and 1 deletions
|
@ -1,6 +1,8 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class Settings::RolesController < ApplicationController
|
||||
before_action :set_role, except: :index
|
||||
|
||||
# GET /settings/roles
|
||||
def index
|
||||
authorize %i[settings role]
|
||||
|
@ -10,4 +12,19 @@ class Settings::RolesController < ApplicationController
|
|||
policy_scope_class: Settings::RolePolicy::Scope,
|
||||
)
|
||||
end
|
||||
|
||||
# DELETE /settings/roles/:id
|
||||
def destroy
|
||||
authorize [:settings, @role]
|
||||
|
||||
current_account.remove_role @role.name, @role.resource
|
||||
|
||||
redirect_to settings_roles_url
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_role
|
||||
@role = current_account.roles.find params[:id]
|
||||
end
|
||||
end
|
||||
|
|
|
@ -5,6 +5,10 @@ class Settings::RolePolicy < ApplicationPolicy
|
|||
account && !account.guest?
|
||||
end
|
||||
|
||||
def destroy?
|
||||
account && !account.guest? && account.roles.include?(record)
|
||||
end
|
||||
|
||||
class Scope < Scope
|
||||
def resolve
|
||||
return scope.none if account.nil? || account.guest?
|
||||
|
|
|
@ -37,7 +37,7 @@ Rails.application.routes.draw do
|
|||
namespace :settings do
|
||||
resource :profile, only: %i[edit update]
|
||||
resources :telegram_contacts, only: :index
|
||||
resources :roles, only: :index
|
||||
resources :roles, only: %i[index destroy]
|
||||
end
|
||||
|
||||
######################################
|
||||
|
|
34
spec/requests/settings/roles/destroy_spec.rb
Normal file
34
spec/requests/settings/roles/destroy_spec.rb
Normal file
|
@ -0,0 +1,34 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe 'DELETE /settings/roles/:id' do
|
||||
let!(:role) { current_account.add_role :superuser }
|
||||
|
||||
before do
|
||||
sign_in current_account.user if current_account&.user
|
||||
end
|
||||
|
||||
def make_request
|
||||
delete "/settings/roles/#{role.id}"
|
||||
end
|
||||
|
||||
for_account_types :usual, :superuser do
|
||||
specify do
|
||||
expect { make_request }.to \
|
||||
change { current_account.roles.reload.count }.by(-1)
|
||||
end
|
||||
|
||||
specify do
|
||||
expect { make_request }.not_to(change { AccountRole.count })
|
||||
end
|
||||
|
||||
context 'after request' do
|
||||
before { make_request }
|
||||
|
||||
specify do
|
||||
expect(response).to redirect_to settings_roles_url
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Reference in a new issue