Added support for themeless searchable models.
This commit is contained in:
parent
b2aa52c22a
commit
dac3a506ec
8 changed files with 74 additions and 10 deletions
|
@ -21,7 +21,7 @@ class SearchController < ApplicationController
|
||||||
# convert objects string into array of class names
|
# convert objects string into array of class names
|
||||||
# e.g. user-ticket-another_object = %w( User Ticket AnotherObject )
|
# e.g. user-ticket-another_object = %w( User Ticket AnotherObject )
|
||||||
if !params[:objects]
|
if !params[:objects]
|
||||||
objects = %w( Ticket User Organization )
|
objects = Setting.get('models_searchable')
|
||||||
else
|
else
|
||||||
objects = params[:objects].split('-').map(&:camelize)
|
objects = params[:objects].split('-').map(&:camelize)
|
||||||
end
|
end
|
||||||
|
|
8
config/initializers/models_searchable.rb
Normal file
8
config/initializers/models_searchable.rb
Normal file
|
@ -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
|
15
db/migrate/20150816000001_update_model_searchable.rb
Normal file
15
db/migrate/20150816000001_update_model_searchable.rb
Normal file
|
@ -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
|
10
db/seeds.rb
10
db/seeds.rb
|
@ -1187,6 +1187,16 @@ Setting.create_if_not_exists(
|
||||||
frontend: true
|
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(
|
Setting.create_if_not_exists(
|
||||||
title: 'Default Screen',
|
title: 'Default Screen',
|
||||||
name: 'default_controller',
|
name: 'default_controller',
|
||||||
|
|
|
@ -10,11 +10,11 @@ get list of models
|
||||||
returns
|
returns
|
||||||
|
|
||||||
{
|
{
|
||||||
'Some::Classname1' => {
|
Some::Classname1 => {
|
||||||
attributes: ['id', 'name', '...']
|
attributes: ['id', 'name', '...']
|
||||||
reflections: ...model.reflections...
|
reflections: ...model.reflections...
|
||||||
},
|
},
|
||||||
'Some::Classname2' => {
|
Some::Classname2 => {
|
||||||
attributes: ['id', 'name', '...']
|
attributes: ['id', 'name', '...']
|
||||||
reflections: ...model.reflections...
|
reflections: ...model.reflections...
|
||||||
},
|
},
|
||||||
|
@ -49,6 +49,28 @@ returns
|
||||||
|
|
||||||
=begin
|
=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
|
get reference list of a models
|
||||||
|
|
||||||
result = Models.references('User', 2)
|
result = Models.references('User', 2)
|
||||||
|
|
|
@ -235,4 +235,13 @@ class ModelTest < ActiveSupport::TestCase
|
||||||
|
|
||||||
end
|
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
|
end
|
||||||
|
|
Loading…
Reference in a new issue