mirror of
https://github.com/opf/openproject.git
synced 2026-06-14 03:30:14 +00:00
move helper methods to project table/row component
This commit is contained in:
@@ -37,13 +37,12 @@ See COPYRIGHT and LICENSE files for more details.
|
||||
</td>
|
||||
<% end %>
|
||||
<td class="buttons">
|
||||
<% items = helpers.project_more_menu_items(project) %>
|
||||
<% if items.any? %>
|
||||
<% if more_menu_items.any? %>
|
||||
<ul class="project-actions">
|
||||
<li aria-haspopup="true" title="<%= I18n.t(:label_open_menu) %>" class="drop-down">
|
||||
<a class="icon icon-show-more-horizontal context-menu--icon" title="<%= t(:label_open_menu) %>" href></a>
|
||||
<ul style="display:none;" class="menu-drop-down-container">
|
||||
<% items.each do |item| %>
|
||||
<% more_menu_items.each do |item| %>
|
||||
<li>
|
||||
<%= link_to(*item) %>
|
||||
</li>
|
||||
|
||||
@@ -174,6 +174,85 @@ module Projects
|
||||
end
|
||||
end
|
||||
|
||||
def more_menu_items
|
||||
@more_menu_items ||= [more_menu_subproject_item,
|
||||
more_menu_settings_item,
|
||||
more_menu_activity_item,
|
||||
more_menu_archive_item,
|
||||
more_menu_unarchive_item,
|
||||
more_menu_copy_item,
|
||||
more_menu_delete_item].compact
|
||||
end
|
||||
|
||||
def more_menu_subproject_item
|
||||
if User.current.allowed_in_project?(:add_subprojects, project)
|
||||
[t(:label_subproject_new),
|
||||
new_project_path(parent_id: project.id),
|
||||
{ class: 'icon-context icon-add',
|
||||
title: t(:label_subproject_new) }]
|
||||
end
|
||||
end
|
||||
|
||||
def more_menu_settings_item
|
||||
if User.current.allowed_in_project?({ controller: '/projects/settings/general', action: 'show', project_id: project.id },
|
||||
project)
|
||||
[t(:label_project_settings),
|
||||
project_settings_general_path(project),
|
||||
{ class: 'icon-context icon-settings',
|
||||
title: t(:label_project_settings) }]
|
||||
end
|
||||
end
|
||||
|
||||
def more_menu_activity_item
|
||||
if User.current.allowed_in_project?(:view_project_activity, project)
|
||||
[
|
||||
t(:label_project_activity),
|
||||
project_activity_index_path(project, event_types: ['project_attributes']),
|
||||
{ class: 'icon-context icon-checkmark',
|
||||
title: t(:label_project_activity) }
|
||||
]
|
||||
end
|
||||
end
|
||||
|
||||
def more_menu_archive_item
|
||||
if User.current.allowed_in_project?(:archive_project, project) && project.active?
|
||||
[t(:button_archive),
|
||||
project_archive_path(project, status: params[:status]),
|
||||
{ data: { confirm: t('project.archive.are_you_sure', name: project.name) },
|
||||
method: :post,
|
||||
class: 'icon-context icon-locked',
|
||||
title: t(:button_archive) }]
|
||||
end
|
||||
end
|
||||
|
||||
def more_menu_unarchive_item
|
||||
if User.current.admin? && project.archived? && (project.parent.nil? || project.parent.active?)
|
||||
[t(:button_unarchive),
|
||||
project_archive_path(project, status: params[:status]),
|
||||
{ method: :delete,
|
||||
class: 'icon-context icon-unlocked',
|
||||
title: t(:button_unarchive) }]
|
||||
end
|
||||
end
|
||||
|
||||
def more_menu_copy_item
|
||||
if User.current.allowed_in_project?(:copy_projects, project) && !project.archived?
|
||||
[t(:button_copy),
|
||||
copy_project_path(project),
|
||||
{ class: 'icon-context icon-copy',
|
||||
title: t(:button_copy) }]
|
||||
end
|
||||
end
|
||||
|
||||
def more_menu_delete_item
|
||||
if User.current.admin
|
||||
[t(:button_delete),
|
||||
confirm_destroy_project_path(project),
|
||||
{ class: 'icon-context icon-delete',
|
||||
title: t(:button_delete) }]
|
||||
end
|
||||
end
|
||||
|
||||
def user_can_view_project?
|
||||
User.current.allowed_in_project?(:view_project, project)
|
||||
end
|
||||
|
||||
@@ -62,7 +62,7 @@ module Projects
|
||||
def rows
|
||||
@rows ||= begin
|
||||
projects_enumerator = ->(model) { to_enum(:projects_with_levels_order_sensitive, model).to_a } # rubocop:disable Lint/ToEnumArguments
|
||||
helpers.instance_exec(model, &projects_enumerator)
|
||||
instance_exec(model, &projects_enumerator)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -79,13 +79,13 @@ module Projects
|
||||
end
|
||||
|
||||
def deactivate_class_on_lft_sort
|
||||
if helpers.sorted_by_lft?
|
||||
if sorted_by_lft?
|
||||
'spot-link_inactive'
|
||||
end
|
||||
end
|
||||
|
||||
def href_only_when_not_sort_lft
|
||||
unless helpers.sorted_by_lft?
|
||||
unless sorted_by_lft?
|
||||
projects_path(sortBy: JSON::dump([['lft', 'asc']]))
|
||||
end
|
||||
end
|
||||
@@ -156,5 +156,31 @@ module Projects
|
||||
.includes(:custom_values, :enabled_modules)
|
||||
.paginate(page: helpers.page_param(params), per_page: helpers.per_page_param(params))
|
||||
end
|
||||
|
||||
def projects_with_levels_order_sensitive(projects, &)
|
||||
if sorted_by_lft?
|
||||
Project.project_tree(projects, &)
|
||||
else
|
||||
projects_with_level(projects, &)
|
||||
end
|
||||
end
|
||||
|
||||
def projects_with_level(projects, &)
|
||||
ancestors = []
|
||||
|
||||
projects.each do |project|
|
||||
while !ancestors.empty? && !project.is_descendant_of?(ancestors.last)
|
||||
ancestors.pop
|
||||
end
|
||||
|
||||
yield project, ancestors.count
|
||||
|
||||
ancestors << project
|
||||
end
|
||||
end
|
||||
|
||||
def sorted_by_lft?
|
||||
query.orders.first.attribute == :lft
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -29,140 +29,6 @@
|
||||
module ProjectsHelper
|
||||
include WorkPackagesFilterHelper
|
||||
|
||||
def no_projects_result_box_params
|
||||
if User.current.allowed_globally?(:add_project)
|
||||
{ action_url: new_project_path, display_action: true }
|
||||
else
|
||||
{}
|
||||
end
|
||||
end
|
||||
|
||||
def project_more_menu_items(project)
|
||||
[project_more_menu_subproject_item(project),
|
||||
project_more_menu_settings_item(project),
|
||||
project_more_menu_activity_item(project),
|
||||
project_more_menu_archive_item(project),
|
||||
project_more_menu_unarchive_item(project),
|
||||
project_more_menu_copy_item(project),
|
||||
project_more_menu_delete_item(project)].compact
|
||||
end
|
||||
|
||||
def project_more_menu_subproject_item(project)
|
||||
if User.current.allowed_in_project?(:add_subprojects, project)
|
||||
[t(:label_subproject_new),
|
||||
new_project_path(parent_id: project.id),
|
||||
{ class: 'icon-context icon-add',
|
||||
title: t(:label_subproject_new) }]
|
||||
end
|
||||
end
|
||||
|
||||
def project_more_menu_settings_item(project)
|
||||
if User.current.allowed_in_project?({ controller: '/projects/settings/general', action: 'show', project_id: project.id },
|
||||
project)
|
||||
[t(:label_project_settings),
|
||||
project_settings_general_path(project),
|
||||
{ class: 'icon-context icon-settings',
|
||||
title: t(:label_project_settings) }]
|
||||
end
|
||||
end
|
||||
|
||||
def project_more_menu_activity_item(project)
|
||||
if User.current.allowed_in_project?(:view_project_activity, project)
|
||||
[
|
||||
t(:label_project_activity),
|
||||
project_activity_index_path(project, event_types: ['project_attributes']),
|
||||
{ class: 'icon-context icon-checkmark',
|
||||
title: t(:label_project_activity) }
|
||||
]
|
||||
end
|
||||
end
|
||||
|
||||
def project_more_menu_archive_item(project)
|
||||
if User.current.allowed_in_project?(:archive_project, project) && project.active?
|
||||
[t(:button_archive),
|
||||
project_archive_path(project, status: params[:status]),
|
||||
{ data: { confirm: t('project.archive.are_you_sure', name: project.name) },
|
||||
method: :post,
|
||||
class: 'icon-context icon-locked',
|
||||
title: t(:button_archive) }]
|
||||
end
|
||||
end
|
||||
|
||||
def project_more_menu_unarchive_item(project)
|
||||
if User.current.admin? && project.archived? && (project.parent.nil? || project.parent.active?)
|
||||
[t(:button_unarchive),
|
||||
project_archive_path(project, status: params[:status]),
|
||||
{ method: :delete,
|
||||
class: 'icon-context icon-unlocked',
|
||||
title: t(:button_unarchive) }]
|
||||
end
|
||||
end
|
||||
|
||||
def project_more_menu_copy_item(project)
|
||||
if User.current.allowed_in_project?(:copy_projects, project) && !project.archived?
|
||||
[t(:button_copy),
|
||||
copy_project_path(project),
|
||||
{ class: 'icon-context icon-copy',
|
||||
title: t(:button_copy) }]
|
||||
end
|
||||
end
|
||||
|
||||
def project_more_menu_delete_item(project)
|
||||
if User.current.admin
|
||||
[t(:button_delete),
|
||||
confirm_destroy_project_path(project),
|
||||
{ class: 'icon-context icon-delete',
|
||||
title: t(:button_delete) }]
|
||||
end
|
||||
end
|
||||
|
||||
def project_options_for_status(project)
|
||||
contract = if project.new_record?
|
||||
Projects::CreateContract
|
||||
else
|
||||
Projects::UpdateContract
|
||||
end
|
||||
|
||||
contract
|
||||
.new(project, current_user)
|
||||
.assignable_status_codes
|
||||
.map do |code|
|
||||
[I18n.t("activerecord.attributes.project.status_codes.#{code}"), code]
|
||||
end
|
||||
end
|
||||
|
||||
def project_options_for_templated
|
||||
::Projects::InstantiateTemplateContract
|
||||
.visible_templates(current_user)
|
||||
.pluck(:name, :id)
|
||||
end
|
||||
|
||||
def shorten_text(text, length)
|
||||
text.to_s.gsub(/\A(.{#{length}[^\n\r]*).*\z/m, '\1...').strip
|
||||
end
|
||||
|
||||
def projects_with_level(projects)
|
||||
ancestors = []
|
||||
|
||||
projects.each do |project|
|
||||
while !ancestors.empty? && !project.is_descendant_of?(ancestors.last)
|
||||
ancestors.pop
|
||||
end
|
||||
|
||||
yield project, ancestors.count
|
||||
|
||||
ancestors << project
|
||||
end
|
||||
end
|
||||
|
||||
def projects_with_levels_order_sensitive(projects, &)
|
||||
if sorted_by_lft?
|
||||
project_tree(projects, &)
|
||||
else
|
||||
projects_with_level(projects, &)
|
||||
end
|
||||
end
|
||||
|
||||
# Just like sort_header tag but removes sorting by
|
||||
# lft from the sort criteria as lft is mutually exclusive with
|
||||
# the other criteria.
|
||||
@@ -176,19 +42,6 @@ module ProjectsHelper
|
||||
@sort_criteria.criteria = former_criteria
|
||||
end
|
||||
|
||||
def sorted_by_lft?
|
||||
@sort_criteria.first_key == 'lft'
|
||||
end
|
||||
|
||||
def allowed_parent_projects(project)
|
||||
if project.persisted?
|
||||
Projects::UpdateContract
|
||||
else
|
||||
Projects::CreateContract
|
||||
end.new(project, current_user)
|
||||
.assignable_parents
|
||||
end
|
||||
|
||||
def short_project_description(project, length = 255)
|
||||
unless project.description.present?
|
||||
return ''
|
||||
|
||||
Reference in New Issue
Block a user