From 20acd9b6931245667b6fdabff08d3f9107198dfb Mon Sep 17 00:00:00 2001 From: Urban Rotnik Date: Wed, 6 Jan 2021 15:01:39 +0100 Subject: [PATCH] Fix controller and tests --- app/controllers/project_folders_controller.rb | 28 +++++++++++++++---- app/models/project_folder.rb | 7 +++++ .../project_folders_controller_spec.rb | 9 ++++-- 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/app/controllers/project_folders_controller.rb b/app/controllers/project_folders_controller.rb index e200fc78e..4cd9ad474 100644 --- a/app/controllers/project_folders_controller.rb +++ b/app/controllers/project_folders_controller.rb @@ -2,6 +2,7 @@ class ProjectFoldersController < ApplicationController include InputSanitizeHelper + include ProjectFoldersHelper before_action :load_current_folder, only: %i(new) before_action :load_project_folder, only: %i(edit update) @@ -40,8 +41,14 @@ class ProjectFoldersController < ApplicationController end def move_to - destination_folder = current_team.project_folders.find(move_params[:id]) - destination_folder.transaction do + destination_folder = + if move_params[:destination_folder_id] == 'root_folder' + nil + else + current_team.project_folders.find(move_params[:destination_folder_id]) + end + + ActiveRecord::Base.transaction do move_projects(destination_folder) move_folders(destination_folder) end @@ -56,6 +63,13 @@ class ProjectFoldersController < ApplicationController end end + def move_to_modal + render json: { + html: render_to_string(partial: 'projects/index/modals/move_to_modal_contents.html.erb', + locals: { items_label: items_label(params[:items]) }) + } + end + def edit render json: { html: render_to_string(partial: 'projects/index/modals/edit_folder_contents.html.erb', @@ -99,9 +113,13 @@ class ProjectFoldersController < ApplicationController end def move_params - params.require(:id) - params.require(:movables) - params.permit(:id, movables: %i(type id)) + parsed_params = ActionController::Parameters.new( + movables: JSON.parse(params[:movables]), + destination_folder_id: params[:destination_folder_id] + ) + parsed_params.require(:destination_folder_id) + parsed_params.require(:movables) + parsed_params.permit(:destination_folder_id, movables: %i(id type)) end def check_create_permissions diff --git a/app/models/project_folder.rb b/app/models/project_folder.rb index 3aeacaedc..2a39149c7 100644 --- a/app/models/project_folder.rb +++ b/app/models/project_folder.rb @@ -10,6 +10,7 @@ class ProjectFolder < ApplicationRecord maximum: Constants::NAME_MAX_LENGTH }, uniqueness: { scope: :team_id, case_sensitive: false } validate :parent_folder_team, if: -> { parent_folder.present? } + validate :parent_folder_validation, if: -> { parent_folder.present? } before_validation :inherit_team_from_parent_folder, on: :create, if: -> { parent_folder.present? } @@ -99,6 +100,12 @@ class ProjectFolder < ApplicationRecord def parent_folder_team return if parent_folder.team_id == team_id + errors.add(:parent_folder, I18n.t('activerecord.errors.models.project_folder.attributes.parent_folder_team')) + end + + def parent_folder_validation + return if parent_folder.id != id + errors.add(:parent_folder, I18n.t('activerecord.errors.models.project_folder.attributes.parent_folder')) end end diff --git a/spec/controllers/project_folders_controller_spec.rb b/spec/controllers/project_folders_controller_spec.rb index 32373319b..d2b213460 100644 --- a/spec/controllers/project_folders_controller_spec.rb +++ b/spec/controllers/project_folders_controller_spec.rb @@ -35,11 +35,11 @@ describe ProjectFoldersController, type: :controller do let(:action) { post :move_to, params: params, format: :json } let(:params) do { - id: project_folder_1.id, + destination_folder_id: project_folder_1.id, movables: [ { id: project_1.id, type: :project }, { id: project_folder_2.id, type: :project_folder } - ] + ].to_json } end @@ -80,7 +80,10 @@ describe ProjectFoldersController, type: :controller do end describe 'PATCH update' do - let(:action) { patch :update, params: { id: project_folder.id }, format: :json } + let(:action) do + patch :update, + params: { project_folder: { name: 'new name' }, id: project_folder.id }, format: :json + end it 'calls create activity for creating project folder' do expect(Activities::CreateActivityService)