Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude text custom fields from sorting #15539

Merged
merged 3 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/components/queries/sort_by_field_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<% flex.with_column(flex: 1) do %>
<%#- We are just using the classes of the primer component here, because when using the primer component, we cannot detach the input element from the form %>
<%#- The form="none" adds the input to a nonexistant form (as we do not have one with the ID="none" and thus the fields to not get appended to the query string %>
<%= select_tag 'sort_field', select_options, include_blank: true, form: "none", class: "FormControl-select FormControl-medium FormControl--fullWidth", data: { action: "change->sort-by-config#fieldChanged"} %>
<%= select_tag 'sort_field', select_options, prompt: "-", form: "none", class: "FormControl-select FormControl-medium FormControl--fullWidth", data: { action: "change->sort-by-config#fieldChanged"} %>
<% end %>
<% flex.with_column do %>
<%= render(Primer::Alpha::SegmentedControl.new("aria-label": "Sort order", hide_labels: true, ml: 3)) do |sort_buttons| %>
Expand Down
16 changes: 13 additions & 3 deletions app/models/queries/projects/orders/custom_field_order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,27 @@
class Queries::Projects::Orders::CustomFieldOrder < Queries::Orders::Base
self.model = Project.all

EXCLUDED_CUSTOM_FIELD_TYPES = %w(text)
KEY_FORMAT = /cf_(\d+)/

validates :custom_field, presence: { message: I18n.t(:"activerecord.errors.messages.does_not_exist") }

def self.key
/cf_(\d+)/
valid_ids = RequestStore.fetch(:custom_sortable_project_custom_fields) do
ProjectCustomField.where.not(field_format: EXCLUDED_CUSTOM_FIELD_TYPES).visible.pluck(:id).join("|")
end

/cf_(#{valid_ids})/
Dismissed Show dismissed Hide dismissed
end

def custom_field
@custom_field ||= begin
id = self.class.key.match(attribute)[1]
id = KEY_FORMAT.match(attribute)[1]

ProjectCustomField.visible.find_by_id(id)
ProjectCustomField
.where.not(field_format: EXCLUDED_CUSTOM_FIELD_TYPES)
.visible
.find_by(id:)
end
end

Expand Down
44 changes: 44 additions & 0 deletions spec/models/queries/projects/orders/custom_field_order_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2012-2024 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.
#++

require "spec_helper"

RSpec.describe Queries::Projects::Orders::CustomFieldOrder do
let!(:cf_text) { FactoryBot.create(:text_project_custom_field) }
let!(:cf_int) { FactoryBot.create(:integer_project_custom_field) }

it "does not allow to sort by the text field" do
cf = described_class.new("cf_#{cf_text.id}")
expect(cf).not_to be_available
end

it "allows to sort by all other fields" do
cf = described_class.new("cf_#{cf_int.id}")
expect(cf).to be_available
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,15 @@

RSpec.describe Queries::Projects::ProjectQueries::SetAttributesService, type: :model do
let(:current_user) { build_stubbed(:user) }
let!(:custom_field) do
build_stubbed(:project_custom_field, id: 1) do |cf|
scope = instance_double(ActiveRecord::Relation)

allow(ProjectCustomField)
.to receive(:visible)
.and_return(scope)

allow(scope)
.to receive(:find_by)
.with(id: cf.id.to_s)
.and_return(cf)
end
end

let(:contract_instance) do
contract = instance_double(Queries::Projects::ProjectQueries::CreateContract)
allow(contract)
.to receive_messages(validate: contract_valid, errors: contract_errors)
contract
end

let(:contract_errors) { instance_double(ActiveModel::Errors) }
let(:contract_valid) { true }
let(:model_valid) { true }

let(:instance) do
described_class.new(user: current_user,
model: model_instance,
Expand All @@ -70,13 +53,25 @@

Queries::Projects::ProjectQueries::CreateContract
end

let(:params) { {} }
let!(:custom_field) do
build_stubbed(:project_custom_field, id: 1) do |cf|
scope = instance_double(ActiveRecord::Relation)

allow(ProjectCustomField)
.to receive(:visible)
.and_return(scope)

allow(scope)
.to receive(:find_by)
.with(id: cf.id.to_s)
.and_return(cf)
end
end

before do
allow(model_instance)
.to receive(:valid?)
.and_return(model_valid)
RequestStore.store[:custom_sortable_project_custom_fields] = "1"
allow(model_instance).to receive(:valid?).and_return(model_valid)
end

subject { instance.call(params) }
Expand Down Expand Up @@ -190,7 +185,7 @@
subject

expect(model_instance.selects.map(&:attribute))
.to eql [:favored, :name]
.to eql %i[favored name]
end
end

Expand Down
Loading