With the swf_fu
plugin, rails treats your swf files like any other asset (images, javascripts, etc…).
swf_fu
(pronounced “swif-fu”, bonus joke for french speakers) uses SWFObject 2.2 to embed swf objects in HTML and supports all its options. SWFObject 2 is such a nice library that Adobe now uses it as the official way to embed swf! SWFObject’s project can be found at code.google.com/p/swfobject
This version of swf_fu
will only work with rails 3.1. No real tests have been done other than “getting it to work on my application”.
As a gem:
[sudo] gem install swf_fu
In rails:
# Using bundler (Rails 3.1) gem 'swf_fu', ‘>=1.3.4', :require => “swf_fu”
In Rails 3.1 add to your app/assets/javascript/application.js
//= require swfobject
For older versions of rails
or without git
, you can always download swf_fu
from github and then install following theirs instructions.
To embed a swf file, use swf_tag
:
<%= swf_tag "i_like_flashing" %>
Exactly like images and javascripts, swf_tag
will use swf_path
to determine the path of the swf file; it will assume it is in /public/swfs/
unless specified otherwise and it will add the “.swf” extension automatically.
You can specify alternate content either with the options <q>:alt => “Get Flash!”</q> or you can use swf_tag
as a block:
<% swf_tag "i_like_flashing" do %> Get Flash <% end %>
-
:id
- the DOMid
of the flashobject
element that is used to contain the Flash object; defaults to the name of the swf insource
-
:width, :height
- the width & height of the Flash object. Defaults to “100%”. These could also specified using :size -
:size
- the size of the Flash object, in the form “400x300”. -
:mode
- Either :dynamic (default) or :static. Refer to SWFObject’s doc -
:flashvars
- a Hash of variables that are passed to the swf. Can also be a string like"foo=bar&hello=world"
. Defaults to{:id => the DOM id}
-
:parameters
- a Hash of configuration parameters for the swf. See Adobe’s doc -
:alt
- HTML text that is displayed when the Flash player is not available. Defaults to a “Get Flash” image pointing to Adobe Flash’s installation page. This can also be specified as a block (see embedding section). In Rails 3, this text is assumed to be HTML, so there is no need to callhtml_safe
on it. -
:flash_version
- the version of the Flash player that is required (e.g. “7” (default) or “8.1.0”) -
:auto_install
- a swf file that will upgrade flash player if needed (defaults to “expressInstall” which was installed byswf_fu
) -
:javascript_class
- specify a javascript class (e.g. “MyFlash”) for your flash object. If it exists, the initialize method will be called. -
:initialize
- arguments to pass to the initialization method of your javascript class. -
:div_id
- the DOMid
of the containing div itself. Defaults to"#{option[:id]}"_div
-
:switch_off_auto_hide_show
- switch off SWFObject’s default hide/show behavior. SWFObject temporarily hides your SWF or alternative content until the library has decided which content to display. Defaults to nil.
You can override these default options with a global setting:
ActionView::Base.swf_default_options = {:mode => :static} # All swf_tag will use the static mode by default
swf_fu
will add ‘swfobject’ to the list asssets. It should be included in the pages using the swf_tag. (see install instructions)
swf_tag
implements and relies on swf_path
which behaves in a similar fashion to image_path
, javascript_path
, etc…:
swf_path("example") => /swfs/example.swf swf_path("example.swf") => /swfs/example.swf swf_path("fonts/optima") => /swfs/fonts/optima.swf swf_path("/fonts/optima") => /fonts/optima.swf swf_path("http://www.example.com/game.swf") => http://www.example.com/game.swf
It takes into account the global setting asset_host
, like any other asset:
ActionController::Base.asset_host = "http://assets.example.com" image_path("logo.jpg") => http://assets.example.com/images/logo.jpg swf_path("fonts/optima") => http://assets.example.com/swfs/fonts/optima.swf
Copyright © 2010 Marc-André Lafortune, released under the BSD license