Class: E9Tags::Rack::TagsJs

Inherits:
Object
  • Object
show all
Defined in:
lib/e9_tags/rack/tags_js.rb

Class Method Summary collapse

Class Method Details

.call(env) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/e9_tags/rack/tags_js.rb', line 3

def self.call(env)
  if env["PATH_INFO"] =~ /^\/js\/tags/
    rel  = ::Tagging
             .joins(:tag)
             .order('tags.name')
             .select('distinct taggings.context, tags.name')

    retv = ::Tagging.connection
             .select_rows(rel.to_sql, 'Tags JS')
             .inject({}) {|h, row| (h[row.first] ||= []) << row.last; h } 

    js   = "window.e9=window.e9||{};window.e9.tags=#{retv.to_json};"

    [200, {"Content-Type" => "text/javascript", "Cache-Control" => "max-age=3600, must-revalidate"}, [js]]
  else
    [404, {"Content-Type" => "text/html", "X-Cascade" => "pass"}, ["Not Found"]]
  end
end