Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

implement editing, creating, deleting, reassignment and moving

Klaus Zanders339cd0086c6f
diff --git a/modules/costs/app/components/admin/time_entry_activities/activity_component.rb b/modules/costs/app/components/admin/time_entry_activities/activity_component.rb
index 71946da846b..d8f3c6839f7 100644
--- a/modules/costs/app/components/admin/time_entry_activities/activity_component.rb
+++ b/modules/costs/app/components/admin/time_entry_activities/activity_component.rb
@@ -71,11 +71,11 @@ module Admin
end
def move_to_top_activity(menu)
- form_inputs = [{ name: "position", value: 1 }]
+ form_inputs = [{ name: "move_to", value: "highest" }]
menu.with_item(label: I18n.t(:label_sort_highest),
tag: :button,
- href: admin_time_entry_activity_move_path(activity),
+ href: move_admin_time_entry_activity_path(activity),
# content_arguments: { data: { turbo_frame: ItemsComponent.wrapper_key } },
form_arguments: { method: :post, inputs: form_inputs }) do |item|
item.with_leading_visual_icon(icon: "move-to-top")
@@ -83,11 +83,11 @@ module Admin
end
def move_up_activity(menu)
- form_inputs = [{ name: "position", value: activity.position - 1 }]
+ form_inputs = [{ name: "move_to", value: "higher" }]
menu.with_item(label: I18n.t(:label_sort_higher),
tag: :button,
- href: admin_time_entry_activity_move_path(activity),
+ href: move_admin_time_entry_activity_path(activity),
# content_arguments: { data: { turbo_frame: ItemsComponent.wrapper_key } },
form_arguments: { method: :post, inputs: form_inputs }) do |item|
item.with_leading_visual_icon(icon: "chevron-up")
@@ -95,11 +95,11 @@ module Admin
end
def move_down_activity(menu)
- form_inputs = [{ name: "position", value: activity.position + 1 }]
+ form_inputs = [{ name: "move_to", value: "lower" }]
menu.with_item(label: I18n.t(:label_sort_lower),
tag: :button,
- href: admin_time_entry_activity_move_path(activity),
+ href: move_admin_time_entry_activity_path(activity),
# content_arguments: { data: { turbo_frame: ItemsComponent.wrapper_key } },
form_arguments: { method: :post, inputs: form_inputs }) do |item|
item.with_leading_visual_icon(icon: "chevron-down")
@@ -107,11 +107,11 @@ module Admin
end
def move_to_bottom_activity(menu)
- form_inputs = [{ name: "position", value: max_activity_position + 1 }]
+ form_inputs = [{ name: "move_to", value: "lowest" }]
menu.with_item(label: I18n.t(:label_sort_lowest),
tag: :button,
- href: admin_time_entry_activity_move_path(activity),
+ href: move_admin_time_entry_activity_path(activity),
# content_arguments: { data: { turbo_frame: ItemsComponent.wrapper_key } },
form_arguments: { method: :post, inputs: form_inputs }) do |item|
item.with_leading_visual_icon(icon: "move-to-bottom")
diff --git a/modules/costs/app/components/admin/time_entry_activities/reassign_form.rb b/modules/costs/app/components/admin/time_entry_activities/reassign_form.rb
new file mode 100644
index 00000000000..6cb909fa2ad
--- /dev/null
+++ b/modules/costs/app/components/admin/time_entry_activities/reassign_form.rb
@@ -0,0 +1,67 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+module Admin
+ module TimeEntryActivities
+ class ReassignForm < ApplicationForm
+ include OpenProject::StaticRouting::UrlHelpers
+
+ attr_reader :other_activities
+
+ def initialize(other_activities:)
+ super()
+
+ @other_activities = other_activities
+ end
+
+ form do |form|
+ form.select_list(
+ name: :reassign_to_id,
+ label: I18n.t(:text_enumeration_category_reassign_to),
+ required: true,
+ input_width: :large
+ ) do |select|
+ other_activities.each do |activity|
+ select.option(value: activity.id, label: activity.name)
+ end
+ end
+
+ form.group(layout: :horizontal) do |button_group|
+ button_group.button(name: :cancel,
+ tag: :a,
+ label: I18n.t(:button_cancel),
+ scheme: :default,
+ href: admin_time_entry_activities_path)
+ button_group.submit(name: :submit, label: I18n.t(:button_apply), scheme: :primary)
+ end
+ end
+ end
+ end
+end
diff --git a/modules/costs/app/controllers/admin/time_entry_activities_controller.rb b/modules/costs/app/controllers/admin/time_entry_activities_controller.rb
index e457a0fe5f2..f78bdb03f8c 100644
--- a/modules/costs/app/controllers/admin/time_entry_activities_controller.rb
+++ b/modules/costs/app/controllers/admin/time_entry_activities_controller.rb
@@ -32,6 +32,7 @@ module Admin
class TimeEntryActivitiesController < ApplicationController
# Allow only admins here
before_action :require_admin
+ before_action :find_time_entry_activity, only: %i[edit update destroy move reassign]
layout "admin"
menu_item :time_entry_activities
@@ -44,16 +45,81 @@ module Admin
@time_entry_activity = TimeEntryActivity.new
end
- def edit
- @time_entry_activity = TimeEntryActivity.find(params[:id])
+ def edit; end
+
+ def create
+ @time_entry_activity = TimeEntryActivity.new(permitted_params.time_entry_activities)
+
+ if @time_entry_activity.save
+ flash[:notice] = I18n.t(:notice_successful_update)
+ redirect_to(action: :index)
+ else
+ render action: :new, status: :unprocessable_entity
+ end
+ end
+
+ def update
+ if @time_entry_activity.update(permitted_params.time_entry_activities)
+ flash[:notice] = I18n.t(:notice_successful_update)
+ redirect_to(action: :index)
+ else
+ render action: :edit, status: :unprocessable_entity
+ end
+ end
+
+ def destroy
+ if @time_entry_activity.in_use?
+ handle_reassignment_on_deletion
+ elsif @time_entry_activity.destroy
+ flash[:notice] = I18n.t(:notice_successful_delete)
+ redirect_to(action: :index)
+ else
+ flash.now[:error] = I18n.t(:error_can_not_delete_entry)
+ redirect_to(action: :index)
+ end
+ end
+
+ def move
+ if @time_entry_activity.update(move_to: move_to_attribute)
+ flash[:notice] = I18n.t(:notice_successful_update)
+ else
+ flash[:error] = I18n.t(:error_type_could_not_be_saved)
+ end
+
+ redirect_to(action: :index)
+ end
+
+ def reassign
+ @other_activities = TimeEntryActivity.all - [@time_entry_activity]
end
- def create; end
+ private
- def update; end
+ def move_to_attribute
+ move_to = params[:move_to]
+
+ if move_to.in? %w(highest higher lower lowest)
+ move_to
+ else
+ raise ArgumentError, "Invalid move_to value: #{move_to}"
+ end
+ end
- def destroy; end
+ def handle_reassignment_on_deletion
+ reassign_to_id = params.dig(:time_entry_activity, :reassign_to_id)
- def move; end
+ if reassign_to_id.present?
+ reassign_to = TimeEntryActivity.find_by(id: reassign_to_id)
+ @time_entry_activity.destroy(reassign_to)
+ flash[:notice] = I18n.t(:notice_successful_delete)
+ redirect_to(action: :index)
+ else
+ redirect_to(action: :reassign, id: @time_entry_activity.id)
+ end
+ end
+
+ def find_time_entry_activity
+ @time_entry_activity = TimeEntryActivity.find(params[:id])
+ end
end
end
diff --git a/modules/costs/app/views/admin/time_entry_activities/reassign.html.erb b/modules/costs/app/views/admin/time_entry_activities/reassign.html.erb
new file mode 100644
index 00000000000..1d5486f9bcc
--- /dev/null
+++ b/modules/costs/app/views/admin/time_entry_activities/reassign.html.erb
@@ -0,0 +1,48 @@
+<%#-- copyright
+OpenProject is an open source project management software.
+Copyright (C) the OpenProject GmbH
+
+This program is free software; you can redistribute it and/or
+modify it under the terms of the GNU General Public License version 3.
+
+OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+Copyright (C) 2006-2013 Jean-Philippe Lang
+Copyright (C) 2010-2013 the ChiliProject Team
+
+This program is free software; you can redistribute it and/or
+modify it under the terms of the GNU General Public License
+as published by the Free Software Foundation; either version 2
+of the License, or (at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.v
+See COPYRIGHT and LICENSE files for more details.
+
+++#%>
+
+<% html_title t(:label_administration), t(:enumeration_activities) %>
+
+<%=
+ render(Primer::OpenProject::PageHeader.new) do |header|
+ header.with_title { @time_entry_activity.name }
+ header.with_description { t(:text_enumeration_destroy_question, count: @time_entry_activity.objects_count) }
+ header.with_breadcrumbs(
+ [
+ { href: admin_index_path, text: t("label_administration") },
+ { href: admin_costs_settings_path, text: t(:project_module_costs) },
+ { href: admin_time_entry_activities_path, text: I18n.t(:enumeration_activities) },
+ @time_entry_activity.name
+ ]
+ )
+ end
+%>
+
+<%= primer_form_with(model: @time_entry_activity, url: admin_time_entry_activity_path(@time_entry_activity), method: :delete) do |f| %>
+ <%= render(Admin::TimeEntryActivities::ReassignForm.new(f, other_activities: @other_activities)) %>
+<% end %>
diff --git a/modules/costs/config/routes.rb b/modules/costs/config/routes.rb
index e00afb5d3ce..ab83cb18271 100644
--- a/modules/costs/config/routes.rb
+++ b/modules/costs/config/routes.rb
@@ -70,7 +70,10 @@ Rails.application.routes.draw do
namespace :admin do
resources :time_entry_activities, except: [:show] do
- post :move
+ member do
+ post :move
+ get :reassign
+ end
end
resources :cost_types, only: %i[index new edit update create destroy] do
diff --git a/modules/costs/lib/costs/patches/permitted_params_patch.rb b/modules/costs/lib/costs/patches/permitted_params_patch.rb
index c3d4d269c68..aeb7cf493d6 100644
--- a/modules/costs/lib/costs/patches/permitted_params_patch.rb
+++ b/modules/costs/lib/costs/patches/permitted_params_patch.rb
@@ -86,5 +86,9 @@ module Costs::Patches::PermittedParamsPatch
)
.merge(custom_field_values(:time_entry))
end
+
+ def time_entry_activities
+ params.require(:time_entry_activity).permit(:name, :active)
+ end
end
end