Просмотр исходного кода

Maintenance: Simplify handling of models_searchable.

This drops the config setting `models_searchable` and instead
injects the same value into the configuration of the desktop client.
By that, code is cleaner and we avoid an initializer that writes to the
database during Rails startup.
Martin Gruner 1 год назад
Родитель
Сommit
54be02f673

+ 1 - 0
.gitlab/ci/test/api_client.yml

@@ -21,6 +21,7 @@ api_client:php:
     ZAMMAD_PHP_API_CLIENT_UNIT_TESTS_URL: "http://localhost:3000"
     ZAMMAD_PHP_API_CLIENT_UNIT_TESTS_USERNAME: "admin@example.com"
     ZAMMAD_PHP_API_CLIENT_UNIT_TESTS_PASSWORD: "test"
+    ZAMMAD_PHP_API_CLIENT_UNIT_TESTS_DEBUG: 0
   script:
     - RAILS_ENV=test bundle exec rake db:create
     - echo -e "\\e[0Ksection_start:`date +%s`:zammad_ci_test_start[collapsed=true]\\r\\e[0Kbundle exec rake zammad:ci:test:start"

+ 3 - 3
app/controllers/search_controller.rb

@@ -17,16 +17,16 @@ class SearchController < ApplicationController
     # convert objects string into array of class names
     # e.g. user-ticket-another_object = %w( User Ticket AnotherObject )
     objects = if params[:objects]
-                params[:objects].split('-').map(&:camelize)
+                params[:objects].split('-').map(&:camelize).map(&:constantize)
               else
-                Setting.get('models_searchable')
+                Models.searchable
               end
 
     assets = {}
     result = []
     Service::Search.new(current_user: current_user).execute(
       term:    query,
-      objects: objects.map(&:constantize),
+      objects: objects,
       options: { limit: limit, ids: params[:ids] },
     ).each do |item|
       assets = item.assets(assets)

+ 3 - 0
app/controllers/sessions_controller.rb

@@ -303,6 +303,9 @@ class SessionsController < ApplicationController
     # https://github.com/zammad/zammad/issues/4263
     config['auth_saml_display_name'] = Setting.get('auth_saml_credentials')[:display_name]
 
+    # Announce searchable models to the front end.
+    config['models_searchable'] = Models.searchable.map(&:to_s)
+
     # remember if we can switch back to user
     if session[:switched_from_user_id]
       config['switch_back_to_possible'] = true

+ 3 - 5
config/initializers/models_searchable.rb → config/initializers/models_preload.rb

@@ -1,12 +1,10 @@
 # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
 
-# update settings for searchable models
-
+# Ensure all models are preloaded, as Zammad uses reflections
+#   which rely on all model classes being present.
 Rails.application.reloader.to_prepare do
   begin
-    next if !Setting.exists?(name: 'models_searchable')
-
-    Setting.set('models_searchable', Models.searchable.map(&:to_s))
+    Models.all
   rescue ActiveRecord::StatementInvalid
     nil
   end

+ 10 - 0
db/migrate/20230622095629_drop_models_searchable_setting.rb

@@ -0,0 +1,10 @@
+# Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
+
+class DropModelsSearchableSetting < ActiveRecord::Migration[6.1]
+  def change
+    # return if it's a new setup
+    return if !Setting.exists?(name: 'system_init_done')
+
+    Setting.find_by(name: 'models_searchable').destroy
+  end
+end

+ 0 - 13
db/seeds/settings.rb

@@ -3352,19 +3352,6 @@ Setting.create_if_not_exists(
   frontend:    true
 )
 
-Setting.create_if_not_exists(
-  title:       __('Defines searchable models.'),
-  name:        'models_searchable',
-  area:        'Models::Base',
-  description: __('Defines the searchable models.'),
-  options:     {},
-  state:       Models.searchable.map(&:to_s),
-  preferences: {
-    authentication: true,
-  },
-  frontend:    true,
-)
-
 Setting.create_if_not_exists(
   title:       __('Default Screen'),
   name:        'default_controller',

+ 0 - 8
i18n/zammad.pot

@@ -3613,10 +3613,6 @@ msgstr ""
 msgid "Defines processing timeout for the html sanitizer."
 msgstr ""
 
-#: db/seeds/settings.rb
-msgid "Defines searchable models."
-msgstr ""
-
 #: db/seeds/settings.rb
 msgid "Defines sync transaction backend."
 msgstr ""
@@ -3761,10 +3757,6 @@ msgstr ""
 msgid "Defines the random application secret."
 msgstr ""
 
-#: db/seeds/settings.rb
-msgid "Defines the searchable models."
-msgstr ""
-
 #: db/seeds/settings.rb
 msgid "Defines the sender email address of Icinga emails."
 msgstr ""