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
fix project query save as that was not saving non modified attributes
Ivan Kuchine9334c4bb80a
diff --git a/app/components/projects/index_page_header_component.html.erb b/app/components/projects/index_page_header_component.html.erbindex bb23b7b4db6..3ae1c936da3 100644--- a/app/components/projects/index_page_header_component.html.erb+++ b/app/components/projects/index_page_header_component.html.erb@@ -97,7 +97,7 @@data: {controller: "params-from-query",'application-target': "dynamic",- 'params-from-query-allowed-value': '["filters", "columns"]'+ 'params-from-query-allowed-value': '["filters", "columns", "query_id"]'},id: 'project-save-form') do |f|render(Queries::Projects::Create.new(f))diff --git a/app/components/projects/index_page_header_component.rb b/app/components/projects/index_page_header_component.rbindex c6c9daf1ba0..e3aac674f41 100644--- a/app/components/projects/index_page_header_component.rb+++ b/app/components/projects/index_page_header_component.rb@@ -110,7 +110,7 @@ class Projects::IndexPageHeaderComponent < ApplicationComponentmethod:,controller: "params-from-query",'application-target': "dynamic",- 'params-from-query-allowed-value': '["filters", "columns"]'+ 'params-from-query-allowed-value': '["filters", "columns", "query_id"]'}.compact) dorender(Primer::Beta::Octicon.new(icon: "op-save",align_self: :center,@@ -129,7 +129,7 @@ class Projects::IndexPageHeaderComponent < ApplicationComponentmethod:,controller: "params-from-query",'application-target': "dynamic",- 'params-from-query-allowed-value': '["filters", "columns"]'+ 'params-from-query-allowed-value': '["filters", "columns", "query_id"]'}.compact}) do |item|diff --git a/app/controllers/projects/queries_controller.rb b/app/controllers/projects/queries_controller.rbindex 2f09edc65c6..a3a62aa642f 100644--- a/app/controllers/projects/queries_controller.rb+++ b/app/controllers/projects/queries_controller.rb@@ -32,7 +32,7 @@ class Projects::QueriesController < ApplicationController# No need for a more specific authorization check. That is carried out in the contracts.before_action :require_loginbefore_action :find_query, only: %i[update destroy]- before_action :load_query_or_deny_access, only: %i[new]+ before_action :build_query_or_deny_access, only: %i[new create]current_menu_item [:new, :create] do:projects@@ -46,7 +46,7 @@ class Projects::QueriesController < ApplicationControllerdef createcall = Queries::Projects::ProjectQueries::CreateService- .new(user: current_user)+ .new(from: @query, user: current_user).call(permitted_query_params)if call.success?diff --git a/app/controllers/projects/query_loading.rb b/app/controllers/projects/query_loading.rbindex 071f41318b5..1d0edfa44ea 100644--- a/app/controllers/projects/query_loading.rb+++ b/app/controllers/projects/query_loading.rb@@ -29,23 +29,31 @@ module Projectsmodule QueryLoadingprivate+ def load_query(duplicate:)+ Queries::Projects::Factory.find(params[:query_id],+ params: permitted_query_params,+ user: current_user,+ duplicate:)+ end+def load_query_or_deny_access- @query = Queries::Projects::Factory.find(params[:query_id],- params: permitted_query_params,- user: current_user)+ @query = load_query(duplicate: false)++ render_403 unless @query+ end++ def build_query_or_deny_access+ @query = load_query(duplicate: true)render_403 unless @queryenddef permitted_query_params- query_params = if params[:query]- params- .require(:query)- .permit(:name)- .to_h- else- {}- end+ query_params = {}++ if params[:query]+ query_params.merge!(params.require(:query).permit(:name))+ endquery_params.merge!(Queries::ParamsParser.parse(params))diff --git a/app/models/queries/projects/factory.rb b/app/models/queries/projects/factory.rbindex c408f3fe368..52f4989c130 100644--- a/app/models/queries/projects/factory.rb+++ b/app/models/queries/projects/factory.rb@@ -36,8 +36,9 @@ class Queries::Projects::FactorySTATIC_AT_RISK = "at_risk".freezeclass << self- def find(id, params:, user:)- query = find_static_query_and_set_attributes(id, params, user) || find_persisted_query_and_set_attributes(id, params, user)+ def find(id, params:, user:, duplicate: false)+ query = find_static_query_and_set_attributes(id, params, user, duplicate:) ||+ find_persisted_query_and_set_attributes(id, params, user, duplicate:)query&.valid_subset!query@@ -118,23 +119,27 @@ class Queries::Projects::Factoryendend- def find_static_query_and_set_attributes(id, params, user)+ def find_static_query_and_set_attributes(id, params, user, duplicate:)query = static_query(id)return unless query+ query = duplicate_query(query) if duplicate || params.any?+if params.any?- new_query(query, params, user)+ set_query_attributes(query, params, user)elsequeryendend- def find_persisted_query_and_set_attributes(id, params, user)+ def find_persisted_query_and_set_attributes(id, params, user, duplicate:)query = Queries::Projects::ProjectQuery.where(user:).find_by(id:)return unless query+ query = duplicate_query(query) if duplicate+if params.any?set_query_attributes(query, params, user)else@@ -142,10 +147,8 @@ class Queries::Projects::Factoryendend- def new_query(source_query, params, user)- set_query_attributes(Queries::Projects::ProjectQuery.new(source_query.attributes.slice("filters", "orders", "selects")),- params,- user)+ def duplicate_query(query)+ Queries::Projects::ProjectQuery.new(query.attributes.slice("filters", "orders", "selects"))enddef set_query_attributes(query, params, user)diff --git a/app/services/queries/projects/project_queries/create_service.rb b/app/services/queries/projects/project_queries/create_service.rbindex d346bda2fa4..0aa6deea366 100644--- a/app/services/queries/projects/project_queries/create_service.rb+++ b/app/services/queries/projects/project_queries/create_service.rb@@ -26,4 +26,13 @@# See COPYRIGHT and LICENSE files for more details.# ++-class Queries::Projects::ProjectQueries::CreateService < BaseServices::Create; end+class Queries::Projects::ProjectQueries::CreateService < BaseServices::Create+ def initialize(from: nil, **)+ @from = from+ super(**)+ end++ def instance(_params)+ @from || super+ end+enddiff --git a/spec/services/queries/projects/project_queries/create_service_spec.rb b/spec/services/queries/projects/project_queries/create_service_spec.rbindex f3eca52e5fe..d33f19686d4 100644--- a/spec/services/queries/projects/project_queries/create_service_spec.rb+++ b/spec/services/queries/projects/project_queries/create_service_spec.rb@@ -33,4 +33,47 @@ RSpec.describe Queries::Projects::ProjectQueries::CreateService, type: :model doit_behaves_like "BaseServices create service" dolet(:factory) { :project_query }end++ describe "overriding the instance" do+ subject(:result) { described_class.new(from: instance, user:).call(params).result }++ let(:user) { build(:user) }+ let(:params) { { filters: [{ attribute: "active", operator: "=", values: ["f"] }] } }++ context "when overriding initial instance" do+ let(:instance) { build(:project_query).where("public", "=", "t").order(name: :desc) }++ it "returns the instance" do+ expect(result).to eq(instance)+ end++ it "keeps instance value for attribute not passed in params" do+ expect(result).to have_attributes(+ orders: [having_attributes(attribute: :name, direction: :desc)]+ )+ end++ it "sets value for attribute passed in params" do+ expect(result).to have_attributes(+ filters: [having_attributes(name: :active, operator: "=", values: %w[f])]+ )+ end+ end++ context "when not overriding initial instance" do+ let(:instance) { nil }++ it "uses default value for attribute not passed in params" do+ expect(result).to have_attributes(+ orders: [having_attributes(attribute: :lft, direction: :asc)]+ )+ end++ it "sets value for attribute passed in params" do+ expect(result).to have_attributes(+ filters: [having_attributes(name: :active, operator: "=", values: %w[f])]+ )+ end+ end+ endend