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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
[#47010] Replace Members::TableCell by ViewComponent
Dombi Attila60b162abb6a3
diff --git a/app/components/members/row_component.rb b/app/components/members/row_component.rbnew file mode 100644index 00000000000..01f47f6523f--- /dev/null+++ b/app/components/members/row_component.rb@@ -0,0 +1,205 @@+# 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 Members+ class RowComponent < ::RowComponent+ include AvatarHelper+ include UsersHelper++ property :principal++ def member+ model+ end++ def row_css_id+ "member-#{member.id}"+ end++ def row_css_class+ "member #{principal_class_name}".strip+ end++ def name+ icon = avatar principal, size: :mini++ icon + principal_link+ end++ def mail+ return unless user?+ return if principal.pref.hide_mail++ link = mail_to(principal.mail)++ if member.principal.invited?+ i = content_tag "i", "", title: t("text_user_invited"), class: "icon icon-mail1"++ link + i+ else+ link+ end+ end++ def roles+ span = content_tag "span", roles_label, id: "member-#{member.id}-roles"++ if may_update?+ span + role_form_cell.call+ else+ span+ end+ end++ def roles_label+ label = h member.roles.uniq.sort.collect(&:name).join(', ')++ if principal&.admin?+ label << tag(:br)+ label << I18n.t(:label_member_all_admin)+ end++ label+ end++ def role_form_cell+ Members::RoleFormCell.new(+ member,+ row: self,+ params: controller.params,+ roles: table.available_roles,+ context: { controller: }+ )+ end++ def groups+ if user?+ principal.groups.map(&:name).join(", ")+ end+ end++ def status+ translate_user_status(model.principal.status)+ end++ def may_update?+ table.authorize_update+ end++ def may_delete?+ table.authorize_update+ end++ def button_links+ if may_update? && may_delete?+ [edit_link, delete_link].compact+ elsif may_delete?+ [delete_link].compact+ else+ []+ end+ end++ def edit_link+ link_to(+ op_icon('icon icon-edit'),+ '#',+ class: "toggle-membership-button #{toggle_item_class_name}",+ data: { 'toggle-target': ".#{toggle_item_class_name}" },+ title: t(:button_edit)+ )+ end++ def roles_css_id+ "member-#{member.id}-roles"+ end++ def toggle_item_class_name+ "member-#{member.id}--edit-toggle-item"+ end++ def delete_link+ if model.deletable?+ link_to(+ op_icon('icon icon-delete'),+ { controller: '/members', action: 'destroy', id: model, page: params[:page] },+ method: :delete,+ data: { confirm: delete_link_confirmation, disable_with: I18n.t(:label_loading) },+ title: delete_title+ )+ end+ end++ def delete_title+ if model.disposable?+ I18n.t(:title_remove_and_delete_user)+ else+ I18n.t(:button_remove)+ end+ end++ def delete_link_confirmation+ if !User.current.admin? && model.include?(User.current)+ t(:text_own_membership_delete_confirmation)+ end+ end++ def column_css_class(column)+ if column == :mail+ "email"+ else+ super+ end+ end++ def principal_link+ link_to principal.name, principal_show_path+ end++ def principal_class_name+ principal.model_name.singular+ end++ def principal_show_path+ case principal+ when User+ user_path(principal)+ when Group+ show_group_path(principal)+ else+ placeholder_user_path(principal)+ end+ end++ def user?+ principal.is_a?(User)+ end+ end+enddiff --git a/app/components/members/table_component.rb b/app/components/members/table_component.rbnew file mode 100644index 00000000000..b809f339961--- /dev/null+++ b/app/components/members/table_component.rb@@ -0,0 +1,70 @@+# 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 Members+ class TableComponent < ::TableComponent+ options :authorize_update, :available_roles, :is_filtered+ columns :name, :mail, :roles, :groups, :status+ sortable_columns :name, :mail, :status++ def initial_sort+ %i[name asc]+ end++ def headers+ columns.map do |name|+ [name.to_s, header_options(name)]+ end+ end++ def header_options(name)+ { caption: User.human_attribute_name(name) }+ end++ ##+ # Adjusts the order so that users are joined to support+ # sorting by their attributes+ def sort_collection(query, sort_clause, sort_columns)+ super(join_users(query), sort_clause, sort_columns)+ end++ def join_users(query)+ query.joins(:principal).references(:principal)+ end++ def empty_row_message+ if is_filtered+ I18n.t :notice_no_principals_found+ else+ I18n.t :'members.index.no_results_title_text'+ end+ end+ end+enddiff --git a/app/components/rails_component.rb b/app/components/rails_component.rbnew file mode 100644index 00000000000..0fbe8450db2--- /dev/null+++ b/app/components/rails_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.+#++++class RailsComponent < ViewComponent::Base+ include ApplicationHelper++ attr_reader :options++ def initialize(**options)+ super+ @options = options+ end++ class << self+ ##+ # Defines options for this cell which can be used within the cell's template.+ # Options are passed to the cell during the render call.+ #+ # @param names [Array<String> | Hash<String, Any>] Either a list of names for options whose+ # default value is empty or a hash mapping+ # option names to default values.+ def options(*names)+ default_values = {}++ if names.size == 1 && names.first.is_a?(Hash)+ default_values = names.first+ names = default_values.keys+ end++ names.each do |name|+ define_method(name) do+ options[name] || default_values[name]+ end+ end+ end++ def property(*names)+ delegate *names, to: :model+ end+ end+enddiff --git a/app/components/row_component.html.erb b/app/components/row_component.html.erbindex 5163f7f7eb1..b4840a36db8 100644--- a/app/components/row_component.html.erb+++ b/app/components/row_component.html.erb@@ -27,7 +27,10 @@ See COPYRIGHT and LICENSE files for more details.++#%>-<tr>+<tr+ <%= "id=\"#{row_css_id}\"" if row_css_id %>+ <%= "class=\"#{row_css_class}\"" if row_css_class %>+><% columns.each do |column| %><td class="<%= column_css_class(column) %>"><%= column_value(column) %>diff --git a/app/components/row_component.rb b/app/components/row_component.rbindex df72565aa17..72851e1af55 100644--- a/app/components/row_component.rb+++ b/app/components/row_component.rb@@ -30,17 +30,21 @@### Abstract view component. Subclass this for a concrete table row.-class RowComponent < ViewComponent::Base- attr_reader :row, :table+class RowComponent < RailsComponent+ attr_reader :model, :table- def initialize(row:, table:)- super()- @row = row+ def initialize(row:, table:, **options)+ super(**options)+ @model = row@table = tableenddelegate :columns, to: :table+ def row+ model+ end+def column_value(column)send(column)end@@ -57,6 +61,14 @@ class RowComponent < ViewComponent::Base[]end+ def row_css_id+ nil+ end++ def row_css_class+ nil+ end+def checkmark(condition)if conditionhelpers.op_icon 'icon icon-checkmark'diff --git a/app/components/table_component.html.erb b/app/components/table_component.html.erbindex 2d5fdded67e..76ab5672efd 100644--- a/app/components/table_component.html.erb+++ b/app/components/table_component.html.erb@@ -39,15 +39,19 @@ See COPYRIGHT and LICENSE files for more details.<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>+ <% if sortable_column?(name) %>+ <%= sort_header_tag name, options %>+ <% else %>+ <th>+ <div class="generic-table--sort-header-outer">+ <div class="generic-table--sort-header">+ <span>+ <%= options[:caption] %>+ </span>+ </div></div>- </div>- </th>+ </th>+ <% end %><% end %><th><%# last column for buttons %>@@ -73,3 +77,6 @@ See COPYRIGHT and LICENSE files for more details.<% end %></div></div>+<% if paginated? %>+ <%= pagination_links_full rows %>+<% end %>diff --git a/app/components/table_component.rb b/app/components/table_component.rbindex 9f9278e9ef6..994c1b3a863 100644--- a/app/components/table_component.rb+++ b/app/components/table_component.rb@@ -30,12 +30,15 @@### Abstract view component. Subclass this for a concrete table.-class TableComponent < ViewComponent::Base- attr_reader :rows+class TableComponent < RailsComponent+ include SortHelper+ include PaginationHelper- def initialize(rows:)- super()- @rows = rows+ attr_reader :model++ def initialize(rows:, **options)+ super(**options)+ @model = rowsendclass << self@@ -52,34 +55,121 @@ class TableComponent < ViewComponent::Base@columns = names.map(&:to_sym)end++ ##+ # Define which of the registered columns are sortable+ # Applies only if +sortable?+ is true+ def sortable_columns(*names)+ if names.present?+ @sortable_columns = names.map(&:to_sym)+ # set available criteria+ return+ end++ # return all columns unless defined otherwise+ if @sortable_columns.nil?+ columns+ else+ Array(@sortable_columns)+ end+ end++ def add_column(name)+ @columns = Array(@columns) + [name]+ row_class.property name+ end++ def row_class+ mod = name.split("::")[0..-2].join("::").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`."+ )+ endend- def row_class- mod = self.class.name.deconstantize.presence || "Table"+ def before_render+ initialize_sorted_model if sortable?+ end++ def initialize_sorted_model+ sort_init *initial_sort.map(&:to_s)+ sort_update sortable_columns.map(&:to_s)+ @model = paginate_collection apply_sort(model)+ end++ def apply_sort(model)+ case model+ when ActiveRecord::QueryMethods+ sort_collection(model, sort_clause)+ when Queries::BaseQuery+ model+ .order(@sort_criteria.to_query_hash)+ .results+ else+ raise ArgumentError, "Cannot sort the given model class #{model.class}"+ end+ end- "#{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`."- )+ ##+ # Sorts the data to be displayed.+ #+ # @param query [ActiveRecord::QueryMethods] An active record collection.+ # @param sort_clause [String] The SQL used as the sort clause.+ def sort_collection(query, sort_clause)+ query+ .reorder(sort_clause)+ .order(Arel.sql(initial_order))+ end++ def paginate_collection(query)+ query+ .page(page_param(controller.params))+ .per_page(per_page_param)+ end++ def rows+ model+ end++ def row_class+ self.class.row_classenddef columnsself.class.columnsend+ def sortable_columns+ self.class.sortable_columns+ end+def render_row(row)render(row_class.new(row:, table: self))end+ def render_collection(rows)+ row_class.with_collection(rows, table: self)+ end+def inline_create_linknilend+ def paginated?+ rows.respond_to? :total_entries+ end+def sortable?- false+ true+ end++ def sortable_column?(column)+ sortable? && sortable_columns.include?(column.to_sym)enddef empty_row_messagediff --git a/app/views/members/index.html.erb b/app/views/members/index.html.erbindex 3b011207cea..18deaff9011 100644--- a/app/views/members/index.html.erb+++ b/app/views/members/index.html.erb@@ -71,7 +71,5 @@ See COPYRIGHT and LICENSE files for more details.<div><%= rails_cell Members::UserFilterCell, params, **@members_filter_options %>- <%= rails_cell Members::TableCell, @members, **@members_table_options %>+ <%= render ::Members::TableComponent.new(rows: @members, **@members_table_options) %></div>--diff --git a/modules/costs/lib/costs/patches/members_patch.rb b/modules/costs/lib/costs/patches/members_patch.rbindex e7b7234a1a9..1f239954edc 100644--- a/modules/costs/lib/costs/patches/members_patch.rb+++ b/modules/costs/lib/costs/patches/members_patch.rb@@ -29,12 +29,12 @@module Costsmodule Patches::MembersPatchdef self.mixin!- ::Members::TableCell.add_column :current_rate- ::Members::TableCell.options :current_user # adds current_user option+ ::Members::TableComponent.add_column :current_rate+ ::Members::TableComponent.options :current_user # adds current_user option::MembersController.prepend TableOptions- ::Members::TableCell.prepend TableCell- ::Members::RowCell.prepend RowCell+ ::Members::TableComponent.prepend TableComponent+ ::Members::RowComponent.prepend RowComponentendmodule TableOptions@@ -43,7 +43,7 @@ module Costsendend- module TableCell+ module TableComponentdef sort_collection(query, sort_clause, sort_columns)q = super query, sort_clause.gsub(/current_rate/, 'COALESCE(rate, 0.0)'), sort_columns@@ -140,7 +140,7 @@ module Costsendend- module RowCell+ module RowComponentinclude ActionView::Helpers::NumberHelper # for #number_to_currency##