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

Link to query by reference in type form configuration

Christophe Bliardbae803c485af
diff --git a/app/seeders/basic_data/type_seeder.rb b/app/seeders/basic_data/type_seeder.rb
index 730f0f92d21..0694c777c94 100644
--- a/app/seeders/basic_data/type_seeder.rb
+++ b/app/seeders/basic_data/type_seeder.rb
@@ -77,8 +77,8 @@ module BasicData
type_data['form_configuration'].each do |form_config_attr|
groups = type.default_attribute_groups
- query = find_query_by_name(form_config_attr['query_name'])
- query_association = "query_#{query}"
+ query = seed_data.find_reference(form_config_attr['query'])
+ query_association = "query_#{query.id}"
groups.unshift([form_config_attr['group_name'], [query_association.to_sym]])
type.attribute_groups = groups
@@ -93,9 +93,5 @@ module BasicData
types_data = seed_data.lookup('type_configuration') || []
types_data.find { |entry| I18n.t(entry['type']) == type.name }
end
-
- def find_query_by_name(name)
- Query.find_by(name:).id
- end
end
end
diff --git a/app/seeders/demo_data/query_builder.rb b/app/seeders/demo_data/query_builder.rb
index 426c1192c34..08de14c86c0 100644
--- a/app/seeders/demo_data/query_builder.rb
+++ b/app/seeders/demo_data/query_builder.rb
@@ -29,15 +29,18 @@ module DemoData
attr_reader :config, :project, :user, :seed_data
def initialize(config, project:, user:, seed_data:)
- super()
+ super(seed_data)
@config = config.with_indifferent_access
@project = project
@user = user
- @seed_data = seed_data
end
def create!
- create_query if valid?
+ return unless valid?
+
+ create_query.tap do |query|
+ seed_data.store_reference(config['reference'], query)
+ end
end
private
diff --git a/modules/bim/app/seeders/bim.yml b/modules/bim/app/seeders/bim.yml
index fbeea58cf4d..0fcae6efb80 100644
--- a/modules/bim/app/seeders/bim.yml
+++ b/modules/bim/app/seeders/bim.yml
@@ -28,6 +28,7 @@
global_queries:
- t_name: "Embedded table: Children"
+ reference: :global_query__children
parent: '{id}'
timeline: false
sort_by: id
@@ -44,9 +45,8 @@ global_queries:
type_configuration:
- type: :default_type_phase
form_configuration:
- # TODO: should they be translated? Or are they refs?
- - group_name: "Children"
- query_name: "Embedded table: Children"
+ - t_group_name: "Children"
+ query: :global_query__children
groups:
- t_name: Architects
- t_name: BIM Coordinators
diff --git a/spec/seeders/basic_data/type_seeder_spec.rb b/spec/seeders/basic_data/type_seeder_spec.rb
new file mode 100644
index 00000000000..9c445818e2b
--- /dev/null
+++ b/spec/seeders/basic_data/type_seeder_spec.rb
@@ -0,0 +1,81 @@
+# 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.
+#++
+
+require 'spec_helper'
+
+RSpec.describe BasicData::TypeSeeder do
+ subject(:seeder) { described_class.new(seed_data) }
+
+ let(:phase_type) { create(:type, name: I18n.t(:default_type_phase)) }
+ let(:seed_data) { SeedData.new(data_hash) }
+
+ before do
+ phase_type # create the Phase type
+ end
+
+ describe '#set_attribute_groups_for_type' do
+ let(:data_hash) { {} }
+
+ context 'without any form_configuration for the given type' do
+ it 'does not change attribute_groups' do
+ attribute_groups_before = phase_type.attribute_groups.dup
+ seeder.set_attribute_groups_for_type(phase_type)
+ attribute_groups_now = phase_type.attribute_groups
+ expect(attribute_groups_now).to eq(attribute_groups_before)
+ end
+ end
+
+ context 'with a form_configuration entry in type_configuration in seed data' do
+ let(:data_hash) do
+ YAML.load <<~SEEDING_DATA_YAML
+ type_configuration:
+ - type: :default_type_phase
+ form_configuration:
+ - group_name: "Children"
+ query: :query__children
+ SEEDING_DATA_YAML
+ end
+ let(:query) { create(:query) }
+
+ before do
+ seed_data.store_reference(:query__children, query)
+ end
+
+ it 'adds a query group in the form configuration of the type' do
+ attribute_groups_before = phase_type.attribute_groups.dup
+ seeder.set_attribute_groups_for_type(phase_type)
+ attribute_groups_now = phase_type.attribute_groups
+ expect(attribute_groups_now).not_to eq(attribute_groups_before)
+ expect(attribute_groups_now)
+ .to include(an_instance_of(Type::QueryGroup).and(having_attributes(attributes: query)))
+ end
+ end
+ end
+end
diff --git a/spec/seeders/demo_data/global_query_seeder_spec.rb b/spec/seeders/demo_data/global_query_seeder_spec.rb
new file mode 100644
index 00000000000..b478dc6378f
--- /dev/null
+++ b/spec/seeders/demo_data/global_query_seeder_spec.rb
@@ -0,0 +1,74 @@
+# 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.
+#++
+
+require 'spec_helper'
+
+RSpec.describe DemoData::GlobalQuerySeeder do
+ subject(:seeder) { described_class.new(seed_data) }
+
+ let(:seed_data) { SeedData.new(data_hash) }
+
+ before do
+ AdminUserSeeder.new(seed_data).seed!
+ end
+
+ context 'with a global_queries defined' do
+ let(:data_hash) do
+ YAML.load <<~SEEDING_DATA_YAML
+ global_queries:
+ - name: "Children"
+ reference: :global_query__children
+ parent: '{id}'
+ timeline: false
+ sort_by: id
+ hidden: true
+ public: false
+ columns:
+ - type
+ - id
+ - subject
+ - status
+ - assigned_to
+ - priority
+ - project
+ SEEDING_DATA_YAML
+ end
+
+ it 'creates a global query' do
+ expect { seeder.seed! }.to change { Query.global.count }.by(1)
+ end
+
+ it 'references the query in the seed data' do
+ seeder.seed!
+ created_query = Query.global.first
+ expect(seed_data.find_reference(:global_query__children)).to eq(created_query)
+ end
+ end
+end
diff --git a/spec/seeders/root_seeder_standard_edition_spec.rb b/spec/seeders/root_seeder_standard_edition_spec.rb
index 71fec2d965c..e8b8b25d58e 100644
--- a/spec/seeders/root_seeder_standard_edition_spec.rb
+++ b/spec/seeders/root_seeder_standard_edition_spec.rb
@@ -73,6 +73,14 @@ describe RootSeeder,
)
end
+ it 'creates different types of queries' do
+ count_by_type = View.group(:type).count
+ expect(count_by_type).to eq(
+ "work_packages_table" => 7,
+ "team_planner" => 1
+ )
+ end
+
include_examples 'no email deliveries'
context 'when run a second time' do