Merge pull request #2842 from okriuchykhin/ok_SCI_5004

Add Completion and Uncompletion consequences [SCI-5004]
This commit is contained in:
Alex Kriuchykhin 2020-09-15 12:56:27 +02:00 committed by GitHub
commit 04686d4ffc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 34 additions and 99 deletions

View file

@ -24,38 +24,6 @@
});
}
// Complete mymodule
function complete_my_module_actions() {
var modal = $('#completed-task-modal');
modal.find('[data-action="complete"]')
.off().on().click(function(event) {
event.stopPropagation();
event.preventDefault();
event.stopImmediatePropagation();
$.ajax({
url: modal.data('url'),
type: 'GET',
success: function(data) {
var task_button = $("[data-action='complete-task']");
task_button.attr('data-action', 'uncomplete-task');
task_button.find('.btn')
.removeClass('btn-toggle').addClass('btn-default');
$('.task-due-date').html(data.module_header_due_date);
$('.task-state-label').html(data.module_state_label);
task_button
.find('button')
.html('<span class="fas fa-times"></span>&nbsp;' +
data.task_button_title);
modal.modal('hide');
},
error: function() {
modal.modal('hide');
}
});
});
}
// Sets callback for completing/uncompleting step
function applyStepCompletedCallBack() {
// First, remove old event handlers, as we use turbolinks
@ -79,11 +47,6 @@
button.attr("data-action", "uncomplete-step");
button.find(".btn").removeClass("btn-toggle").addClass("btn-default");
button.find("button").html('<span class="fas fa-times"></span>&nbsp;' + data.new_title);
if (data.task_ready_to_complete) {
$('#completed-task-modal').modal('show');
complete_my_module_actions();
}
}
else {
step.addClass("not-completed").removeClass("completed");

View file

@ -60,13 +60,13 @@ module Api
def task_params_create
raise TypeError unless params.require(:data).require(:type) == 'tasks'
params.require(:data).require(:attributes).permit(%i(name x y description state))
params.require(:data).require(:attributes).permit(%i(name x y description))
end
def task_params_update
raise TypeError unless params.require(:data).require(:type) == 'tasks'
params.require(:data).require(:attributes).permit(%i(name x y description state my_module_status_id))
params.require(:data).require(:attributes).permit(%i(name x y description my_module_status_id))
end
def load_task_for_managing

View file

@ -321,10 +321,6 @@ class StepsController < ApplicationController
@step.completed = completed
if @step.save
if @protocol.in_module?
ready_to_complete = @protocol.my_module.check_completness_status
end
# Create activity
if changed
completed_steps = @protocol.steps.where(completed: true).count
@ -350,14 +346,7 @@ class StepsController < ApplicationController
t('protocols.steps.options.uncomplete_title')
end
format.json do
if ready_to_complete && @protocol.my_module.uncompleted?
render json: {
task_ready_to_complete: true,
new_title: localized_title
}, status: :ok
else
render json: { new_title: localized_title }, status: :ok
end
render json: { new_title: localized_title }, status: :ok
end
else
format.json { render json: {}, status: :unprocessable_entity }

View file

@ -9,8 +9,6 @@ class MyModule < ApplicationRecord
before_create :create_blank_protocol
before_create :assign_default_status_flow
before_validation :set_completed, if: :my_module_status_id_changed?
before_validation :set_completed_on, if: :state_changed?
before_save :exec_status_consequences, if: :my_module_status_id_changed?
auto_strip_attributes :name, :description, nullify: false
@ -477,18 +475,6 @@ class MyModule < ApplicationRecord
{ x: 0, y: positions.last[1] + HEIGHT }
end
# Check if my_module is ready to become completed
def check_completness_status
if protocol && protocol.steps.count > 0
completed = true
protocol.steps.find_each do |step|
completed = false unless step.completed
end
return true if completed
end
false
end
def assign_user(user, assigned_by = nil)
user_my_modules.create(
assigned_by: assigned_by || user,
@ -506,22 +492,6 @@ class MyModule < ApplicationRecord
private
def set_completed
return if my_module_status.blank?
if my_module_status.final_status?
self.state = 'completed'
else
self.state = 'uncompleted'
end
end
def set_completed_on
return if completed? && completed_on.present?
self.completed_on = completed? ? DateTime.now : nil
end
def create_blank_protocol
protocols << Protocol.new_blank_for_module(self)
end

View file

@ -0,0 +1,12 @@
# frozen_string_literal: true
# Just an example, to be replaced with an actual implementation
module MyModuleStatusConsequences
class Completion < MyModuleStatusConsequence
def call(my_module)
my_module.state = 'completed'
my_module.completed_on = DateTime.now
my_module.save!
end
end
end

View file

@ -0,0 +1,14 @@
# frozen_string_literal: true
# Just an example, to be replaced with an actual implementation
module MyModuleStatusConsequences
class Uncompletion < MyModuleStatusConsequence
def call(my_module)
return unless my_module.state == 'completed'
my_module.state = 'uncompleted'
my_module.completed_on = nil
my_module.save!
end
end
end

View file

@ -327,7 +327,7 @@ class Extends
DEFAULT_FLOW_STATUSES = [
{ name: 'Not started', color: '#406d86' },
{ name: 'In progress', color: '#0065ff' },
{ name: 'Completed', color: '#00b900' }
{ name: 'In progress', color: '#0065ff', consequences: ['MyModuleStatusConsequences::Uncompletion'] },
{ name: 'Completed', color: '#00b900', consequences: ['MyModuleStatusConsequences::Completion'] }
]
end

View file

@ -292,7 +292,7 @@ RSpec.describe 'Api::V1::TasksController', type: :request do
end
end
context 'task completion, when has valid params' do
context 'direct task completion disabled, when has valid params' do
let(:request_body) do
{
data: {
@ -304,23 +304,10 @@ RSpec.describe 'Api::V1::TasksController', type: :request do
}
end
it 'returns status 200' do
it 'returns status 204, no changes to task' do
action
expect(response).to have_http_status 200
end
it 'returns well formated response' do
action
expect(json).to match(
hash_including(
data: hash_including(
type: 'tasks',
attributes: hash_including(state: 'completed')
)
)
)
expect(response).to have_http_status 204
end
end