Class: Jammit::Controller
- Inherits:
-
ActionController::Base
- Object
- ActionController::Base
- Jammit::Controller
- Defined in:
- lib/jammit/controller.rb
Overview
The JammitController is added to your Rails application when the Gem is loaded. It takes responsibility for /assets, and dynamically packages any missing or uncached asset packages.
Constant Summary
- VALID_FORMATS =
[:css, :js, :jst]
- SUFFIX_STRIPPER =
/-(datauri|mhtml)\Z/
- NOT_FOUND_PATH =
"#{PUBLIC_ROOT}/404.html"
Method Summary
- - (Object) package The “package” action receives all requests for asset packages that haven’t yet been cached.
- - (Object) cache_package private Tells the Jammit::Packager to cache and gzip an asset package.
- - (Object) generate_stylesheets private If we’re generating MHTML/CSS, return a stylesheet with the absolute request URL to the client, and cache a version with the timestamped cache URL swapped in.
- - (Object) package_not_found private Render the 404 page, if one exists, for any packages that don’t.
- - (Object) parse_request private Extracts the package name, extension (:css, :js, :jst), and variant (:datauri, :mhtml) from the incoming URL.
- - (Object) prefix_url(path) private Generate the complete, timestamped, MHTML url — if we’re rendering a dynamic MHTML package, we’ll need to put one URL in the response, and a different one into the cached package.
Method Details
- (Object) package
The “package” action receives all requests for asset packages that haven’t yet been cached. The package will be built, cached, and gzipped.
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/jammit/controller.rb', line 18 def package parse_request case @extension when :js then render :js => Jammit.packager.pack_javascripts(@package) when :css then render :text => generate_stylesheets, :content_type => 'text/css' when :jst then render :js => Jammit.packager.pack_templates(@package) end rescue Jammit::PackageNotFound package_not_found end |
- (Object) cache_package (private)
Tells the Jammit::Packager to cache and gzip an asset package. We can’t just use the built-in “cache_page” because we need to ensure that the timestamp that ends up in the MHTML is also on the cached file.
35 36 37 38 |
# File 'lib/jammit/controller.rb', line 35 def cache_package dir = File.join(page_cache_directory, Jammit.package_path) Jammit.packager.cache(@package, @extension, @contents || response.body, dir, @variant, @mtime) end |
- (Object) generate_stylesheets (private)
If we’re generating MHTML/CSS, return a stylesheet with the absolute request URL to the client, and cache a version with the timestamped cache URL swapped in.
51 52 53 54 55 56 57 58 59 |
# File 'lib/jammit/controller.rb', line 51 def generate_stylesheets return Jammit.packager.pack_stylesheets(@package, @variant) unless @variant == :mhtml @mtime = Time.now request_url = prefix_url(request.request_uri) cached_url = prefix_url(Jammit.asset_url(@package, @extension, @variant, @mtime)) css = Jammit.packager.pack_stylesheets(@package, @variant, request_url) @contents = css.gsub(request_url, cached_url) if perform_caching css end |
- (Object) package_not_found (private)
Render the 404 page, if one exists, for any packages that don’t.
76 77 78 79 |
# File 'lib/jammit/controller.rb', line 76 def package_not_found return render(:file => NOT_FOUND_PATH, :status => 404) if File.exists?(NOT_FOUND_PATH) render :text => "<h1>404: \"#{@package}\" asset package not found.</h1>", :status => 404 end |
- (Object) parse_request (private)
Extracts the package name, extension (:css, :js, :jst), and variant (:datauri, :mhtml) from the incoming URL.
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/jammit/controller.rb', line 63 def parse_request pack = params[:package] @extension = params[:extension].to_sym raise PackageNotFound unless VALID_FORMATS.include?(@extension) if Jammit. suffix_match = pack.match(SUFFIX_STRIPPER) @variant = Jammit. && suffix_match && suffix_match[1].to_sym pack.sub!(SUFFIX_STRIPPER, '') end @package = pack.to_sym end |
- (Object) prefix_url(path) (private)
Generate the complete, timestamped, MHTML url — if we’re rendering a dynamic MHTML package, we’ll need to put one URL in the response, and a different one into the cached package.
43 44 45 46 |
# File 'lib/jammit/controller.rb', line 43 def prefix_url(path) host = request.port == 80 ? request.host : request.host_with_port "#{request.protocol}#{host}#{path}" end |