Fix controller and tests

This commit is contained in:
Urban Rotnik 2021-01-06 15:01:39 +01:00
parent ba13a0b14b
commit 20acd9b693
3 changed files with 36 additions and 8 deletions

View file

@ -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

View file

@ -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

View file

@ -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)