diff --git a/app/controllers/search_controller.rb b/app/controllers/search_controller.rb index 7e0025692..0fa3af35c 100644 --- a/app/controllers/search_controller.rb +++ b/app/controllers/search_controller.rb @@ -21,7 +21,7 @@ class SearchController < ApplicationController # convert objects string into array of class names # e.g. user-ticket-another_object = %w( User Ticket AnotherObject ) if !params[:objects] - objects = %w( Ticket User Organization ) + objects = Setting.get('models_searchable') else objects = params[:objects].split('-').map(&:camelize) end diff --git a/app/models/ticket/search.rb b/app/models/ticket/search.rb index 338f3cd4a..4087738f3 100644 --- a/app/models/ticket/search.rb +++ b/app/models/ticket/search.rb @@ -20,12 +20,12 @@ returns if user has no permissions to search =end - def search_preferences(_current_user) - { - prio: 3000, - direct_search_index: false, - } - end + def search_preferences(_current_user) + { + prio: 3000, + direct_search_index: false, + } + end =begin @@ -79,7 +79,7 @@ returns =end - def search (params) + def search(params) # get params query = params[:query] diff --git a/config/initializers/zammad_constants.rb b/config/initializers/core_zammad_constants.rb similarity index 100% rename from config/initializers/zammad_constants.rb rename to config/initializers/core_zammad_constants.rb diff --git a/config/initializers/models_searchable.rb b/config/initializers/models_searchable.rb new file mode 100644 index 000000000..d57d805f7 --- /dev/null +++ b/config/initializers/models_searchable.rb @@ -0,0 +1,8 @@ +# update settings for searchable models +if ActiveRecord::Base.connection.tables.include?('settings') + models_current = Models.searchable.map(&:to_s) + models_config = Setting.get('models_searchable') + if models_current != models_config + Setting.set('models_searchable', models_current) + end +end diff --git a/db/migrate/20150816000001_update_model_searchable.rb b/db/migrate/20150816000001_update_model_searchable.rb new file mode 100644 index 000000000..72db5ad58 --- /dev/null +++ b/db/migrate/20150816000001_update_model_searchable.rb @@ -0,0 +1,15 @@ +class UpdateModelSearchable < ActiveRecord::Migration + def up + + Setting.create_if_not_exists( + title: 'Define searchable models.', + name: 'models_searchable', + area: 'Models::Base', + description: 'Define the models which can be searched for.', + options: {}, + state: [], + frontend: false, + ) + + end +end diff --git a/db/seeds.rb b/db/seeds.rb index 5f96d478e..5c2b6d362 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -1187,6 +1187,16 @@ Setting.create_if_not_exists( frontend: true ) +Setting.create_if_not_exists( + title: 'Define searchable models.', + name: 'models_searchable', + area: 'Models::Base', + description: 'Define the models which can be searched for.', + options: {}, + state: [], + frontend: false, +) + Setting.create_if_not_exists( title: 'Default Screen', name: 'default_controller', diff --git a/lib/models.rb b/lib/models.rb index 23adfdc1c..4158450e3 100644 --- a/lib/models.rb +++ b/lib/models.rb @@ -10,11 +10,11 @@ get list of models returns { - 'Some::Classname1' => { + Some::Classname1 => { attributes: ['id', 'name', '...'] reflections: ...model.reflections... }, - 'Some::Classname2' => { + Some::Classname2 => { attributes: ['id', 'name', '...'] reflections: ...model.reflections... }, @@ -49,6 +49,28 @@ returns =begin +get list of searchable models + + result = Models.searchable + +returns + + [Model1, Model2, Model3] + +=end + + def self.searchable + models = [] + all.each {|model_class, options| + next if !model_class + next if !model_class.respond_to? :search_preferences + models.push model_class + } + models + end + +=begin + get reference list of a models result = Models.references('User', 2) diff --git a/test/unit/model_test.rb b/test/unit/model_test.rb index 5bb8e5bf9..89de3260e 100644 --- a/test/unit/model_test.rb +++ b/test/unit/model_test.rb @@ -235,4 +235,13 @@ class ModelTest < ActiveSupport::TestCase end + test 'searchable test' do + searchable = Models.searchable + assert(searchable.include?(Ticket)) + assert(searchable.include?(User)) + assert(searchable.include?(Organization)) + assert_equal(3, searchable.count) + + end + end