5
6
7
8
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
|
# File 'lib/e9_tags/rack/tag_context_auto_completer.rb', line 5
def self.call(env)
if env["PATH_INFO"] =~ /^\/autocomplete\/tag\-contexts/
terms = []
if @term = Rack::Request.new(env).params['term']
taggings = ::Tagging.arel_table
relation = taggings.
group(taggings[:context]).
where(taggings[:context].matches("#{@term}%")).
project(taggings[:context], taggings[:context].count.as('count')).
take(DEFAULT_LIMIT).
order('context ASC')
terms = ::ActiveRecord::Base.connection.send(:select, relation.to_sql, 'Tag Context Autocomplete').map do |row|
unescaped_context = E9Tags.unescape_context(row['context'])
{ :label => "#{unescaped_context} - #{row['count']}", :value => unescaped_context, :count => row['count'] }
end
end
[200, {"Content-Type" => "application/json", "Cache-Control" => "max-age=3600, must-revalidate"}, [terms.to_json]]
else
[404, {"Content-Type" => "text/html", "X-Cascade" => "pass"}, ["Not Found"]]
end
end
|