How to generate asset IDs in Rails 3 to control cache? - ruby-on-rails-3

In Rails, when serving static files, you get an asset ID that's appended to the URL, e.g.,
<script src="/javascripts/application.js?1300371955" ...
<link href="/stylesheets/custom.css?1299690788" ...
This way, the URL of a static file is changed if the file’s timestamp is changed and browsers automatically request the new file.
I saw that by using the helper methods for including static assets --- stylesheet_link_tag, javascript_include_tag and image_tag --- Rails automatically adds timestamps to all references to those files.
How can I implement something similar for other assets that don't have such helper methods e.g., .swf files?
(Does this "strategy" to force the re-download have a name; if so, what is it called?)

The Rails method that appends the timestamp to assets is called rails_asset_id, and is defined in ActionView::Helpers::AssetTagHelper. Though it is a private method, it can be accessed in a helper to generate one for your custom tags like so:
def swf_param_tag(path)
asset_id = rails_asset_id(path)
"<param name='movie' value='#{path}?#{asset_id}'/>"
end
It needs you to pass in the path, because it then calls File.mtime(path).to_i.to_s on it to get the last modified time on it.
There is further explanation about what the asset tag buys you here:
http://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html

#Unixmonkey: your solution works very well.
As an alternative there's also the swf_fu plugin:
treat your Adobe Flash swf files like any other asset (images, javascripts, etc...) and embed them using SWFObject 2.2
that has asset tagging built-in.

Related

In UWP app, where should html assets be stored in the assets directory?

In a UWP app using cppwinrt I want to use WebView to display contents of a book kept in the Assets folder. I read that it is necessary to reference an html asset this way for use as a Uri argument to the Navigate method in web view:
TheWebView.Navigate(Uri(L"ms-appx-web:///SampleBook/PageOne.html"));
This produces an empty view, while
TheWebView.Navigate(Uri(L"ms-appx:///SampleBook/PageOne.html"));
crashes. Msdn says that for files "that will be loaded into the web compartment" one must use ms-appx-web, and I've seen mention that this is a security issue. But does that mean the files are in a special location within the project - i.e. not merely in the Assets folder - or does it only mean that the path must begin with ms-appx-web independent of the file's location? "Web compartment" is not explained but seems to be not a location but rather a classification of the type of resource. At any rate, neither of the above approaches works, so I'm curious to know the recommended way to store and access a collection of html files in the package. In the assets folder? A special folder within assets? In Solution Explorer the html file is listed, "content" is True, and the file is Included In Project. Thanks.
My mistake: ms-appx-web does not point to the assets folder, but to its parent. The correct path for content of this type would be ms-appx-web///Assets/SampleBook/PageOne.html. The reference to material to be "loaded to the web compartment" apparently is just a way of saying: stuff to be loaded with WebViewer.

Find filename of view template in Rails 3 based on action and format

I'm upgrading an old Rails 2.3 (I know, I know) which uses an external program to modify a PDF before rendering it for an action. Therefore, it's necessary for the action to be able to determine the filename of the PDF source file, which is stored in the view template directory, which was done with:
view_paths.find_template(default_template_name(:show), :pdf).filename
However, this no longer works in Rails 3. I've tried something like:
lookup_context.find :show, controller_name
But I can't find a way to specify the format, so that always returns the path for the HTML template (app/views/name/show.html.erb). How can I get the filename of the template for the PDF format (app/views/name/show.pdf)?
Though more complicated than I would like, I eventually found this, which seems to work:
ActionView::LookupContext.
new(view_paths, {formats: :pdf}).
find(:show, controller_name).
identifier

Middleman Build assets without hash

How do I configure Middleman to build images without appending a hash to the filename? I'm referring to filepaths in javascript and need to know the full filename to refer to the files. My JS doesn't get updated with the hashed filenames like my CSS does.
Oops, figured it out. I had enabled activate :asset_hash. removing that from config.rb fixed it.
No need to abandon :asset_hash just because you want to refer to them in JS. The asset hash extension actually attempts to rewrite paths in CSS and JavaScript automatically, but it sounds like whatever way you're linking them isn't getting detected.
You can always name your javascript something like application.js.erb and then have code like this:
var my_image = <%= image_path("myimage.png") %>;
That way you'll always have the right path.

rails assets pipeline with yielded javascript

since I wan't to keep my js files separated for some views, I am making use of a helper function inside the view which yields the js file into the html head
def javascript(*files)
content_for(:head) { javascript_include_tag(*files) }
end
So I was wondering how can I achieve this with assets the pipeline and the pre compile mechanism?
Best,
Phil
Every JS file that is being required in the HTML head section needs to be precompiled. By default only application.js is being precompiled, but you can schedule additional files for precompilation in config/application.rb:
config.assets.precompile += ['admin.js', 'customer_page.js']
Those files can be manifests, just like application.js, if you want to group the JS-files together.

Where should I put my custom widget files in Yii framework?

From this page,
http://www.yiiframework.com/wiki/23/how-to-create-a-breadcrumb-widget/
It seems it suggests that we should put the files in the component folder. But if my widget contains javascript and css files, where should these files be placed?
By the way, is this a good idea that I create it as an extension? If I go this way, all widget files are more self-contained in a folder inside the extension folder. But since the widget I am going to work on is very customized, it's unlikely that it will be useful to other people or my other projects. Making it an extension seems a little bit strange.
I understand that it does not really matter where I put these files as long as the paths I am using in the codes are correct but I would like to know the common practice.
I think the common practice is to put the widget in extensions folder with js & css files in an folder named asset. In the php class file, you do initialization first by publishing the asset with yii asset manager.
The file structure may be like
extensions/
widget_name/
widget.class.php
assets/
plugin.js
style.css
I would join the recommendation to put the widget under /protected/extensions.
I put the assets in a slightly more detailed manner: /protected/extensions/WidgetClassName/assets/ and the widget view files in /protected/extensions/WidgetClassName/views/...
Don't forget to edit your /protected/config/main.php and add a row in the 'import' section (for autoloading of the widget): 'ext.WidgetClassName.WidgetClassName.*'