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 ...@@ -53,7 +53,7 @@ class Admin::ApplicationSettingsController < Admin::ApplicationController
def integrations def integrations
return not_found unless instance_level_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 end
def update def update
......
...@@ -8,8 +8,8 @@ class Admin::IntegrationsController < Admin::ApplicationController ...@@ -8,8 +8,8 @@ class Admin::IntegrationsController < Admin::ApplicationController
private private
def find_or_initialize_integration(name) def find_or_initialize_non_project_specific_integration(name)
Service.find_or_initialize_integration(name, instance: true) Service.find_or_initialize_non_project_specific_integration(name, instance: true)
end end
def integrations_enabled? def integrations_enabled?
......
...@@ -52,7 +52,7 @@ module IntegrationsActions ...@@ -52,7 +52,7 @@ module IntegrationsActions
def integration def integration
# Using instance variable `@service` still required as it's used in ServiceParams. # Using instance variable `@service` still required as it's used in ServiceParams.
# Should be removed once that is refactored to use `@integration`. # 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 end
def success_message def success_message
......
...@@ -10,7 +10,7 @@ module Groups ...@@ -10,7 +10,7 @@ module Groups
feature_category :integrations feature_category :integrations
def index 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 end
def edit def edit
...@@ -21,8 +21,8 @@ module Groups ...@@ -21,8 +21,8 @@ module Groups
private private
def find_or_initialize_integration(name) def find_or_initialize_non_project_specific_integration(name)
Service.find_or_initialize_integration(name, group_id: group.id) Service.find_or_initialize_non_project_specific_integration(name, group_id: group.id)
end end
def integrations_enabled? def integrations_enabled?
......
...@@ -169,13 +169,13 @@ class Service < ApplicationRecord ...@@ -169,13 +169,13 @@ class Service < ApplicationRecord
end end
private_class_method :create_nonexistent_templates 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)) if name.in?(available_services_names(include_project_specific: false))
"#{name}_service".camelize.constantize.find_or_initialize_by(instance: instance, group_id: group_id) "#{name}_service".camelize.constantize.find_or_initialize_by(instance: instance, group_id: group_id)
end end
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) scope + build_nonexistent_services_for(scope)
end end
......
...@@ -208,27 +208,27 @@ RSpec.describe Service do ...@@ -208,27 +208,27 @@ RSpec.describe Service do
end end
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!(:service1) { create(:jira_service, project_id: nil, group_id: group.id) }
let!(:service2) { create(:jira_service) } let!(:service2) { create(:jira_service) }
it 'returns the right service' do 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 end
it 'does not create a new service' do 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
end end
describe '.find_or_initialize_all' do describe '.find_or_initialize_all_non_project_specific' do
shared_examples 'service instances' do shared_examples 'service instances' do
it 'returns the available 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 end
it 'does not create service instances' do 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
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