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 Enumerations::TableCell by ViewComponent

Christophe Bliard8d1a6784bef9
diff --git a/app/cells/enumerations/row_cell.rb b/app/cells/enumerations/row_cell.rb
deleted file mode 100644
index 6b7f808c39e..00000000000
--- a/app/cells/enumerations/row_cell.rb
+++ /dev/null
@@ -1,51 +0,0 @@
-module Enumerations
- class RowCell < ::RowCell
- include ::IconsHelper
- include ::ColorsHelper
- include ReorderLinksHelper
-
- def enumeration
- model
- end
-
- def name
- link_to h(enumeration.name), edit_enumeration_path(enumeration)
- end
-
- def is_default
- if enumeration.is_default?
- op_icon 'icon icon-checkmark'
- end
- end
-
- def color
- icon_for_color enumeration.color
- end
-
- def active
- if enumeration.active?
- op_icon 'icon icon-checkmark'
- end
- end
-
- def sort
- reorder_links('enumeration', { action: 'update', id: enumeration }, method: :put)
- end
-
- def button_links
- [
- delete_link
- ]
- end
-
- def delete_link
- link_to(
- op_icon('icon icon-delete'),
- enumeration_path(enumeration),
- method: :delete,
- data: { confirm: I18n.t(:text_are_you_sure) },
- title: t(:button_delete)
- )
- end
- end
-end
diff --git a/app/cells/enumerations/table_cell.rb b/app/cells/enumerations/table_cell.rb
deleted file mode 100644
index 14e8c58443e..00000000000
--- a/app/cells/enumerations/table_cell.rb
+++ /dev/null
@@ -1,54 +0,0 @@
-module Enumerations
- class TableCell < ::TableCell
- def initial_sort
- %i[id asc]
- end
-
- def sortable?
- false
- end
-
- def with_colors
- model.colored?
- end
-
- def columns
- %i[name is_default active sort].tap do |default|
- if with_colors
- default.insert 3, :color
- end
- end
- end
-
- def inline_create_link
- link_to new_enumeration_path(type: model.name),
- aria: { label: t(:label_enumeration_new) },
- class: 'wp-inline-create--add-link',
- data: { 'qa-selector': "create-enumeration-#{model.name.underscore.dasherize}" },
- title: t(:label_enumeration_new) do
- op_icon('icon icon-add')
- end
- end
-
- def empty_row_message
- I18n.t :no_results_title_text
- end
-
- def row_class
- ::Enumerations::RowCell
- end
-
- def headers
- [
- ['name', { caption: Enumeration.human_attribute_name(:name) }],
- ['is_default', { caption: Enumeration.human_attribute_name(:is_default) }],
- ['is_default', { caption: Enumeration.human_attribute_name(:active) }],
- ['sort', { caption: I18n.t(:label_sort) }]
- ].tap do |default|
- if with_colors
- default.insert 3, ['color', { caption: Enumeration.human_attribute_name(:color) }]
- end
- end
- end
- end
-end
diff --git a/app/components/enumerations/row_component.rb b/app/components/enumerations/row_component.rb
new file mode 100644
index 00000000000..5117171a4b0
--- /dev/null
+++ b/app/components/enumerations/row_component.rb
@@ -0,0 +1,73 @@
+# 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 Enumerations
+ class RowComponent < ::RowComponent
+ def enumeration
+ row
+ end
+
+ def name
+ link_to h(enumeration.name), edit_enumeration_path(enumeration)
+ end
+
+ def is_default # rubocop:disable Naming/PredicateName
+ checkmark(enumeration.is_default?)
+ end
+
+ def color
+ helpers.icon_for_color enumeration.color
+ end
+
+ def active
+ checkmark(enumeration.active?)
+ end
+
+ def sort
+ helpers.reorder_links('enumeration', { action: 'update', id: enumeration }, method: :put)
+ end
+
+ def button_links
+ [
+ delete_link
+ ]
+ end
+
+ def delete_link
+ helpers.link_to(
+ helpers.op_icon('icon icon-delete'),
+ helpers.enumeration_path(enumeration),
+ method: :delete,
+ data: { confirm: I18n.t(:text_are_you_sure) },
+ title: t(:button_delete)
+ )
+ end
+ end
+end
diff --git a/app/components/enumerations/table_component.rb b/app/components/enumerations/table_component.rb
new file mode 100644
index 00000000000..93520640a08
--- /dev/null
+++ b/app/components/enumerations/table_component.rb
@@ -0,0 +1,68 @@
+# 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 Enumerations
+ class TableComponent < ::TableComponent
+ def columns
+ %i[name is_default active sort].tap do |default|
+ if with_colors
+ default.insert 3, :color
+ end
+ end
+ end
+
+ def headers
+ [
+ ['name', { caption: Enumeration.human_attribute_name(:name) }],
+ ['is_default', { caption: Enumeration.human_attribute_name(:is_default) }],
+ ['active', { caption: Enumeration.human_attribute_name(:active) }],
+ ['sort', { caption: I18n.t(:label_sort) }]
+ ].tap do |default|
+ if with_colors
+ default.insert 3, ['color', { caption: Enumeration.human_attribute_name(:color) }]
+ end
+ end
+ end
+
+ def with_colors
+ rows.colored?
+ end
+
+ def inline_create_link
+ link_to new_enumeration_path(type: rows.name),
+ aria: { label: t(:label_enumeration_new) },
+ class: 'wp-inline-create--add-link',
+ data: { 'qa-selector': "create-enumeration-#{rows.name.underscore.dasherize}" },
+ title: t(:label_enumeration_new) do
+ helpers.op_icon('icon icon-add')
+ end
+ end
+ end
+end
diff --git a/app/components/row_component.html.erb b/app/components/row_component.html.erb
new file mode 100644
index 00000000000..5163f7f7eb1
--- /dev/null
+++ b/app/components/row_component.html.erb
@@ -0,0 +1,41 @@
+<%#-- 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.
+
+++#%>
+
+<tr>
+ <% columns.each do |column| %>
+ <td class="<%= column_css_class(column) %>">
+ <%= column_value(column) %>
+ </td>
+ <% end %>
+ <td class="buttons">
+ <% button_links.each do |link| %>
+ <%= link %>
+ <% end %>
+ </td>
+</tr>
diff --git a/app/components/row_component.rb b/app/components/row_component.rb
new file mode 100644
index 00000000000..df72565aa17
--- /dev/null
+++ b/app/components/row_component.rb
@@ -0,0 +1,65 @@
+# 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.
+#++
+
+##
+# Abstract view component. Subclass this for a concrete table row.
+class RowComponent < ViewComponent::Base
+ attr_reader :row, :table
+
+ def initialize(row:, table:)
+ super()
+ @row = row
+ @table = table
+ end
+
+ delegate :columns, to: :table
+
+ def column_value(column)
+ send(column)
+ end
+
+ def column_css_class(column)
+ column_css_classes[column]
+ end
+
+ def column_css_classes
+ @column_css_classes ||= columns.to_h { |name| [name, name] }
+ end
+
+ def button_links
+ []
+ end
+
+ def checkmark(condition)
+ if condition
+ helpers.op_icon 'icon icon-checkmark'
+ end
+ end
+end
diff --git a/app/components/table_component.html.erb b/app/components/table_component.html.erb
new file mode 100644
index 00000000000..2d5fdded67e
--- /dev/null
+++ b/app/components/table_component.html.erb
@@ -0,0 +1,75 @@
+<%#-- 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.
+
+++#%>
+
+<div class="generic-table--container">
+ <div class="generic-table--results-container">
+ <table class="generic-table">
+ <colgroup>
+ <% headers.each do |_name, _options| %>
+ <col opHighlightCol>
+ <% end %>
+ <col>
+ </colgroup>
+ <thead>
+ <tr>
+ <% headers.each do |name, options| %>
+ <th>
+ <div class="generic-table--sort-header-outer">
+ <div class="generic-table--sort-header">
+ <span>
+ <%= options[:caption] %>
+ </span>
+ </div>
+ </div>
+ </th>
+ <% end %>
+ <th>
+ <%# last column for buttons %>
+ <div class="generic-table--empty-header"></div>
+ </th>
+ </tr>
+ </thead>
+ <tbody>
+ <% if rows.empty? %>
+ <tr class="generic-table--empty-row">
+ <td colspan="<%= headers.length + 1 %>"><%= empty_row_message %></td>
+ </tr>
+ <% end %>
+ <% for row in rows do -%>
+ <%= render_row row %>
+ <% end -%>
+ </tbody>
+ </table>
+ <% if inline_create_link %>
+ <div class="wp-inline-create-button">
+ <%= inline_create_link %>
+ </div>
+ <% end %>
+ </div>
+</div>
diff --git a/app/components/table_component.rb b/app/components/table_component.rb
new file mode 100644
index 00000000000..59eaae7bd5f
--- /dev/null
+++ b/app/components/table_component.rb
@@ -0,0 +1,90 @@
+# 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.
+#++
+
+##
+# Abstract view component. Subclass this for a concrete table.
+class TableComponent < ViewComponent::Base
+ attr_reader :rows
+
+ def initialize(rows:)
+ super()
+ @rows = rows
+ end
+
+ def row_class
+ mod = self.class.name.deconstantize.presence || "Table"
+
+ "#{mod}::RowComponent".constantize
+ rescue NameError
+ raise(
+ NameError,
+ "#{mod}::RowComponent required by #{mod}::TableComponent not defined. " +
+ "Expected to be defined in `app/components/#{mod.underscore}/row_component.rb`."
+ )
+ end
+
+ def columns
+ self.class.columns
+ end
+
+ def render_row(row)
+ render(row_class.new(row:, table: self))
+ end
+
+ def inline_create_link
+ nil
+ end
+
+ def sortable?
+ false
+ end
+
+ # ##
+ # # An array listing each column and its respective options.
+ # #
+ # # @return Array<Array>
+ # def headers
+ # columns.map { |name| [name.to_s, {}] }
+ # end
+
+ def empty_row_message
+ I18n.t :no_results_title_text
+ end
+
+ # # required by the sort helper
+
+ # delegate :controller_name, to: :controller
+
+ # delegate :action_name, to: :controller
+
+ # def options
+ # super
+ # end
+end
diff --git a/app/views/enumerations/index.html.erb b/app/views/enumerations/index.html.erb
index eba6a305861..ca64a0b19e3 100644
--- a/app/views/enumerations/index.html.erb
+++ b/app/views/enumerations/index.html.erb
@@ -30,5 +30,5 @@ See COPYRIGHT and LICENSE files for more details.
<%= toolbar title: t(:label_enumerations) %>
<% Enumeration.subclasses.each do |klass| %>
<h3 class="-no-border"><%= t(klass::OptionName) %></h3>
- <%= rails_cell ::Enumerations::TableCell, klass.shared %>
+ <%= render(::Enumerations::TableComponent.new(rows: klass.shared)) %>
<% end %>