9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'app/controllers/slash_admin/remote_select_controller.rb', line 9
def search
model_class = model
results = if model_class.respond_to? :translated_attribute_names
model_class.with_translations(I18n.locale).all
else
model_class.all
end
duplicate_for_orwhere = results
virtual_fields = []
params[:fields].each_with_index do |f, index|
if model_class.respond_to? :translated_attribute_names
if model_class.translated_attribute_names.include?(f.to_sym)
f = "#{params[:model_class].singularize.underscore}_translations.#{f}"
end
else
unless model_class.column_names.include?(f) || model_class.respond_to?(f)
raise Exception.new("Unable to find attribute: #{f} in model_column: #{model_class}, you may need to override autocomplete_params in you target's model controller")
end
end
if index == 0
if model_class.column_names.include?(f)
results = results.where("lower(#{f}) LIKE lower(:query)", query: "%#{params[:q]}%")
else
virtual_fields << f
end
else
results = results.or(duplicate_for_orwhere.where("lower(#{f}) LIKE lower(:query)", query: "%#{params[:q]}%"))
end
end
params[:fields].each do |f|
unless params[:q].blank?
if virtual_fields.present? && virtual_fields.include?(f)
results = results.select { |s| s.send(f).present? ? s.send(f).downcase.include?(params[:q].downcase) : nil }
end
end
end
formatted_result = []
results.each do |r|
formatted_result << {id: r.id, text: helpers.show_object(r)}
end
render json: formatted_result
end
|