Module: SWFHelper

Defined in:
lib/restfulx/rails/swf_helper.rb

Overview

Adds a little helper to make it easier empbedding SWFs in ERB templates.

Instance Method Summary collapse

Instance Method Details

#swfobject(swf_url, params = {}) ⇒ Object

Creates a swfObject Javascript call. You must include swfobject.js to use this. See code.google.com/p/swfobject/wiki/documentation for full details and documentation of the swfobject js library.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/restfulx/rails/swf_helper.rb', line 7

def swfobject(swf_url, params = {})
  params.reverse_merge!({:width => '100%',
                         :height => '100%',
                         :id => 'flashContent',
                         :version => '9.0.0',
                         :express_install_swf => '/expressInstall.swf',
                         :flash_vars => nil,
                         :params => nil,
                         :attributes => nil,
                         :create_div => false, 
                         :include_authenticity_token => true,
                         :include_session_token => true
                        })                       
  arg_order = [:id, :width, :height, :version, :express_install_swf]
  js_params = ["'#{swf_url}?#{rails_asset_id(swf_url)}'"]
  js_params += arg_order.collect {|arg| "'#{params[arg]}'" }
  
  # Add authenticity_token and the session key to flashVars.  This will only work if flashVars is a Hash or nil
  # If it's a string representing the name of a Javascript variable, then you need to add them yourself 
  # like this:
  # <script>
  #   ... other code that defines flashVars and sets some of its parameters
  #   flashVars['authenticity_token'] = <%= form_authenticity_token -%>
  #   flashVars['session_token'] = <%= session.session_id -%>
  # </script>
  # If you include an authenticity_token parameter in flashVars, 
  # then the Flex app will add it to Rx.defaultMetadata, so that it will be sent
  # back up to your Rails app with every request.
  params[:flash_vars] ||= {}
  if params[:flash_vars].is_a?(Hash)
    if params[:include_authenticity_token] && ActionController::Base.allow_forgery_protection
      params[:flash_vars].reverse_merge!(:authenticity_token => form_authenticity_token)
    end
    if params[:include_session_token]
      params[:flash_vars].reverse_merge!(:session_token => session.session_id)
    end        
  end          
  
  js_params += [params[:flash_vars], params[:params], params[:attributes]].collect do |hash_or_string|
    if hash_or_string.is_a?(Hash)
      hash_or_string.to_json
    else # If it's not a hash, then it should be a string giving the name of the Javascript variable to use
      hash_or_string
    end
  end.compact

  swf_tag = javascript_tag do 
    "swfobject.embedSWF(#{js_params.join(',')})"
  end 
  swf_tag += (:div, nil, :id => params[:id]) if params[:create_div]    
  swf_tag
end