Created
October 7, 2014 13:54
-
-
Save leschenko/85f5fbde08f3f1ff5c94 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AdminAutocompleteApp | |
def self.call(env) | |
I18n.locale = :ru | |
params = Rack::Request.new(env).params | |
raw_klass = params['custom'].try(:fetch, 'class') || params['class'] | |
klass = Rack::Utils.unescape(raw_klass).safe_constantize | |
options = params.slice('with', 'with_all', 'without', 'page', 'per_page').symbolize_keys | |
options[:no_escape] = true | |
case klass.name | |
when 'Structure' | |
options[:order] = 'lft' | |
options[:per_page] = 100 | |
else | |
options[:order] = 'created_at' | |
options[:sort_mode] = 'desc' | |
end | |
q = params['q'].to_s.force_encoding('UTF-8') | |
entries = klass.ac_search(q, options).to_a | |
entries = klass.ac_search(q.tr_lang, options).to_a if entries.empty? | |
attr = params['attr'] || (klass.ac_opts[:localized] ? "#{klass.ac_attr}_#{I18n.locale}" : klass.ac_attr.to_s) | |
if params['token'] | |
res = entries.map { |r| klass.for_input_token(r, attr) } | |
else | |
suggestions = entries.map_val(attr) | |
res = {suggestions: suggestions, data: entries.map_val(:id), query: q} | |
end | |
[200, {'Content-Type' => 'application/json'}, res.to_json] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment