Commit b2d09c24 authored by Arturo Herrero's avatar Arturo Herrero

Remove conditional using optional parameters

If we pass optional parameters instead of a boolean we can remove the
conditional.
parent 40d1ea1c
...@@ -2513,10 +2513,10 @@ class Project < ApplicationRecord ...@@ -2513,10 +2513,10 @@ class Project < ApplicationRecord
def build_from_instance_or_template(name) def build_from_instance_or_template(name)
instance = find_service(services_instances, name) instance = find_service(services_instances, name)
return Service.build_from_integration(id, instance) if instance return Service.build_from_integration(instance, project_id: id) if instance
template = find_service(services_templates, name) template = find_service(services_templates, name)
return Service.build_from_integration(id, template) if template return Service.build_from_integration(template, project_id: id) if template
end end
def services_templates def services_templates
......
...@@ -215,7 +215,7 @@ class Service < ApplicationRecord ...@@ -215,7 +215,7 @@ class Service < ApplicationRecord
services_names.map { |service_name| "#{service_name}_service".camelize } services_names.map { |service_name| "#{service_name}_service".camelize }
end end
def self.build_from_integration(id, integration, belongs_to_project = true) def self.build_from_integration(integration, project_id: nil, group_id: nil)
service = integration.dup service = integration.dup
if integration.supports_data_fields? if integration.supports_data_fields?
...@@ -223,16 +223,10 @@ class Service < ApplicationRecord ...@@ -223,16 +223,10 @@ class Service < ApplicationRecord
data_fields.service = service data_fields.service = service
end end
if belongs_to_project
service.group = nil
service.project_id = id
else
service.project = nil
service.group_id = id
end
service.template = false service.template = false
service.instance = false service.instance = false
service.project_id = project_id
service.group_id = group_id
service.inherit_from_id = integration.id if integration.instance? || integration.group service.inherit_from_id = integration.id if integration.instance? || integration.group
service.active = false if service.invalid? service.active = false if service.invalid?
service service
......
...@@ -95,7 +95,7 @@ module Groups ...@@ -95,7 +95,7 @@ module Groups
Service.active.where(instance: true), Service.active.where(instance: true),
Service.active.where(group_id: group_ids) Service.active.where(group_id: group_ids)
]).order(by_type_group_ids_and_instance(group_ids)).group_by(&:type).each do |type, records| ]).order(by_type_group_ids_and_instance(group_ids)).group_by(&:type).each do |type, records|
Service.build_from_integration(group.id, records.first, false).save! Service.build_from_integration(records.first, group_id: group.id).save!
end end
end end
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
......
...@@ -237,7 +237,7 @@ module Projects ...@@ -237,7 +237,7 @@ module Projects
Service.active.where(template: true), Service.active.where(template: true),
Service.active.where(group_id: group_ids) Service.active.where(group_id: group_ids)
]).order(by_type_group_ids_and_instance(group_ids)).group_by(&:type).each do |type, records| ]).order(by_type_group_ids_and_instance(group_ids)).group_by(&:type).each do |type, records|
Service.build_from_integration(project.id, records.first).save! Service.build_from_integration(records.first, project_id: project.id).save!
end end
end end
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
......
...@@ -320,7 +320,7 @@ RSpec.describe Service do ...@@ -320,7 +320,7 @@ RSpec.describe Service do
end end
it 'sets service to inactive' do it 'sets service to inactive' do
service = described_class.build_from_integration(project.id, integration) service = described_class.build_from_integration(integration, project_id: project.id)
expect(service).to be_valid expect(service).to be_valid
expect(service.active).to be false expect(service.active).to be false
...@@ -331,7 +331,7 @@ RSpec.describe Service do ...@@ -331,7 +331,7 @@ RSpec.describe Service do
let(:integration) { create(:jira_service, :instance) } let(:integration) { create(:jira_service, :instance) }
it 'sets inherit_from_id from integration' do it 'sets inherit_from_id from integration' do
service = described_class.build_from_integration(project.id, integration) service = described_class.build_from_integration(integration, project_id: project.id)
expect(service.inherit_from_id).to eq(integration.id) expect(service.inherit_from_id).to eq(integration.id)
end end
...@@ -341,7 +341,7 @@ RSpec.describe Service do ...@@ -341,7 +341,7 @@ RSpec.describe Service do
let(:integration) { create(:jira_service, group: group, project: nil) } let(:integration) { create(:jira_service, group: group, project: nil) }
it 'sets inherit_from_id from integration' do it 'sets inherit_from_id from integration' do
service = described_class.build_from_integration(project.id, integration) service = described_class.build_from_integration(integration, project_id: project.id)
expect(service.inherit_from_id).to eq(integration.id) expect(service.inherit_from_id).to eq(integration.id)
end end
...@@ -361,7 +361,7 @@ RSpec.describe Service do ...@@ -361,7 +361,7 @@ RSpec.describe Service do
shared_examples 'service creation from an integration' do shared_examples 'service creation from an integration' do
it 'creates a correct service for a project integration' do it 'creates a correct service for a project integration' do
service = described_class.build_from_integration(project.id, integration) service = described_class.build_from_integration(integration, project_id: project.id)
expect(service).to be_active expect(service).to be_active
expect(service.url).to eq(url) expect(service.url).to eq(url)
...@@ -375,7 +375,7 @@ RSpec.describe Service do ...@@ -375,7 +375,7 @@ RSpec.describe Service do
end end
it 'creates a correct service for a group integration' do it 'creates a correct service for a group integration' do
service = described_class.build_from_integration(group.id, integration, false) service = described_class.build_from_integration(integration, group_id: group.id)
expect(service).to be_active expect(service).to be_active
expect(service.url).to eq(url) expect(service.url).to eq(url)
......
...@@ -61,8 +61,8 @@ RSpec.describe Admin::PropagateServiceTemplate do ...@@ -61,8 +61,8 @@ RSpec.describe Admin::PropagateServiceTemplate do
} }
) )
Service.build_from_integration(project.id, service_template).save! Service.build_from_integration(service_template, project_id: project.id).save!
Service.build_from_integration(project.id, other_service).save! Service.build_from_integration(other_service, project_id: project.id).save!
expect { described_class.propagate(service_template) } expect { described_class.propagate(service_template) }
.not_to change { Service.count } .not_to change { Service.count }
......
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