Skip to content

Instantly share code, notes, and snippets.

@jamis
Created October 22, 2011 19:22
Show Gist options
  • Save jamis/1306388 to your computer and use it in GitHub Desktop.
Save jamis/1306388 to your computer and use it in GitHub Desktop.
Sprockets asset resource for Webmachine (asset pipelining!)
class Resources::Assets < Webmachine::Resource
def content_types_provided
@asset = Application.assets[request.uri.path]
if @asset.present?
[[@asset.content_type, :to_asset]]
else
accept = request.headers['Accept'] || "text/html"
[[accept.split(/,/).first.split(/;/).first.strip, :to_html]]
end
end
def resource_exists?
@asset.present?
end
def generate_etag
@asset.digest
end
def last_modified
@asset.mtime
end
def to_asset
@asset.to_s
end
def to_html
Application.logger.error "reached impossible state in asset resource (no asset, but no error) path=#{request.path_tokens.inspect}"
"<html><body><h1>Bug!</h1><p>Shouldn't ever get here.</p></body></html>"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment