Commit a1101b05 authored by Arturo Herrero's avatar Arturo Herrero

Improve method names when finding or initializing services

find_or_initialize_all and find_or_initialize_integration are only used
excluding project specific services, this change helps to understand the
purpose of the methods.
parent c1b384dd
......@@ -53,7 +53,7 @@ class Admin::ApplicationSettingsController < Admin::ApplicationController
def integrations
return not_found unless instance_level_integrations?
@integrations = Service.find_or_initialize_all(Service.for_instance).sort_by(&:title)
@integrations = Service.find_or_initialize_all_non_project_specific(Service.for_instance).sort_by(&:title)
end
def update
......
......@@ -8,8 +8,8 @@ class Admin::IntegrationsController < Admin::ApplicationController
private
def find_or_initialize_integration(name)
Service.find_or_initialize_integration(name, instance: true)
def find_or_initialize_non_project_specific_integration(name)
Service.find_or_initialize_non_project_specific_integration(name, instance: true)
end
def integrations_enabled?
......
......@@ -52,7 +52,7 @@ module IntegrationsActions
def integration
# Using instance variable `@service` still required as it's used in ServiceParams.
# Should be removed once that is refactored to use `@integration`.
@integration = @service ||= find_or_initialize_integration(params[:id]) # rubocop:disable Gitlab/ModuleWithInstanceVariables
@integration = @service ||= find_or_initialize_non_project_specific_integration(params[:id]) # rubocop:disable Gitlab/ModuleWithInstanceVariables
end
def success_message
......
......@@ -10,7 +10,7 @@ module Groups
feature_category :integrations
def index
@integrations = Service.find_or_initialize_all(Service.for_group(group)).sort_by(&:title)
@integrations = Service.find_or_initialize_all_non_project_specific(Service.for_group(group)).sort_by(&:title)
end
def edit
......@@ -21,8 +21,8 @@ module Groups
private
def find_or_initialize_integration(name)
Service.find_or_initialize_integration(name, group_id: group.id)
def find_or_initialize_non_project_specific_integration(name)
Service.find_or_initialize_non_project_specific_integration(name, group_id: group.id)
end
def integrations_enabled?
......
......@@ -169,13 +169,13 @@ class Service < ApplicationRecord
end
private_class_method :create_nonexistent_templates
def self.find_or_initialize_integration(name, instance: false, group_id: nil)
def self.find_or_initialize_non_project_specific_integration(name, instance: false, group_id: nil)
if name.in?(available_services_names(include_project_specific: false))
"#{name}_service".camelize.constantize.find_or_initialize_by(instance: instance, group_id: group_id)
end
end
def self.find_or_initialize_all(scope)
def self.find_or_initialize_all_non_project_specific(scope)
scope + build_nonexistent_services_for(scope)
end
......
......@@ -208,27 +208,27 @@ RSpec.describe Service do
end
end
describe '.find_or_initialize_integration' do
describe '.find_or_initialize_non_project_specific_integration' do
let!(:service1) { create(:jira_service, project_id: nil, group_id: group.id) }
let!(:service2) { create(:jira_service) }
it 'returns the right service' do
expect(Service.find_or_initialize_integration('jira', group_id: group)).to eq(service1)
expect(Service.find_or_initialize_non_project_specific_integration('jira', group_id: group)).to eq(service1)
end
it 'does not create a new service' do
expect { Service.find_or_initialize_integration('redmine', group_id: group) }.not_to change { Service.count }
expect { Service.find_or_initialize_non_project_specific_integration('redmine', group_id: group) }.not_to change { Service.count }
end
end
describe '.find_or_initialize_all' do
describe '.find_or_initialize_all_non_project_specific' do
shared_examples 'service instances' do
it 'returns the available service instances' do
expect(Service.find_or_initialize_all(Service.for_instance).pluck(:type)).to match_array(Service.available_services_types(include_project_specific: false))
expect(Service.find_or_initialize_all_non_project_specific(Service.for_instance).pluck(:type)).to match_array(Service.available_services_types(include_project_specific: false))
end
it 'does not create service instances' do
expect { Service.find_or_initialize_all(Service.for_instance) }.not_to change { Service.count }
expect { Service.find_or_initialize_all_non_project_specific(Service.for_instance) }.not_to change { Service.count }
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment