Commit cf002738 authored by James Lopez's avatar James Lopez

refactor a few things based on feedback

parent 78d05914
...@@ -15,7 +15,9 @@ class Admin::ServicesController < Admin::ApplicationController ...@@ -15,7 +15,9 @@ class Admin::ServicesController < Admin::ApplicationController
end end
def update def update
if service.update_and_propagate(service_params[:service]) if service.update_attributes(service_params[:service])
PropagateProjectServiceWorker.perform_async(service.id) if service.active?
redirect_to admin_application_settings_services_path, redirect_to admin_application_settings_services_path,
notice: 'Application settings saved successfully' notice: 'Application settings saved successfully'
else else
......
...@@ -254,16 +254,6 @@ class Service < ActiveRecord::Base ...@@ -254,16 +254,6 @@ class Service < ActiveRecord::Base
service service
end end
def update_and_propagate(service_params)
return false unless update_attributes(service_params)
if service_params[:active]
PropagateProjectServiceWorker.perform_async(id)
end
true
end
private private
def cache_project_has_external_issue_tracker def cache_project_has_external_issue_tracker
......
...@@ -2,15 +2,15 @@ module Projects ...@@ -2,15 +2,15 @@ module Projects
class PropagateService class PropagateService
BATCH_SIZE = 100 BATCH_SIZE = 100
def self.propagate!(*args) def self.propagate(*args)
new(*args).propagate! new(*args).propagate
end end
def initialize(template) def initialize(template)
@template = template @template = template
end end
def propagate! def propagate
return unless @template&.active return unless @template&.active
Rails.logger.info("Propagating services for template #{@template.id}") Rails.logger.info("Propagating services for template #{@template.id}")
...@@ -28,7 +28,7 @@ module Projects ...@@ -28,7 +28,7 @@ module Projects
batch.each { |project_id| create_from_template(project_id) } batch.each { |project_id| create_from_template(project_id) }
break if batch.count < BATCH_SIZE break if batch.size < BATCH_SIZE
offset += BATCH_SIZE offset += BATCH_SIZE
end end
......
...@@ -10,7 +10,7 @@ class PropagateProjectServiceWorker ...@@ -10,7 +10,7 @@ class PropagateProjectServiceWorker
def perform(template_id) def perform(template_id)
return unless try_obtain_lease_for(template_id) return unless try_obtain_lease_for(template_id)
Projects::PropagateService.propagate!(Service.find_by(id: template_id)) Projects::PropagateService.propagate(Service.find_by(id: template_id))
end end
private private
......
...@@ -23,4 +23,36 @@ describe Admin::ServicesController do ...@@ -23,4 +23,36 @@ describe Admin::ServicesController do
end end
end end
end end
describe "#update" do
let(:project) { create(:empty_project) }
let!(:service) do
RedmineService.create(
project: project,
active: false,
template: true,
properties: {
project_url: 'http://abc',
issues_url: 'http://abc',
new_issue_url: 'http://abc'
}
)
end
it 'updates the service params successfully and calls the propagation worker' do
expect(PropagateProjectServiceWorker).to receive(:perform_async).with(service.id)
put :update, id: service.id, service: { active: true }
expect(response).to have_http_status(302)
end
it 'updates the service params successfully' do
expect(PropagateProjectServiceWorker).not_to receive(:perform_async)
put :update, id: service.id, service: { properties: {} }
expect(response).to have_http_status(302)
end
end
end end
...@@ -254,31 +254,4 @@ describe Service, models: true do ...@@ -254,31 +254,4 @@ describe Service, models: true do
end end
end end
end end
describe "#update_and_propagate" do
let(:project) { create(:empty_project) }
let!(:service) do
RedmineService.create(
project: project,
active: false,
properties: {
project_url: 'http://abc',
issues_url: 'http://abc',
new_issue_url: 'http://abc'
}
)
end
it 'updates the service params successfully and calls the propagation worker' do
expect(PropagateProjectServiceWorker).to receive(:perform_async).with(service.id)
expect(service.update_and_propagate(active: true)).to be true
end
it 'updates the service params successfully' do
expect(PropagateProjectServiceWorker).not_to receive(:perform_async)
expect(service.update_and_propagate(properties: {})).to be true
end
end
end end
...@@ -18,7 +18,7 @@ describe Projects::PropagateService, services: true do ...@@ -18,7 +18,7 @@ describe Projects::PropagateService, services: true do
let!(:project) { create(:empty_project) } let!(:project) { create(:empty_project) }
it 'creates services for projects' do it 'creates services for projects' do
expect { described_class.propagate!(service_template) }. expect { described_class.propagate(service_template) }.
to change { Service.count }.by(1) to change { Service.count }.by(1)
end end
...@@ -36,7 +36,7 @@ describe Projects::PropagateService, services: true do ...@@ -36,7 +36,7 @@ describe Projects::PropagateService, services: true do
Service.build_from_template(project.id, other_service).save! Service.build_from_template(project.id, other_service).save!
expect { described_class.propagate!(service_template) }. expect { described_class.propagate(service_template) }.
to change { Service.count }.by(1) to change { Service.count }.by(1)
end end
...@@ -55,12 +55,12 @@ describe Projects::PropagateService, services: true do ...@@ -55,12 +55,12 @@ describe Projects::PropagateService, services: true do
Service.build_from_template(project.id, service_template).save! Service.build_from_template(project.id, service_template).save!
Service.build_from_template(project.id, other_service).save! Service.build_from_template(project.id, other_service).save!
expect { described_class.propagate!(service_template) }. expect { described_class.propagate(service_template) }.
not_to change { Service.count } not_to change { Service.count }
end end
it 'creates the service containing the template attributes' do it 'creates the service containing the template attributes' do
described_class.propagate!(service_template) described_class.propagate(service_template)
service = Service.find_by(type: service_template.type, template: false) service = Service.find_by(type: service_template.type, template: false)
......
...@@ -21,7 +21,7 @@ describe PropagateProjectServiceWorker do ...@@ -21,7 +21,7 @@ describe PropagateProjectServiceWorker do
describe '#perform' do describe '#perform' do
it 'calls the propagate service with the template' do it 'calls the propagate service with the template' do
expect(Projects::PropagateService).to receive(:propagate!).with(service_template) expect(Projects::PropagateService).to receive(:propagate).with(service_template)
subject.perform(service_template.id) subject.perform(service_template.id)
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