From ba3c73439a1abcb226ec56a4d7c057103d95fd01 Mon Sep 17 00:00:00 2001 From: Anton Date: Fri, 22 Apr 2022 13:07:51 +0200 Subject: [PATCH] Add step creation/deletion functionality [SCI-6754] --- app/controllers/steps_controller.rb | 34 +++++------ app/javascript/packs/vue/protocol.js | 5 +- app/javascript/vue/protocol/container.vue | 71 +++++++++++++++++++++-- app/javascript/vue/protocol/step.vue | 37 ++++++++++++ app/models/protocol.rb | 30 ++++++++++ app/models/step.rb | 3 + app/serializers/step_serializer.rb | 11 ++++ app/views/my_modules/protocols.html.erb | 4 ++ config/locales/en.yml | 1 + config/routes.rb | 2 +- 10 files changed, 174 insertions(+), 24 deletions(-) create mode 100644 app/javascript/vue/protocol/step.vue create mode 100644 app/serializers/step_serializer.rb diff --git a/app/controllers/steps_controller.rb b/app/controllers/steps_controller.rb index a0b6fe9db..12af835c6 100644 --- a/app/controllers/steps_controller.rb +++ b/app/controllers/steps_controller.rb @@ -6,15 +6,19 @@ class StepsController < ApplicationController before_action :load_vars, only: %i(edit update destroy show toggle_step_state checklistitem_state update_view_state move_up move_down update_asset_view_mode) - before_action :load_vars_nested, only: %i(new create) + before_action :load_vars_nested, only: %i(new create index) before_action :convert_table_contents_to_utf8, only: %i(create update) - before_action :check_view_permissions, only: :show + before_action :check_view_permissions, only: %i(show index) before_action :check_create_permissions, only: %i(new create) before_action :check_manage_permissions, only: %i(edit update destroy move_up move_down update_view_state update_asset_view_mode) before_action :check_complete_and_checkbox_permissions, only: %i(toggle_step_state checklistitem_state) + def index + render json: @protocol.steps.in_order, each_serializer: StepSerializer + end + def new @step = Step.new @@ -28,6 +32,16 @@ class StepsController < ApplicationController end def create + new_step = Step.new( + name: t('protocols.steps.default_name'), + completed: false, + user: current_user, + last_modified_by: current_user + ) + render json: @protocol.insert_step(new_step, params[:position]), serializer: StepSerializer + end + + def create_old @step = Step.new @step.transaction do new_step_params = step_params @@ -264,23 +278,9 @@ class StepsController < ApplicationController # Release space taken by the step team.release_space(previous_size) team.save - - flash[:success] = t( - 'protocols.steps.destroy.success_flash', - step: (@step.position_plus_one).to_s - ) - else - flash[:error] = t( - 'protocols.steps.destroy.error_flash', - step: (@step.position_plus_one).to_s - ) end - if @protocol.in_module? - redirect_to protocols_my_module_path(@step.my_module) - else - redirect_to edit_protocol_path(@protocol) - end + render json: @step, serializer: StepSerializer end # Responds to checkbox toggling in steps view diff --git a/app/javascript/packs/vue/protocol.js b/app/javascript/packs/vue/protocol.js index 36fc3b7ab..77b330ec7 100644 --- a/app/javascript/packs/vue/protocol.js +++ b/app/javascript/packs/vue/protocol.js @@ -9,7 +9,6 @@ Vue.prototype.i18n = window.I18n; window.initProtocolComponent = () => { Vue.prototype.dateFormat = $('#protocolContainer').data('date-format'); - new Vue({ el: '#protocolContainer', components: { @@ -17,7 +16,9 @@ window.initProtocolComponent = () => { }, data() { return { - protocolUrl: $('#protocolContainer').data('protocol-url') + protocolUrl: $('#protocolContainer').data('protocol-url'), + stepsUrl: $('#protocolContainer').data('steps-url'), + addStepUrl: $('#protocolContainer').data('add-step-url') }; } }); diff --git a/app/javascript/vue/protocol/container.vue b/app/javascript/vue/protocol/container.vue index e94844856..2990b4a96 100644 --- a/app/javascript/vue/protocol/container.vue +++ b/app/javascript/vue/protocol/container.vue @@ -13,28 +13,91 @@
- Steps go here +
+ diff --git a/app/javascript/vue/protocol/step.vue b/app/javascript/vue/protocol/step.vue new file mode 100644 index 000000000..cec048f88 --- /dev/null +++ b/app/javascript/vue/protocol/step.vue @@ -0,0 +1,37 @@ + + + diff --git a/app/models/protocol.rb b/app/models/protocol.rb index 3e7d4905e..47abb6883 100644 --- a/app/models/protocol.rb +++ b/app/models/protocol.rb @@ -202,6 +202,36 @@ class Protocol < ApplicationRecord user_id: user.id)) end + def insert_step(step, position) + ActiveRecord::Base.transaction do + steps.where('position >= ?', position).desc_order.each do |s| + s.update!(position: s.position + 1) + end + step.position = position + step.protocol = self + step.save! + end + step + end + + def move_step(step, new_position) + ActiveRecord::Base.transaction do + old_position = step.position + step.update!(posistion: -1) + if old_position > new_position + steps.where('position > ? AND position <= ?', old_position, new_position).desc_order.each do |s| + s.update!(position: s.position + 1) + end + else + steps.where('position >= ? AND position < ?', old_position, new_position).at_order.each do |s| + s.update!(position: s.position - 1) + end + end + step.update!(posistion: new_position) + end + step + end + def linked_modules MyModule.joins(:protocols).where('protocols.parent_id = ?', id) end diff --git a/app/models/step.rb b/app/models/step.rb index 2da8c1184..27dc45c12 100644 --- a/app/models/step.rb +++ b/app/models/step.rb @@ -45,6 +45,9 @@ class Step < ApplicationRecord }, allow_destroy: true + scope :in_order, -> { order(position: :asc) } + scope :desc_order, -> { order(position: :desc) } + def self.search(user, include_archived, query = nil, diff --git a/app/serializers/step_serializer.rb b/app/serializers/step_serializer.rb new file mode 100644 index 000000000..042ce7d97 --- /dev/null +++ b/app/serializers/step_serializer.rb @@ -0,0 +1,11 @@ +class StepSerializer < ActiveModel::Serializer + include Rails.application.routes.url_helpers + + attributes :name, :position, :completed, :urls + + def urls + { + delete_url: step_path(object) + } + end +end diff --git a/app/views/my_modules/protocols.html.erb b/app/views/my_modules/protocols.html.erb index d9d97960e..d623466ac 100644 --- a/app/views/my_modules/protocols.html.erb +++ b/app/views/my_modules/protocols.html.erb @@ -121,11 +121,15 @@
diff --git a/config/locales/en.yml b/config/locales/en.yml index dde61f8c3..cfd77a959 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -2491,6 +2491,7 @@ en: success_flash: 'Protocol %{name} successfully imported to %{type}.' steps: + default_name: 'Untitled step' completed: 'Completed' uncompleted: 'Uncompleted' expand_label: "Expand All" diff --git a/config/routes.rb b/config/routes.rb index 9a868e3c2..34f7e4f1d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -443,7 +443,7 @@ Rails.application.routes.draw do get 'users/edit', to: 'user_my_modules#index_edit' end - resources :steps, only: [:edit, :update, :destroy, :show] do + resources :steps, only: %i(index edit update destroy show) do resources :step_comments, path: '/comments', only: %i(create index update destroy)