Introduce a new Keys::DestroyService service

Signed-off-by: Rémy Coutable <remy@rymai.me>
This commit is contained in:
Rémy Coutable 2018-05-16 20:26:22 +02:00
parent c75ae23729
commit 6d81905faf
No known key found for this signature in database
GPG Key ID: 98DFFD1C0C62B70B
4 changed files with 27 additions and 2 deletions

View File

@ -23,7 +23,7 @@ class Profiles::KeysController < Profiles::ApplicationController
def destroy
@key = current_user.keys.find(params[:id])
@key.destroy
Keys::DestroyService.new(current_user).execute(@key)
respond_to do |format|
format.html { redirect_to profile_keys_url, status: 302 }

View File

@ -2,7 +2,7 @@ module Keys
class BaseService
attr_accessor :user, :params
def initialize(user, params)
def initialize(user, params = {})
@user, @params = user, params
@ip_address = @params.delete(:ip_address)
end

View File

@ -0,0 +1,12 @@
module Keys
class DestroyService < ::Keys::BaseService
def execute(key)
key.destroy if destroy_possible?(key)
end
# overriden in EE::Keys::DestroyService
def destroy_possible?(key)
true
end
end
end

View File

@ -0,0 +1,13 @@
require 'spec_helper'
describe Keys::DestroyService do
let(:user) { create(:user) }
subject { described_class.new(user) }
it 'destroys a key' do
key = create(:key)
expect { subject.execute(key) }.to change(Key, :count).by(-1)
end
end