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
[#47016] Replace Users::TableCell by ViewComponent
Dombi Attiladb4b927e3ddc
diff --git a/app/cells/individual_principal_base_filter_cell.rb b/app/cells/individual_principal_base_filter_cell.rbdeleted file mode 100644index c4580a1b790..00000000000--- a/app/cells/individual_principal_base_filter_cell.rb+++ /dev/null@@ -1,115 +0,0 @@-#-- 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-2017 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 IndividualPrincipalBaseFilterCell < RailsCell- include UsersHelper- include ActionView::Helpers::FormOptionsHelper-- class << self- def query(params)- q = base_query.new-- apply_filters(params, q)-- q- end-- def filter(params)- query(params).results- end-- def filtered?(params)- %i(name status group_id role_id).any? { |name| params[name].present? }- end-- def filter_name(query, name)- if name.present?- query.where(:any_name_attribute, '~', name)- end- end-- def filter_group(query, group_id)- if group_id.present?- query.where(:group, '=', group_id)- end- end-- def filter_role(query, role_id)- if role_id.present?- query.where(:role_id, '=', role_id)- end- end-- def filter_project(query, project_id)- if project_id.present?- query.where(:project_id, '=', project_id)- end- end-- def base_query- raise NotImplementedError- end-- protected-- def apply_filters(params, query)- filter_project query, params[:project_id]- filter_name query, params[:name]- filter_group query, params[:group_id]- filter_role query, params[:role_id]-- query- end- end-- # INSTANCE METHODS:-- def filter_path- raise NotImplementedError- end-- def initially_visible?- true- end-- def has_close_icon?- false- end-- def has_statuses?- defined?(status)- end-- def has_groups?- defined?(groups) && groups.present?- end-- def params- model- end-enddiff --git a/app/cells/users/row_cell.rb b/app/cells/users/row_cell.rbdeleted file mode 100644index 3c2da77fbff..00000000000--- a/app/cells/users/row_cell.rb+++ /dev/null@@ -1,68 +0,0 @@-module Users- class RowCell < ::RowCell- include AvatarHelper- include UsersHelper-- def user- model- end-- def row_css_class- status = user.status- blocked = "blocked" if user.failed_too_many_recent_login_attempts?-- ["user", status, blocked].compact.join(" ")- end-- def login- icon = avatar user, size: :mini- link = link_to h(user.login), edit_user_path(user)-- icon + link- end-- def mail- mail_to user.mail- end-- def admin- checked_image user.admin?- end-- def last_login_on- format_time user.last_login_on unless user.last_login_on.nil?- end-- def created_at- format_time user.created_at- end-- def status- full_user_status user- end-- def button_links- [status_link].compact- end-- def status_link- # Don't show for current user- return if user.id == table.current_user.id-- # Don't show if non-admin- return unless table.current_user.admin?-- change_user_status_links user- end-- def column_css_class(column)- if column == :mail- "email"- elsif column == :login- "username"- else- super- end- end- end-enddiff --git a/app/cells/users/table_cell.rb b/app/cells/users/table_cell.rbdeleted file mode 100644index cc90388164f..00000000000--- a/app/cells/users/table_cell.rb+++ /dev/null@@ -1,28 +0,0 @@-module Users- class TableCell < ::TableCell- options :current_user # adds this option to those of the base class- columns :login, :firstname, :lastname, :mail, :admin, :created_at, :last_login_on-- def initial_sort- %i[id asc]- end-- def headers- columns.map do |name|- [name.to_s, header_options(name)]- end- end-- def header_options(name)- options = { caption: User.human_attribute_name(name) }-- options[:default_order] = 'desc' if desc_by_default.include? name-- options- end-- def desc_by_default- %i[admin created_at last_login_on]- end- end-enddiff --git a/app/cells/views/individual_principal_base_filter/show.erb b/app/cells/views/individual_principal_base_filter/show.erbdeleted file mode 100644index 362bd968e20..00000000000--- a/app/cells/views/individual_principal_base_filter/show.erb+++ /dev/null@@ -1,91 +0,0 @@-<%#-- 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.--++#%>--<%= form_tag(filter_path, method: :get) do %>- <% collapsed_class = initially_visible? ? '' : 'collapsed' %>- <fieldset class="simple-filters--container <%= collapsed_class %>">- <legend><%= t(:label_filter_plural) %></legend>- <% if has_close_icon? %>- <a title="<%= t('js.close_form_title') %>"- class="toggle-member-filter-link simple-filters--close icon-context icon-close">- </a>- <% end %>- <ul class="simple-filters--filters">- <% if has_statuses? %>- <li class="simple-filters--filter">- <label class='simple-filters--filter-name' for='status'><%= User.human_attribute_name(:status) %>:</label>- <%= select_tag 'status', user_status_options, class: 'simple-filters--filter-value' %>- </li>- <% end %>- <% if has_groups? %>- <li class="simple-filters--filter">- <label class='simple-filters--filter-name' for='group_id'><%= Group.model_name.human %>:</label>- <%= collection_select :group,- :id,- groups,- :id,- :name,- { include_blank: true,- selected: params[:group_id].to_i },- { name: "group_id",- class: 'simple-filters--filter-value' } %>- </li>- <% end %>- <% if roles.present? %>- <li class="simple-filters--filter">- <label class='simple-filters--filter-name' for='role_id'><%= Role.model_name.human %>:</label>- <%=- collection_select(- :role,- :id,- roles,- :id,- :name,- {- include_blank: true,- selected: params[:role_id].to_i- },- {- name: "role_id",- class: 'simple-filters--filter-value'- })- %>- </li>- <% end %>- <li class="simple-filters--filter">- <label class='simple-filters--filter-name' for='name'><%= User.human_attribute_name :name %>:</label>- <%= text_field_tag 'name', params[:name], class: 'simple-filters--filter-value' %>- </li>- <li class="simple-filters--controls">- <%= submit_tag t(:button_apply), class: 'button -highlight -small', name: nil %>- <%= link_to t(:button_clear), clear_url, class: 'button -small -with-icon icon-undo' %>- </li>- </ul>- </fieldset>-<% end %>diff --git a/app/components/users/row_component.rb b/app/components/users/row_component.rbnew file mode 100644index 00000000000..1d155756bbf--- /dev/null+++ b/app/components/users/row_component.rb@@ -0,0 +1,100 @@+# 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 Users+ class RowComponent < ::RowComponent+ include AvatarHelper+ include UsersHelper++ property :firstname, :lastname++ def user+ model+ end++ def row_css_class+ status = user.status+ blocked = "blocked" if user.failed_too_many_recent_login_attempts?++ ["user", status, blocked].compact.join(" ")+ end++ def login+ icon = avatar user, size: :mini+ link = link_to h(user.login), edit_user_path(user)++ icon + link+ end++ def mail+ mail_to user.mail+ end++ def admin+ checked_image user.admin?+ end++ def last_login_on+ format_time user.last_login_on unless user.last_login_on.nil?+ end++ def created_at+ format_time user.created_at+ end++ def status+ full_user_status user+ end++ def button_links+ [status_link].compact+ end++ def status_link+ # Don't show for current user+ return if user.id == table.current_user.id++ # Don't show if non-admin+ return unless table.current_user.admin?++ change_user_status_links user+ end++ def column_css_class(column)+ if column == :mail+ "email"+ elsif column == :login+ "username"+ else+ super+ end+ end+ end+enddiff --git a/app/cells/user_filter_cell.rb b/app/components/users/table_component.rbsimilarity index 51%rename from app/cells/user_filter_cell.rbrename to app/components/users/table_component.rbindex 6695e1cce51..8af4f9a1b07 100644--- a/app/cells/user_filter_cell.rb+++ b/app/components/users/table_component.rb@@ -1,3 +1,5 @@+# frozen_string_literal: true+#-- copyright# OpenProject is an open source project management software.# Copyright (C) 2012-2023 the OpenProject GmbH@@ -6,7 +8,7 @@# 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-2017 Jean-Philippe Lang+# 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@@ -26,57 +28,31 @@# See COPYRIGHT and LICENSE files for more details.#++-class UserFilterCell < IndividualPrincipalBaseFilterCell- options :groups, :status, :roles, :clear_url, :project+module Users+ class TableComponent < ::TableComponent+ columns :login, :firstname, :lastname, :mail, :admin, :created_at, :last_login_on+ options :current_user- class << self- ##- # Returns the selected status from the parameters- # or the default status to be filtered by (all)- # if no status is given.- def status_param(params)- params[:status].presence || 'all'+ def initial_sort+ %i[id asc]end- def filter_status(query, status)- return unless status && status != 'all'-- case status- when 'blocked'- query.where(:blocked, '=', :blocked)- when 'active'- query.where(:status, '=', status.to_sym)- query.where(:blocked, '!', :blocked)- else- query.where(:status, '=', status.to_sym)+ def headers+ columns.map do |name|+ [name.to_s, header_options(name)]endend- def base_query- Queries::Users::UserQuery- end-- protected+ def header_options(name)+ options = { caption: User.human_attribute_name(name) }- def apply_filters(params, query)- super(params, query)- filter_status query, status_param(params)+ options[:default_order] = 'desc' if desc_by_default.include? name- query+ optionsend- end-- # INSTANCE METHODS:- def filter_path- users_path- end-- def user_status_options- users_status_options_for_select status, extra: extra_user_status_options- end-- def extra_user_status_options- {}+ def desc_by_default+ %i[admin created_at last_login_on]+ endendenddiff --git a/app/cells/users/user_filter_cell.rb b/app/components/users/user_filter_component.rbsimilarity index 91%rename from app/cells/users/user_filter_cell.rbrename to app/components/users/user_filter_component.rbindex 517a754e015..c68bf7d03de 100644--- a/app/cells/users/user_filter_cell.rb+++ b/app/components/users/user_filter_component.rb@@ -1,3 +1,5 @@+# frozen_string_literal: true+#-- copyright# OpenProject is an open source project management software.# Copyright (C) 2012-2023 the OpenProject GmbH@@ -6,7 +8,7 @@# 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-2017 Jean-Philippe Lang+# 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@@ -27,7 +29,7 @@#++module Users- class UserFilterCell < ::UserFilterCell+ class UserFilterComponent < ::UserFilterComponentdef filter_role(query, role_id)super.uniqenddiff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rbindex 2fc7a7b7437..e4a10dc8f26 100644--- a/app/controllers/users_controller.rb+++ b/app/controllers/users_controller.rb@@ -60,8 +60,8 @@ class UsersController < ApplicationControllerdef index@groups = Group.all.sort- @status = Users::UserFilterCell.status_param params- @users = Users::UserFilterCell.filter params+ @status = Users::UserFilterComponent.status_param params+ @users = Users::UserFilterComponent.filter paramsenddef showdiff --git a/app/views/users/index.html.erb b/app/views/users/index.html.erbindex 3c9ee83e995..aafcab7ec17 100644--- a/app/views/users/index.html.erb+++ b/app/views/users/index.html.erb@@ -53,6 +53,6 @@ See COPYRIGHT and LICENSE files for more details.<%= call_hook(:user_admin_action_menu) %><% end %>-<%= rails_cell Users::UserFilterCell, params, groups: @groups, status: @status %>+<%= render Users::UserFilterComponent.new(params, groups: @groups, status: @status) %> -<%= rails_cell Users::TableCell, @users, project: @project, current_user: current_user %>+<%= render Users::TableComponent.new(rows: @users, current_user: current_user) %>diff --git a/spec/views/users/index.html.erb_spec.rb b/spec/views/users/index.html.erb_spec.rbindex d8b521ab0f7..ebf063920ab 100644--- a/spec/views/users/index.html.erb_spec.rb+++ b/spec/views/users/index.html.erb_spec.rb@@ -40,9 +40,8 @@ describe 'users/index' doassign(:groups, Group.all)allow(view).to receive(:current_user).and_return(admin)-- allow_any_instance_of(TableCell).to receive(:controller_name).and_return("users")- allow_any_instance_of(TableCell).to receive(:action_name).and_return("index")+ allow(controller).to receive(:controller_name).and_return("users")+ allow(controller).to receive(:action_name).and_return("index")endsubject { rendered.squish }