-
-
Save seancribbs/1307440 to your computer and use it in GitHub Desktop.
Sprockets asset resource for Webmachine (asset pipelining!)
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 Resources::Assets < Webmachine::Resource | |
def content_types_provided | |
path = request.uri.path | |
@asset = Application.assets[path] | |
if @asset.present? | |
[[@asset.content_type, :to_asset]] | |
else | |
# Better to simply provide a content-type you CAN provide, even if it's not acceptable. | |
# Since the asset is not there, it'll bail anyway at resource_exists? | |
[["text/html", :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 |
Ah, interesting approach. I'm still learning my way around the callbacks, but I'll try experimenting with service_available.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yeah, it's interesting because
content_types_provided
happens much earlier thanresource_exists?
(and is called many times). Another option might be to use one of the halt-able callbacks (service_available?
comes to mind) to simply return a 404.