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

extract project column options into helper method

ulferts963fd34dbc35
diff --git a/app/helpers/projects_helper.rb b/app/helpers/projects_helper.rb
index 73e6a34cdee..fb747a281c2 100644
--- a/app/helpers/projects_helper.rb
+++ b/app/helpers/projects_helper.rb
@@ -43,10 +43,22 @@ module ProjectsHelper
end
def short_project_description(project, length = 255)
- unless project.description.present?
+ if project.description.blank?
return ''
end
project.description.gsub(/\A(.{#{length}}[^\n\r]*).*\z/m, '\1...').strip
end
+
+ def enabled_projects_columns_options
+ ::Queries::Projects::ProjectQuery
+ .new.available_selects
+ .sort_by(&:caption)
+ .map do |s|
+ [s.caption,
+ s.attribute.to_s,
+ { checked: Setting.enabled_projects_columns.include?(s.attribute.to_s) || s.attribute == :name,
+ disabled: s.attribute == :name }]
+ end
+ end
end
diff --git a/app/models/queries/projects/selects/custom_field.rb b/app/models/queries/projects/selects/custom_field.rb
index 4e10c2a6ca0..e0077aa1495 100644
--- a/app/models/queries/projects/selects/custom_field.rb
+++ b/app/models/queries/projects/selects/custom_field.rb
@@ -43,7 +43,7 @@ class Queries::Projects::Selects::CustomField < Queries::Selects::Base
ProjectCustomField
.visible
.pluck(:id)
- .map { |cf_id| new("cf_#{cf_id}") }
+ .map { |cf_id| new(:"cf_#{cf_id}") }
end
def caption
diff --git a/app/views/admin/settings/projects_settings/show.html.erb b/app/views/admin/settings/projects_settings/show.html.erb
index 8fd4977baab..becc15d34f6 100644
--- a/app/views/admin/settings/projects_settings/show.html.erb
+++ b/app/views/admin/settings/projects_settings/show.html.erb
@@ -47,14 +47,6 @@ See COPYRIGHT and LICENSE files for more details.
<section class="form--section">
<fieldset class="form--fieldset">
<legend class="form--fieldset-legend"><%= t('settings.projects.section_project_overview') %></legend>
- <%
- column_choices = ::Projects::TableComponent
- .new(current_user: current_user)
- .all_columns
- .reject { |_, options| !options[:caption] }
- .map { |name, options| [options[:caption], name.to_s, checked: options[:builtin], disabled: options[:builtin]] }
- %>
-
<% unless EnterpriseToken.allows_to?(:custom_fields_in_projects_list) %>
<div class="form--field">
<%=
@@ -67,9 +59,9 @@ See COPYRIGHT and LICENSE files for more details.
%>
</div>
<% end %>
-
+
<div class="form--field">
- <%= setting_multiselect(:enabled_projects_columns, column_choices) %>
+ <%= setting_multiselect(:enabled_projects_columns, enabled_projects_columns_options) %>
</div>
<div class="form--field">
diff --git a/spec/helpers/projects_helper_spec.rb b/spec/helpers/projects_helper_spec.rb
index bcdcdb45fcc..e360444e5b8 100644
--- a/spec/helpers/projects_helper_spec.rb
+++ b/spec/helpers/projects_helper_spec.rb
@@ -32,76 +32,6 @@ RSpec.describe ProjectsHelper do
include ApplicationHelper
include ProjectsHelper
- describe '#projects_with_level' do
- let(:root) do
- stub_descendant_of
- end
- let(:child1) { stub_descendant_of(root) }
- let(:grandchild1) { stub_descendant_of(root, child1) }
- let(:grandchild2) { stub_descendant_of(root, child1) }
- let(:grandgrandchild1) { stub_descendant_of(root, child1, grandchild2) }
- let(:child2) { stub_descendant_of(root) }
-
- def stub_descendant_of(*ancestors)
- wp = build_stubbed(:project)
-
- allow(wp)
- .to receive(:is_descendant_of?)
- .and_return(false)
-
- ancestors.each do |ancestor|
- allow(wp)
- .to receive(:is_descendant_of?)
- .with(ancestor)
- .and_return(true)
- end
-
- wp
- end
-
- context 'when ordered by hierarchy' do
- let(:projects) do
- [root,
- child1,
- grandchild1,
- grandchild2,
- grandgrandchild1,
- child2]
- end
-
- it 'returns the projects in the provided order with the appropriate levels' do
- expect { |b| helper.projects_with_level(projects, &b) }
- .to yield_successive_args [root, 0],
- [child1, 1],
- [grandchild1, 2],
- [grandchild2, 2],
- [grandgrandchild1, 3],
- [child2, 1]
- end
- end
-
- context 'when ordered by arbitrarily' do
- let(:projects) do
- [grandchild1,
- child1,
- grandchild2,
- grandgrandchild1,
- child2,
- root]
- end
-
- it 'returns the projects in the provided order with the appropriate levels' do
- expect { |b| helper.projects_with_level(projects, &b) }
- .to yield_successive_args [grandchild1, 0],
- [child1, 0],
- [grandchild2, 1],
- [grandgrandchild1, 2],
- [child2, 0],
- [root, 0]
- end
- end
- end
-
describe '#short_project_description' do
let(:project) { build_stubbed(:project, description: (('Abcd ' * 5) + "\n") * 11) }
@@ -111,63 +41,30 @@ RSpec.describe ProjectsHelper do
end
end
- describe '#project_more_menu_items' do
- # need to use refind: true because @allowed_permissions is cached in the instance
- shared_let(:project, refind: true) { create(:project) }
- shared_let(:current_user) { create(:user, member_with_permissions: { project => %i[view_work_packages edit_work_packages] }) }
-
- subject(:menu) do
- items = project_more_menu_items(project)
- # each item is a [label, href, **link_to_options]
- items.pluck(0)
+ describe '#enabled_projects_columns_options', with_settings: { enabled_projects_columns: %w[name project_status] } do
+ let(:selects) do
+ [
+ Queries::Projects::Selects::Default.new(:name),
+ Queries::Projects::Selects::Default.new(:description),
+ Queries::Projects::Selects::Status.new(:project_status)
+ ]
end
before do
- allow(User).to receive(:current).and_return(current_user)
- end
-
- # "Archive project" menu entry
-
- context 'when current user is admin' do
- before do
- current_user.update(admin: true)
- end
-
- it { is_expected.to include(t(:button_archive)) }
- end
-
- context 'when current user has archive_project permission' do
- before do
- current_user.roles(project).first.add_permission!(:archive_project)
- end
-
- it { is_expected.to include(t(:button_archive)) }
- end
-
- context 'when current user does not have archive_project permission' do
- it { is_expected.not_to include(t(:button_archive)) }
- end
-
- context 'when project is archived' do
- before do
- project.update(active: false)
- end
-
- it { is_expected.not_to include(t(:button_archive)) }
- end
-
- # "Project activity" menu entry
-
- context 'when project does not have activity module enabled' do
- before do
- project.enabled_module_names -= ['activity']
- end
+ query_instance = instance_double(Queries::Projects::ProjectQuery, available_selects: selects)
- it { is_expected.not_to include(t(:label_project_activity)) }
+ allow(Queries::Projects::ProjectQuery)
+ .to receive(:new)
+ .and_return(query_instance)
end
- context 'when project has activity module enabled' do
- it { is_expected.to include(t(:label_project_activity)) }
+ it 'returns the columns options' do
+ expect(helper.enabled_projects_columns_options)
+ .to eql([
+ ['Description', 'description', { checked: false, disabled: false }],
+ ['Name', 'name', { checked: true, disabled: true }],
+ ['Status', 'project_status', { checked: true, disabled: false }]
+ ])
end
end
end
diff --git a/spec/models/queries/projects/project_query_spec.rb b/spec/models/queries/projects/project_query_spec.rb
index 3327b219763..47bcd25832d 100644
--- a/spec/models/queries/projects/project_query_spec.rb
+++ b/spec/models/queries/projects/project_query_spec.rb
@@ -93,35 +93,60 @@ RSpec.describe Queries::Projects::ProjectQuery do
describe '.available_selects' do
current_user { user }
+ before do
+ scope = instance_double(ActiveRecord::Relation)
+
+ allow(ProjectCustomField)
+ .to receive(:visible)
+ .and_return(scope)
+
+ allow(scope)
+ .to receive(:pluck)
+ .and_return([23, 42])
+ end
+
it 'lists registered selects' do
- expect(instance.available_selects)
- .to contain_exactly(Queries::Projects::Selects::Default)
+ expect(instance.available_selects.map(&:attribute))
+ .to contain_exactly(:name,
+ :public,
+ :description,
+ :hierarchy,
+ :project_status,
+ :status_explanation)
end
context 'with the user being admin' do
current_user { admin }
it 'includes admin columns' do
- expect(instance.available_selects)
- .to contain_exactly(Queries::Projects::Selects::Default,
- Queries::Projects::Selects::Admin)
+ expect(instance.available_selects.map(&:attribute))
+ .to contain_exactly(:name,
+ :public,
+ :description,
+ :hierarchy,
+ :project_status,
+ :status_explanation,
+ :created_at,
+ :latest_activity_at,
+ :required_disk_space)
end
end
context 'with an enterprise token',
with_ee: %i[custom_fields_in_projects_list] do
+ # rubocop:disable Naming/VariableNumber
it 'includes custom field columns' do
- expect(instance.available_selects)
- .to contain_exactly(Queries::Projects::Selects::Default,
- Queries::Projects::Selects::CustomField)
+ expect(instance.available_selects.map(&:attribute))
+ .to contain_exactly(:name,
+ :public,
+ :description,
+ :hierarchy,
+ :project_status,
+ :status_explanation,
+ :cf_23,
+ :cf_42)
end
- end
- end
-
- describe '.available_select_names' do
- it 'lists registered selects' do
- expect(instance.available_select_keys)
- .to include(:name, :public)
+ # rubocop:enable Naming/VariableNumber
end
end
end
diff --git a/spec/models/query_spec.rb b/spec/models/query_spec.rb
index 12bed04655b..5039fc106a7 100644
--- a/spec/models/query_spec.rb
+++ b/spec/models/query_spec.rb
@@ -262,7 +262,7 @@ RSpec.describe Query,
no_highlight: {}
}
- allow(Queries::WorkPackages::Selects::PropertySelect).to receive(:property_columns)
+ allow(Queries::WorkPackages::Selects::PropertySelect).to receive(:property_selects)
.and_return(available_columns)
expect(query.available_highlighting_columns.map(&:name)).to eq(%i{highlightable1 highlightable2})