Is there a built-in way to determine if an asset exists without resorting to File.exists?(File.join(Rails.root, "foo", "bar", "baz")) and that looks through the asset paths.
My app goes and fetches images from a remote server on a Resque queue; until we have the image downloaded I want to serve a placeholder image. Currently I'm using File.exists... but this means hard-coding a path, which sucks, or looking through the configured asset paths. It seems like this should be there already, but I can't find it in the docs.
Given an image in app/assets/images/lolshirts/theme/bg-header.png,
Rails.application.assets.find_asset 'lolshirts/theme/bg-header.png'
=> #> Sprockets::StaticAsset:0x80c388ec pathname="/Users/joevandyk/projects/tanga/sites/lolshirts/app/assets/images/lolshirts/theme/bg-header.png", mtime=2011-10-07 12:34:48 -0700, digest="a63cc84aca38e2172ae25de3d837c71a">
Rails.application.assets.find_asset 'notthere.png'
=> nil
Since this is still the top question when searching Google, and since the accepted answer does not work properly in production (at least in some cases), here is the solution that works for me (Rails 4.x to 6.x at least):
def asset_exist?(path)
if Rails.configuration.assets.compile
Rails.application.precompiled_assets.include? path
else
Rails.application.assets_manifest.assets[path].present?
end
end
This is copied from this github issue
See this answer:
https://stackoverflow.com/a/8217598/549252
= Rails.application.assets.find_asset("my_asset.css").nil?
Please see the answer here for a discussion on why find_asset may not always work:
Include Assets Only If They Exist
Related
I am using Ruby to find/store all links in a web page that is pointing to another resource (like another HTML page).
Obviously, links like the below are real links:
Dummy 3
I am encountering links like the below that are not truly links to resources:
Dummy
Dummy 2 //a javascript link could redirect me to another resource, but I am not interested in these
I'm trying to come up with a rule on excluding links like the 2 above.
Is there other links I should be excluding, aside from "#", and anything that starts with "javascript:" ?
Did you think to use a gem for that? Some people already did the job to get this kind of information and you could use it as well.
I quickly find one but there is a few.
Take a look:
https://github.com/alexpeattie/nitlink#usage
Did a MediaWiki upgrade from 1.15.1 to 1.20.2 by following the simple update instructions (basically a new installation, copying over the old LocalSettings.php, update script and copying over images). Weird thing now is that all of the File: prefixes don't work. Instead the internal links to images is a "file:name of image" URL rather than "http://mediawiki address/index.php/File:name of image".
Anybody else getting this. Assuming it is something wrong with the old LocalSettings.php.
Ran the refreshLinks and refreshImageMetadata maintenance scripts without fixing the problem.
In the comments, you wrote that you have file: added to $wgUrlProtocols. This is very likely what's triggering the problem.
It looks like something has changed in the parser between MW 1.15 and 1.20 so that it's now parsing file:whatever as an external link (since it matches the file: prefix you've defined in $wgUrlProtocols) even if it's inside square brackets.
The obvious workaround would be to change the $wgUrlProtocols entry from file: to file:// so that it will only match if the slashes are there (as they should be, according to standard file: URL syntax). Since your on-wiki filenames are, presumably, very unlikely to begin with double slashes, they should not match this more specific prefix.
That said, this could still be considered a bug in MediaWiki. You may want to file a bug report about it, if there isn't one yet.
(Edit: Looks like Mark A. Hershberger filed one already.)
Say in vendor/assets I have two subdirectories, /alpha and /beta, each with a file named temp.jpg. To my understanding, a GET request is made for /assets/temp.jpg, and I'm pretty sure the one from the alpha directory is served. But how can I distinguish between the two of them? I think it can be done with the asset_url helper but I'm not quite sure - if anyone can advise that would be great.
From the manual:
You can view the search path by inspecting Rails.application.config.assets.paths in the Rails console.
Additional (fully qualified) paths can be added to the pipeline in config/application.rb. For example:
config.assets.paths << Rails.root.join("app", "assets", "flash")
Sprockets will also look through the paths specified in config.assets.paths which includes the standard application paths and any path added by Rails engines.
Images can also be organized into subdirectories if required, and they can be accessed by specifying the directory’s name in the tag:
<%= image_tag "icons/rails.png" %>
If you're using the asset pipeline, I'm not sure which of your images would be supplied by a link that doesn't specify the url, if any. If one is being supplied, it's going to have to do with the order in which Sprockets recursively reads those directories. If it reads them in alpha order, then the "beta" image is going to be served. Or else it's just going to be random, I'm not sure how Sprockets reads the directories.
You're going to be better off putting an explicit path in there. If you are doing some sort of a test, like if you want the beta images to appear, I'd recommend some sort of paramaterized approach so that you can pass in "alpha" or "beta" to your path.
Check http://guides.rubyonrails.org/asset_pipeline.html#asset-organization and try execute Rails.application.config.assets.paths in the Rails console in order to debug your assets loading path.
I have a Modular Sinatra app and I'm trying to add the Bootstrap less to the application.
get '/bootstrap/application.css' do
less :"bootstrap/bootstrap"
end
I have all less files in views/bootstrap, including bootstrap.less.
I get this error:
Less::ParseError at /bootstrap/application.css 'reset.less' wasn't found.
The first real line of Bootstrap.less is:
// CSS Reset
#import "reset.less";
I've tried all different path formats, but it never finds the files it's looking to import. Any idea how to make this work?
Have you tried passing the :paths config option?
get '/bootstrap/application.css' do
less :"bootstrap/bootstrap", :paths => ["views/bootstrap"]
end
I've had problems with this option in the past but it may work for you.
I found a solution that worked for me here: Parsing LESS options in a Sinatra app
Since this question was the one I found first on Google, I thought I'd leave the link here so that others can find it.
mkristgan's rack_datamapper gem says that it "can be wrapped to be used in a specific environement, i.e. Rack::Session::Datamapper".
Unfortunately, I don't know quite enough about Ruby to accomplish this task yet –Modules/Classes in Ruby are still above my head (coming from PHP).
Can anyone offer assistance with using rack_datamapper to implement Rack::Session::Datamapper?
You probably don't want to do this anyway.
The answer below is great, but upon closer consideration, I realized I shouldn't do it anyway. Instead, I'm placing the user_id, ip and first name (for convenience) in a cookie and protecting it.
This gem should help:
In Sinatra just add:
use Rack::Session::Moneta,
store: Moneta.new(:DataMapper, setup: (ENV['DATABASE_URL'] || "sqlite://#{Dir.pwd}/development.db"))
and use session[] object at will.