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

Replace CustomActions::TableCell by ViewComponent

Christophe Bliard05e0eec67fc5
diff --git a/app/cells/custom_actions/row_cell.rb b/app/cells/custom_actions/row_cell.rb
deleted file mode 100644
index e568539b9b1..00000000000
--- a/app/cells/custom_actions/row_cell.rb
+++ /dev/null
@@ -1,43 +0,0 @@
-module CustomActions
- class RowCell < ::RowCell
- include ::IconsHelper
- include ReorderLinksHelper
-
- def action
- model
- end
-
- def name
- link_to h(action.name), edit_custom_action_path(action)
- end
-
- def sort
- reorder_links('custom_action', { action: 'update', id: action }, method: :put)
- end
-
- def button_links
- [
- edit_link,
- delete_link
- ]
- end
-
- def edit_link
- link_to(
- op_icon('icon icon-edit'),
- edit_custom_action_path(action),
- title: t(:button_edit)
- )
- end
-
- def delete_link
- link_to(
- op_icon('icon icon-delete'),
- custom_action_path(action),
- method: :delete,
- data: { confirm: I18n.t(:text_are_you_sure) },
- title: t(:button_delete)
- )
- end
- end
-end
diff --git a/app/cells/custom_actions/table_cell.rb b/app/cells/custom_actions/table_cell.rb
deleted file mode 100644
index 3b3fd4f184a..00000000000
--- a/app/cells/custom_actions/table_cell.rb
+++ /dev/null
@@ -1,36 +0,0 @@
-module CustomActions
- class TableCell < ::TableCell
- columns :name,
- :description,
- :sort
-
- def initial_sort
- %i[id asc]
- end
-
- def sortable?
- false
- end
-
- def inline_create_link
- link_to new_custom_action_path,
- aria: { label: t('custom_actions.new') },
- class: 'wp-inline-create--add-link',
- title: t('custom_actions.new') do
- op_icon('icon icon-add')
- end
- end
-
- def empty_row_message
- I18n.t :no_results_title_text
- end
-
- def headers
- [
- ['name', { caption: CustomAction.human_attribute_name(:name) }],
- ['description', { caption: CustomAction.human_attribute_name(:description) }],
- ['sort', { caption: I18n.t(:label_sort) }]
- ]
- end
- end
-end
diff --git a/app/components/custom_actions/row_component.rb b/app/components/custom_actions/row_component.rb
new file mode 100644
index 00000000000..ffe4c4c40f9
--- /dev/null
+++ b/app/components/custom_actions/row_component.rb
@@ -0,0 +1,72 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2023 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 CustomActions
+ class RowComponent < ::RowComponent
+ def action
+ row
+ end
+
+ def name
+ link_to action.name, edit_custom_action_path(action)
+ end
+
+ delegate :description, to: :action
+
+ def sort
+ helpers.reorder_links('custom_action', { action: 'update', id: action }, method: :put)
+ end
+
+ def button_links
+ [
+ edit_link,
+ delete_link
+ ]
+ end
+
+ def edit_link
+ link_to(
+ helpers.op_icon('icon icon-edit'),
+ helpers.edit_custom_action_path(action),
+ title: t(:button_edit)
+ )
+ end
+
+ def delete_link
+ link_to(
+ helpers.op_icon('icon icon-delete'),
+ helpers.custom_action_path(action),
+ method: :delete,
+ data: { confirm: I18n.t(:text_are_you_sure) },
+ title: t(:button_delete)
+ )
+ end
+ end
+end
diff --git a/app/components/custom_actions/table_component.rb b/app/components/custom_actions/table_component.rb
new file mode 100644
index 00000000000..f6ec1c400a7
--- /dev/null
+++ b/app/components/custom_actions/table_component.rb
@@ -0,0 +1,54 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2023 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 CustomActions
+ class TableComponent < ::TableComponent
+ columns :name,
+ :description,
+ :sort
+
+ def headers
+ [
+ ['name', { caption: CustomAction.human_attribute_name(:name) }],
+ ['description', { caption: CustomAction.human_attribute_name(:description) }],
+ ['sort', { caption: I18n.t(:label_sort) }]
+ ]
+ end
+
+ def inline_create_link
+ link_to new_custom_action_path,
+ aria: { label: t('custom_actions.new') },
+ class: 'wp-inline-create--add-link',
+ title: t('custom_actions.new') do
+ helpers.op_icon('icon icon-add')
+ end
+ end
+ end
+end
diff --git a/app/components/table_component.rb b/app/components/table_component.rb
index 97738c8e1f9..9f9278e9ef6 100644
--- a/app/components/table_component.rb
+++ b/app/components/table_component.rb
@@ -38,6 +38,22 @@ class TableComponent < ViewComponent::Base
@rows = rows
end
+ class << self
+ # Declares columns shown in the table.
+ #
+ # Use it in subclasses like so:
+ #
+ # columns :name, :description, :sort
+ #
+ # When table is sortable, the column names are used by sort logic. It means
+ # these names will be used directly in the generated SQL queries.
+ def columns(*names)
+ return Array(@columns) if names.empty?
+
+ @columns = names.map(&:to_sym)
+ end
+ end
+
def row_class
mod = self.class.name.deconstantize.presence || "Table"
diff --git a/app/views/custom_actions/index.html.erb b/app/views/custom_actions/index.html.erb
index 5b1c086af65..102406b6f62 100644
--- a/app/views/custom_actions/index.html.erb
+++ b/app/views/custom_actions/index.html.erb
@@ -12,5 +12,5 @@
<% end %>
<section class="admin--edit-section">
- <%= rails_cell ::CustomActions::TableCell, @custom_actions %>
+ <%= render(::CustomActions::TableComponent.new(rows: @custom_actions)) %>
</section>