Commit cb38e99d authored by Arturo Herrero's avatar Arturo Herrero Committed by Kamil Trzciński

Rename service model scopes

parent e6184680
......@@ -32,7 +32,7 @@ class Admin::ApplicationSettingsController < Admin::ApplicationController
end
def integrations
@integrations = Service.find_or_initialize_all(Service.instances).sort_by(&:title)
@integrations = Service.find_or_initialize_all(Service.for_instance).sort_by(&:title)
end
def update
......
......@@ -8,7 +8,7 @@ class Admin::ServicesController < Admin::ApplicationController
def index
@services = Service.find_or_create_templates.sort_by(&:title)
@existing_instance_types = Service.instances.pluck(:type) # rubocop: disable CodeReuse/ActiveRecord
@existing_instance_types = Service.for_instance.pluck(:type) # rubocop: disable CodeReuse/ActiveRecord
end
def edit
......
......@@ -8,7 +8,7 @@ module Groups
before_action :authorize_admin_group!
def index
@integrations = Service.find_or_initialize_all(Service.by_group(group)).sort_by(&:title)
@integrations = Service.find_or_initialize_all(Service.for_group(group)).sort_by(&:title)
end
private
......
......@@ -2533,11 +2533,11 @@ class Project < ApplicationRecord
end
def services_templates
@services_templates ||= Service.templates
@services_templates ||= Service.for_template
end
def services_instances
@services_instances ||= Service.instances
@services_instances ||= Service.for_instance
end
def closest_namespace_setting(name)
......
......@@ -63,9 +63,9 @@ class Service < ApplicationRecord
scope :active, -> { where(active: true) }
scope :by_type, -> (type) { where(type: type) }
scope :by_active_flag, -> (flag) { where(active: flag) }
scope :by_group, -> (group) { where(group_id: group, type: available_services_types) }
scope :templates, -> { where(template: true, type: available_services_types) }
scope :instances, -> { where(instance: true, type: available_services_types) }
scope :for_group, -> (group) { where(group_id: group, type: available_services_types) }
scope :for_template, -> { where(template: true, type: available_services_types) }
scope :for_instance, -> { where(instance: true, type: available_services_types) }
scope :push_hooks, -> { where(push_events: true, active: true) }
scope :tag_push_hooks, -> { where(tag_push_events: true, active: true) }
......@@ -291,11 +291,11 @@ class Service < ApplicationRecord
def self.find_or_create_templates
create_nonexistent_templates
templates
for_template
end
private_class_method def self.create_nonexistent_templates
nonexistent_services = list_nonexistent_services_for(templates)
nonexistent_services = list_nonexistent_services_for(for_template)
return if nonexistent_services.empty?
# Create within a transaction to perform the lowest possible SQL queries.
......
......@@ -4,7 +4,7 @@ require 'spec_helper'
RSpec.describe 'Admin visits service templates' do
let(:admin) { create(:user, :admin) }
let(:slack_service) { Service.templates.find { |s| s.type == 'SlackService' } }
let(:slack_service) { Service.for_template.find { |s| s.type == 'SlackService' } }
before do
sign_in(admin)
......
......@@ -92,12 +92,12 @@ RSpec.describe Service do
end
end
describe '.by_group' do
describe '.for_group' do
let!(:service1) { create(:jira_service, project_id: nil, group_id: group.id) }
let!(:service2) { create(:jira_service) }
it 'returns the right group service' do
expect(described_class.by_group(group)).to match_array([service1])
expect(described_class.for_group(group)).to match_array([service1])
end
end
......@@ -215,11 +215,11 @@ RSpec.describe Service do
describe '.find_or_initialize_all' do
shared_examples 'service instances' do
it 'returns the available service instances' do
expect(Service.find_or_initialize_all(Service.instances).pluck(:type)).to match_array(Service.available_services_types)
expect(Service.find_or_initialize_all(Service.for_instance).pluck(:type)).to match_array(Service.available_services_types)
end
it 'does not create service instances' do
expect { Service.find_or_initialize_all(Service.instances) }.not_to change { Service.count }
expect { Service.find_or_initialize_all(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