swf_fu 1.3.3 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +30 -50
- data/Rakefile +38 -0
- data/app/assets/javascripts/swf_fu.js.coffee +7 -0
- data/{assets → app/assets}/javascripts/swfobject.js +0 -0
- data/app/helpers/swf_fu_helper.rb +51 -0
- data/app/models/swf_fu/generator.rb +139 -0
- data/lib/swf_fu.rb +15 -2
- data/lib/swf_fu/version.rb +3 -0
- data/lib/tasks/swf_fu_tasks.rake +4 -0
- data/test/dummy/README.rdoc +261 -0
- data/test/dummy/Rakefile +7 -0
- data/test/dummy/app/assets/javascripts/application.js +16 -0
- data/test/dummy/app/assets/javascripts/example.js.coffee +9 -0
- data/test/dummy/app/assets/stylesheets/application.css +13 -0
- data/test/dummy/app/controllers/application_controller.rb +5 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/views/application/example.html.erb +7 -0
- data/test/dummy/app/views/layouts/application.html.erb +14 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/config/application.rb +56 -0
- data/test/dummy/config/boot.rb +10 -0
- data/test/dummy/config/database.yml +25 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +37 -0
- data/test/dummy/config/environments/production.rb +67 -0
- data/test/dummy/config/environments/test.rb +37 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/inflections.rb +15 -0
- data/test/dummy/config/initializers/mime_types.rb +5 -0
- data/test/dummy/config/initializers/secret_token.rb +7 -0
- data/test/dummy/config/initializers/session_store.rb +8 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/test/dummy/config/locales/en.yml +5 -0
- data/test/dummy/config/routes.rb +58 -0
- data/test/dummy/db/development.sqlite3 +0 -0
- data/test/dummy/db/test.sqlite3 +0 -0
- data/test/dummy/log/development.log +2861 -0
- data/test/dummy/log/test.log +1002 -0
- data/test/dummy/public/404.html +26 -0
- data/test/dummy/public/422.html +26 -0
- data/test/dummy/public/500.html +25 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/dummy/public/swfs/example.swf +0 -0
- data/test/dummy/public/swfs/source/Example.as +26 -0
- data/test/dummy/public/swfs/source/example.fla +0 -0
- data/test/dummy/script/rails +6 -0
- data/test/dummy/tmp/cache/assets/C14/440/sprockets%2F7165b4ee637490447887e423298985d7 +0 -0
- data/test/dummy/tmp/cache/assets/CC9/940/sprockets%2F384b7526ce19a1676cf839c97e283d65 +0 -0
- data/test/dummy/tmp/cache/assets/CD8/370/sprockets%2F357970feca3ac29060c1e3861e2c0953 +0 -0
- data/test/dummy/tmp/cache/assets/D21/B30/sprockets%2F3c26aead77064197ac98a91d4c46f879 +0 -0
- data/test/dummy/tmp/cache/assets/D2B/260/sprockets%2F85f8be26166cf70b5d954fff37a42987 +0 -0
- data/test/dummy/tmp/cache/assets/D32/A10/sprockets%2F13fe41fee1fe35b49d145bcc06610705 +0 -0
- data/test/dummy/tmp/cache/assets/D41/740/sprockets%2F414358cbdcf6fbb7d59c54ea24824407 +0 -0
- data/test/dummy/tmp/cache/assets/D4E/1B0/sprockets%2Ff7cbd26ba1d28d48de824f0e94586655 +0 -0
- data/test/dummy/tmp/cache/assets/D4F/590/sprockets%2Ff4f8bbee986218b7a2173c69ca28429e +0 -0
- data/test/dummy/tmp/cache/assets/D5A/EA0/sprockets%2Fd771ace226fc8215a3572e0aa35bb0d6 +0 -0
- data/test/dummy/tmp/cache/assets/D68/4F0/sprockets%2F448bbac6b7407ae050c726c3491fde6d +0 -0
- data/test/dummy/tmp/cache/assets/DB2/170/sprockets%2Fc6c10bc26cb025a7b1beb05c24198fae +0 -0
- data/test/dummy/tmp/cache/assets/DDC/400/sprockets%2Fcffd775d018f68ce5dba1ee0d951a994 +0 -0
- data/test/dummy/tmp/cache/assets/E04/890/sprockets%2F2f5173deea6c795b8fdde723bb4b63af +0 -0
- data/test/dummy/tmp/cache/assets/E1E/4F0/sprockets%2Fefa070e0f1ca299bc30dad1ff1b196ee +0 -0
- data/test/dummy/tmp/cache/assets/E6D/DA0/sprockets%2F753c6b9fddf2dffb5f978cd1ae50cda8 +0 -0
- data/test/dummy/tmp/pids/server.pid +1 -0
- data/test/support/assert_same_striped.rb +8 -0
- data/test/{results.rb → support/fixtures/results.rb} +5 -6
- data/test/swf_path_test.rb +35 -0
- data/test/swf_tag_test.rb +93 -0
- data/test/test_helper.rb +9 -18
- metadata +220 -85
- data/CHANGELOG.rdoc +0 -45
- data/FLASH_OBJECT.rdoc +0 -31
- data/VERSION +0 -1
- data/assets/swfs/expressInstall.swf +0 -0
- data/lib/action_view/helpers/asset_tag_helper/swf_asset.rb +0 -61
- data/lib/action_view/helpers/swf_fu_helper.rb +0 -184
- data/lib/swf_fu/tasks.rb +0 -72
- data/rails/init.rb +0 -11
- data/tasks/rails.rake +0 -1
- data/test/swf_fu_test.rb +0 -131
data/README.rdoc
CHANGED
@@ -6,48 +6,21 @@ With the +swf_fu+ plugin, rails treats your swf files like any other asset (imag
|
|
6
6
|
SWFObject 2 is such a nice library that Adobe now uses it as the official way to embed swf!
|
7
7
|
SWFObject's project can be found at http://code.google.com/p/swfobject
|
8
8
|
|
9
|
-
+swf_fu+
|
9
|
+
Version 2 of +swf_fu+ is an engine for Rails 3.1+ and Ruby 1.8.7+. Use version 1.x for previous versions of Rails.
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
Assuming you have git[http://git-scm.com/] installed (check with <q>git version</q>), it is easy to install from your applications directory
|
14
|
-
|
15
|
-
As a plugin:
|
16
|
-
|
17
|
-
# rails 3
|
18
|
-
rails plugin install git://github.com/marcandre/swf_fu.git
|
19
|
-
|
20
|
-
# rails 2 (starting at 2.0.2)
|
21
|
-
script/plugin install git://github.com/marcandre/swf_fu.git
|
22
|
-
|
23
|
-
As a gem:
|
24
|
-
|
25
|
-
[sudo] gem install swf_fu
|
26
|
-
|
27
|
-
In rails:
|
11
|
+
Comments and pull requests welcome: http://github.com/marcandre/swf_fu
|
28
12
|
|
29
|
-
|
30
|
-
gem 'swf_fu', ‘>=1.3.2', :require => “swf_fu”
|
31
|
-
|
32
|
-
# Rails 2.3.x
|
33
|
-
config.gem 'swf_fu', :lib => 'swf_fu', :version => '>=1.3.2', :source => 'http://gemcutter.org/‘
|
34
|
-
|
35
|
-
Because gems don't have install/uninstall hooks like plugins, there is some additional setup:
|
36
|
-
|
37
|
-
# Rakefile add
|
38
|
-
require 'swf_fu/tasks'
|
13
|
+
== Install
|
39
14
|
|
40
|
-
|
15
|
+
Add to your +Gemfile+:
|
41
16
|
|
42
|
-
|
43
|
-
rake swf:uninstall # Uninstall swf_fu assets from your rails application
|
17
|
+
gem "swf_fu", "~> 2.0"
|
44
18
|
|
45
|
-
|
46
|
-
+swf_fu+ from github[http://github.com/marcandre/swf_fu/archives/master] and then install it manually:
|
19
|
+
Then run `bundle install`.
|
47
20
|
|
48
|
-
|
21
|
+
You also need to require some javascripts, so in your `app/assets/javascripts/application.js` you want to append:
|
49
22
|
|
50
|
-
|
23
|
+
\\= require swf_fu
|
51
24
|
|
52
25
|
== Usage
|
53
26
|
|
@@ -57,10 +30,10 @@ To embed a swf file, use +swf_tag+:
|
|
57
30
|
<%= swf_tag "i_like_flashing" %>
|
58
31
|
|
59
32
|
Exactly like images and javascripts, +swf_tag+ will use +swf_path+
|
60
|
-
to determine the path of the swf file; it will assume it is in <tt>/public/swfs/</tt>
|
33
|
+
to determine the path of the swf file; it will assume it is in <tt>/assets/swfs/</tt> or in <tt>/public/swfs/</tt>
|
61
34
|
unless specified otherwise and it will add the ".swf" extension automatically.
|
62
35
|
|
63
|
-
You can specify alternate content either with the options <
|
36
|
+
You can specify alternate content either with the options <tt>:alt => "Get Flash!"</tt> or you can use +swf_tag+ as a block:
|
64
37
|
|
65
38
|
<% swf_tag "i_like_flashing" do %>
|
66
39
|
Get Flash
|
@@ -74,37 +47,44 @@ You can specify alternate content either with the options <q>:alt => "Get Flash!
|
|
74
47
|
* <tt>:mode</tt> - Either :dynamic (default) or :static. Refer to SWFObject's doc[http://code.google.com/p/swfobject/wiki/documentation#Should_I_use_the_static_or_dynamic_publishing_method?]
|
75
48
|
* <tt>:flashvars</tt> - a Hash of variables that are passed to the swf. Can also be a string like <tt>"foo=bar&hello=world"</tt>. Defaults to <tt>{:id => the DOM id}</tt>
|
76
49
|
* <tt>:parameters</tt> - a Hash of configuration parameters for the swf. See Adobe's doc[http://kb.adobe.com/selfservice/viewContent.do?externalId=tn_12701#optional]
|
50
|
+
* <tt>:html_options</tt> - a Hash of extra html options for the <tt>object</tt> tag.
|
77
51
|
* <tt>:alt</tt> - 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 call +html_safe+ on it.
|
78
52
|
* <tt>:flash_version</tt> - the version of the Flash player that is required (e.g. "7" (default) or "8.1.0")
|
79
53
|
* <tt>:auto_install</tt> - a swf file that will upgrade flash player if needed (defaults to "expressInstall" which was installed by +swf_fu+)
|
80
|
-
* <tt>:javascript_class</tt> - specify a javascript class (e.g. "MyFlash") for your flash object. If it exists, the initialize method will be called.
|
54
|
+
* <tt>:javascript_class</tt> - specify a javascript class (e.g. "MyFlash") for your flash object. If it exists, the +initialize+ method will be called. See the 'dummy' example in the test folder
|
81
55
|
* <tt>:initialize</tt> - arguments to pass to the initialization method of your javascript class.
|
82
|
-
* <tt>:div_id</tt> - the DOM +id+ of the containing div itself. Defaults to <tt>"#{option[:id]}"
|
56
|
+
* <tt>:div_id</tt> - the DOM +id+ of the containing div itself. Defaults to <tt>"#{option[:id]}_div"</tt>
|
83
57
|
* <tt>:switch_off_auto_hide_show</tt> - 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.
|
84
58
|
|
85
59
|
You can override these default options with a global setting:
|
86
60
|
|
87
61
|
ActionView::Base.swf_default_options = {:mode => :static} # All swf_tag will use the static mode by default
|
88
62
|
|
89
|
-
|
63
|
+
In your config files, you can write this as:
|
64
|
+
|
65
|
+
config.action_view.swf_default_options = {:mode => :static} # All swf_tag will use the static mode by default
|
66
|
+
|
67
|
+
Any of these options can be a +Proc+, in which case it will be called each time swf_tag is called.
|
68
|
+
|
69
|
+
For example, the following will generate unique IDs:
|
90
70
|
|
91
|
-
|
92
|
-
|
71
|
+
my_swf_counter = 0
|
72
|
+
config.action_view.swf_default_options[:id] = Proc.new{"swf_unique_id_#{my_swf_counter+=1}"}
|
93
73
|
|
94
74
|
=== swf_path
|
95
75
|
|
96
76
|
+swf_tag+ implements and relies on +swf_path+ which behaves in a similar fashion to +image_path+, +javascript_path+, etc...:
|
97
77
|
|
98
|
-
swf_path("example") => /swfs/example.swf
|
99
|
-
swf_path("example.swf") => /swfs/example.swf
|
100
|
-
swf_path("fonts/optima") => /swfs/fonts/optima.swf
|
101
|
-
swf_path("/fonts/optima") => /fonts/optima.swf
|
102
|
-
swf_path("http://www.example.com/game.swf") => http://www.example.com/game.swf
|
78
|
+
swf_path("example") => "/swfs/example.swf"
|
79
|
+
swf_path("example.swf") => "/swfs/example.swf"
|
80
|
+
swf_path("fonts/optima") => "/swfs/fonts/optima.swf"
|
81
|
+
swf_path("/fonts/optima") => "/fonts/optima.swf"
|
82
|
+
swf_path("http://www.example.com/game.swf") => "http://www.example.com/game.swf"
|
103
83
|
|
104
84
|
It takes into account the global setting +asset_host+, like any other asset:
|
105
85
|
|
106
86
|
ActionController::Base.asset_host = "http://assets.example.com"
|
107
|
-
image_path("logo.jpg") => http://assets.example.com/images/logo.jpg
|
108
|
-
swf_path("fonts/optima") => http://assets.example.com/swfs/fonts/optima.swf
|
87
|
+
image_path("logo.jpg") => "http://assets.example.com/images/logo.jpg"
|
88
|
+
swf_path("fonts/optima") => "http://assets.example.com/swfs/fonts/optima.swf"
|
109
89
|
|
110
|
-
Copyright (c) 2010 Marc-André Lafortune, released under the BSD license
|
90
|
+
Copyright (c) 2010-2012 Marc-André Lafortune, released under the BSD license
|
data/Rakefile
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
begin
|
3
|
+
require 'bundler/setup'
|
4
|
+
rescue LoadError
|
5
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
|
+
end
|
7
|
+
begin
|
8
|
+
require 'rdoc/task'
|
9
|
+
rescue LoadError
|
10
|
+
require 'rdoc/rdoc'
|
11
|
+
require 'rake/rdoctask'
|
12
|
+
RDoc::Task = Rake::RDocTask
|
13
|
+
end
|
14
|
+
|
15
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
16
|
+
rdoc.rdoc_dir = 'rdoc'
|
17
|
+
rdoc.title = 'Swf Fu'
|
18
|
+
rdoc.options << '--line-numbers'
|
19
|
+
rdoc.rdoc_files.include('README.rdoc')
|
20
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
|
26
|
+
Bundler::GemHelper.install_tasks
|
27
|
+
|
28
|
+
require 'rake/testtask'
|
29
|
+
|
30
|
+
Rake::TestTask.new(:test) do |t|
|
31
|
+
t.libs << 'lib'
|
32
|
+
t.libs << 'test'
|
33
|
+
t.pattern = 'test/**/*_test.rb'
|
34
|
+
t.verbose = false
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
task :default => :test
|
File without changes
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module SwfFuHelper
|
2
|
+
# Returns a set of tags that display a Flash object within an
|
3
|
+
# HTML page.
|
4
|
+
#
|
5
|
+
# Options:
|
6
|
+
# * <tt>:id</tt> - the DOM +id+ of the flash +object+ element that is used to contain the Flash object; defaults to the name of the swf in +source+
|
7
|
+
# * <tt>:width, :height</tt> - the width & height of the Flash object. Defaults to "100%". These could also specified using :size
|
8
|
+
# * <tt>:size</tt> - the size of the Flash object, in the form "400x300".
|
9
|
+
# * <tt>:mode</tt> - Either :dynamic (default) or :static. Refer to SWFObject's doc[http://code.google.com/p/swfobject/wiki/documentation#Should_I_use_the_static_or_dynamic_publishing_method?]
|
10
|
+
# * <tt>:flashvars</tt> - a Hash of variables that are passed to the swf. Can also be a string like <tt>"foo=bar&hello=world"</tt>
|
11
|
+
# * <tt>:parameters</tt> - a Hash of configuration parameters for the swf. See Adobe's doc[http://kb.adobe.com/selfservice/viewContent.do?externalId=tn_12701#optional]
|
12
|
+
# * <tt>:alt</tt> - 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.
|
13
|
+
# * <tt>:flash_version</tt> - the version of the Flash player that is required (e.g. "7" (default) or "8.1.0")
|
14
|
+
# * <tt>:auto_install</tt> - a swf file that will upgrade flash player if needed (defaults to "expressInstall" which was installed by swf_fu)
|
15
|
+
# * <tt>:javascript_class</tt> - specify a javascript class (e.g. "MyFlash") for your flash object. The initialize method will be called when the flash object is ready.
|
16
|
+
# * <tt>:initialize</tt> - arguments to pass to the initialization method of your javascript class.
|
17
|
+
# * <tt>:div_id</tt> - the DOM +id+ of the containing div itself. Defaults to <tt>"#{option[:id]}"_div</tt>
|
18
|
+
#
|
19
|
+
def swf_tag(source, options={}, &block)
|
20
|
+
::SwfFu::Generator.new(source, options, self).generate(&block)
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
# Computes the path to an swf asset in the public 'swfs' directory.
|
25
|
+
# Full paths from the document root will be passed through.
|
26
|
+
# Used internally by +swf_tag+ to build the swf path.
|
27
|
+
#
|
28
|
+
# ==== Examples
|
29
|
+
# swf_path("example") # => /swfs/example.swf
|
30
|
+
# swf_path("example.swf") # => /swfs/example.swf
|
31
|
+
# swf_path("fonts/optima") # => /swfs/fonts/optima.swf
|
32
|
+
# swf_path("/fonts/optima") # => /fonts/optima.swf
|
33
|
+
# swf_path("http://www.example.com/game.swf") # => http://www.example.com/game.swf
|
34
|
+
#
|
35
|
+
# It takes into account the global setting +asset_host+, like any other asset:
|
36
|
+
#
|
37
|
+
# ActionController::Base.asset_host = "http://assets.example.com"
|
38
|
+
# image_path("logo.jpg") # => http://assets.example.com/images/logo.jpg
|
39
|
+
# swf_path("fonts/optima") # => http://assets.example.com/swfs/fonts/optima.swf
|
40
|
+
def swf_path(source)
|
41
|
+
asset_paths.compute_public_path(source, 'swfs', :ext => 'swf')
|
42
|
+
end
|
43
|
+
alias_method :path_to_swf, :swf_path # aliased to avoid conflicts with an image_path named route
|
44
|
+
|
45
|
+
# Computes the full URL to a swf asset in the public swf directory.
|
46
|
+
# This will use +swf_path+ internally, so most of their behaviors will be the same.
|
47
|
+
def swf_url(source)
|
48
|
+
URI.join(current_host, path_to_swf(source)).to_s
|
49
|
+
end
|
50
|
+
alias_method :url_to_swf, :swf_url # aliased to avoid conflicts with an swf_url named route
|
51
|
+
end
|
@@ -0,0 +1,139 @@
|
|
1
|
+
module SwfFu
|
2
|
+
DEFAULTS = {
|
3
|
+
:width => "100%",
|
4
|
+
:height => "100%",
|
5
|
+
:flash_version => 7,
|
6
|
+
:mode => :dynamic,
|
7
|
+
:auto_install => "expressInstall",
|
8
|
+
:alt => <<-"EOS".squeeze(" ").strip.freeze
|
9
|
+
<a href="http://www.adobe.com/go/getflashplayer">
|
10
|
+
<img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" />
|
11
|
+
</a>
|
12
|
+
EOS
|
13
|
+
}.freeze
|
14
|
+
|
15
|
+
class Generator # :nodoc:
|
16
|
+
VALID_MODES = [:static, :dynamic]
|
17
|
+
def initialize(source, options, view)
|
18
|
+
@view = view
|
19
|
+
@source = view.swf_path(source)
|
20
|
+
options = ActionView::Base.swf_default_options.merge(options)
|
21
|
+
options.each do |key, value|
|
22
|
+
options[key] = value.call(source) if value.respond_to?(:call)
|
23
|
+
end
|
24
|
+
[:html_options, :parameters, :flashvars].each do |k|
|
25
|
+
options[k] = convert_to_hash(options[k]).reverse_merge convert_to_hash(ActionView::Base.swf_default_options[k])
|
26
|
+
end
|
27
|
+
options.reverse_merge!(DEFAULTS)
|
28
|
+
options[:id] ||= source.gsub(/^.*\//, '').gsub(/\.swf$/,'')
|
29
|
+
options[:id] = force_to_valid_id(options[:id])
|
30
|
+
options[:div_id] ||= options[:id]+"_div"
|
31
|
+
options[:div_id] = force_to_valid_id(options[:div_id])
|
32
|
+
options[:width], options[:height] = options[:size].scan(/^(\d*%?)x(\d*%?)$/).first if options[:size]
|
33
|
+
options[:auto_install] &&= @view.swf_path(options[:auto_install])
|
34
|
+
options[:flashvars][:id] ||= options[:id]
|
35
|
+
@mode = options.delete(:mode)
|
36
|
+
@options = options
|
37
|
+
unless VALID_MODES.include? @mode
|
38
|
+
raise ArgumentError, "options[:mode] should be either #{VALID_MODES.join(' or ')}"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def force_to_valid_id(id)
|
43
|
+
id = id.gsub /[^A-Za-z0-9\-_]/, "_" # HTML id can only contain these characters
|
44
|
+
id = "swf_" + id unless id =~ /^[A-Z]/i # HTML id must start with alpha
|
45
|
+
id
|
46
|
+
end
|
47
|
+
|
48
|
+
def generate(&block)
|
49
|
+
@options[:alt] = @view.capture(&block) if block_given?
|
50
|
+
send(@mode)
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
def convert_to_hash(s)
|
55
|
+
case s
|
56
|
+
when Hash
|
57
|
+
s
|
58
|
+
when nil
|
59
|
+
{}
|
60
|
+
when String
|
61
|
+
s.split("&").inject({}) do |h, kvp|
|
62
|
+
key, value = kvp.split("=")
|
63
|
+
h[key.to_sym] = CGI::unescape(value)
|
64
|
+
h
|
65
|
+
end
|
66
|
+
else
|
67
|
+
raise ArgumentError, "#{s} should be a Hash, a String or nil"
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def convert_to_string(h)
|
72
|
+
h.map do |key_value|
|
73
|
+
key_value.map{|val| CGI::escape(val.to_s)}.join("=")
|
74
|
+
end.join("&")
|
75
|
+
end
|
76
|
+
|
77
|
+
def static
|
78
|
+
param_list = @options[:parameters].map{|k,v| %(<param name="#{k}" value="#{v}"/>) }.join("\n")
|
79
|
+
param_list += %(\n<param name="flashvars" value="#{convert_to_string(@options[:flashvars])}"/>) unless @options[:flashvars].empty?
|
80
|
+
html_options = @options[:html_options].map{|k,v| %(#{k}="#{v}")}.join(" ")
|
81
|
+
r = @view.javascript_tag(
|
82
|
+
%(swfobject.registerObject("#{@options[:id]}_container", "#{@options[:flash_version]}", #{convert_to_escaped_arguments(@options[:auto_install])});)
|
83
|
+
)
|
84
|
+
r << <<-"EOS".strip.html_safe
|
85
|
+
<div id="#{@options[:div_id]}"><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="#{@options[:width]}" height="#{@options[:height]}" id="#{@options[:id]}_container" #{html_options}>
|
86
|
+
<param name="movie" value="#{@source}" />
|
87
|
+
#{param_list}
|
88
|
+
<!--[if !IE]>-->
|
89
|
+
<object type="application/x-shockwave-flash" data="#{@source}" width="#{@options[:width]}" height="#{@options[:height]}" id="#{@options[:id]}">
|
90
|
+
#{param_list}
|
91
|
+
<!--<![endif]-->
|
92
|
+
#{@options[:alt]}
|
93
|
+
<!--[if !IE]>-->
|
94
|
+
</object>
|
95
|
+
<!--<![endif]-->
|
96
|
+
</object></div>
|
97
|
+
EOS
|
98
|
+
r << @view.javascript_tag(extend_js) if @options[:javascript_class]
|
99
|
+
r << library_check
|
100
|
+
r
|
101
|
+
end
|
102
|
+
|
103
|
+
def dynamic
|
104
|
+
@options[:html_options] = @options[:html_options].merge(:id => @options[:id])
|
105
|
+
@options[:parameters] = @options[:parameters].dup # don't modify the original parameters
|
106
|
+
args = convert_to_escaped_arguments(@source,
|
107
|
+
*@options.values_at(:div_id,:width,:height,:flash_version).map(&:to_s),
|
108
|
+
*@options.values_at(:auto_install,:flashvars,:parameters,:html_options)
|
109
|
+
)
|
110
|
+
|
111
|
+
preambule = @options[:switch_off_auto_hide_show] ? "swfobject.switchOffAutoHideShow();" : ""
|
112
|
+
r = @view.javascript_tag(preambule + "swfobject.embedSWF(#{args})")
|
113
|
+
r << @view.content_tag("div", @options[:alt].html_safe, :id => @options[:div_id])
|
114
|
+
r << @view.javascript_tag("swfobject.addDomLoadEvent(function(){#{extend_js}})") if @options[:javascript_class]
|
115
|
+
r << library_check
|
116
|
+
r
|
117
|
+
end
|
118
|
+
|
119
|
+
def convert_to_escaped_arguments(*values)
|
120
|
+
# Note: Rails used to escape <>& but it's now a setting
|
121
|
+
values.map(&:to_json).join(",").gsub('>', '\u003E').gsub('<', '\u003C').gsub('&', '\u0026')
|
122
|
+
end
|
123
|
+
|
124
|
+
def extend_js
|
125
|
+
args = @options[:initialize]
|
126
|
+
args = [args] unless args.is_a?(Array)
|
127
|
+
"SwfFu.setup($('##{@options[:id]}')[0], #{@options[:javascript_class]}, [#{convert_to_escaped_arguments(*args)}])"
|
128
|
+
end
|
129
|
+
|
130
|
+
def library_check
|
131
|
+
return "" unless 'development' == ENV['RAILS_ENV']
|
132
|
+
@view.javascript_tag(<<-"EOS")
|
133
|
+
if (typeof swfobject == 'undefined') {
|
134
|
+
document.getElementById('#{@options[:div_id]}').innerHTML = '<strong>Warning:</strong> SWFObject.js was not loaded properly. Make sure you require "swfobject" in your main javascript file.';
|
135
|
+
}
|
136
|
+
EOS
|
137
|
+
end
|
138
|
+
end #class Generator
|
139
|
+
end
|
data/lib/swf_fu.rb
CHANGED
@@ -1,2 +1,15 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
module SwfFu
|
2
|
+
class Engine < Rails::Engine
|
3
|
+
# Thanks to http://robots.thoughtbot.com/post/159805560/tips-for-writing-your-own-rails-engine for:
|
4
|
+
config.to_prepare do
|
5
|
+
ActionView::Helpers.class_eval { include SwfFuHelper }
|
6
|
+
end
|
7
|
+
# Thanks to http://jonswope.com/2010/07/25/rails-3-engines-plugins-and-static-assets/ for:
|
8
|
+
initializer "static assets" do |app|
|
9
|
+
app.middleware.use ::ActionDispatch::Static, "#{root}/public"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
::ActionView::Base.cattr_accessor :swf_default_options
|
14
|
+
::ActionView::Base.swf_default_options = {}
|
15
|
+
end
|
@@ -0,0 +1,261 @@
|
|
1
|
+
== Welcome to Rails
|
2
|
+
|
3
|
+
Rails is a web-application framework that includes everything needed to create
|
4
|
+
database-backed web applications according to the Model-View-Control pattern.
|
5
|
+
|
6
|
+
This pattern splits the view (also called the presentation) into "dumb"
|
7
|
+
templates that are primarily responsible for inserting pre-built data in between
|
8
|
+
HTML tags. The model contains the "smart" domain objects (such as Account,
|
9
|
+
Product, Person, Post) that holds all the business logic and knows how to
|
10
|
+
persist themselves to a database. The controller handles the incoming requests
|
11
|
+
(such as Save New Account, Update Product, Show Post) by manipulating the model
|
12
|
+
and directing data to the view.
|
13
|
+
|
14
|
+
In Rails, the model is handled by what's called an object-relational mapping
|
15
|
+
layer entitled Active Record. This layer allows you to present the data from
|
16
|
+
database rows as objects and embellish these data objects with business logic
|
17
|
+
methods. You can read more about Active Record in
|
18
|
+
link:files/vendor/rails/activerecord/README.html.
|
19
|
+
|
20
|
+
The controller and view are handled by the Action Pack, which handles both
|
21
|
+
layers by its two parts: Action View and Action Controller. These two layers
|
22
|
+
are bundled in a single package due to their heavy interdependence. This is
|
23
|
+
unlike the relationship between the Active Record and Action Pack that is much
|
24
|
+
more separate. Each of these packages can be used independently outside of
|
25
|
+
Rails. You can read more about Action Pack in
|
26
|
+
link:files/vendor/rails/actionpack/README.html.
|
27
|
+
|
28
|
+
|
29
|
+
== Getting Started
|
30
|
+
|
31
|
+
1. At the command prompt, create a new Rails application:
|
32
|
+
<tt>rails new myapp</tt> (where <tt>myapp</tt> is the application name)
|
33
|
+
|
34
|
+
2. Change directory to <tt>myapp</tt> and start the web server:
|
35
|
+
<tt>cd myapp; rails server</tt> (run with --help for options)
|
36
|
+
|
37
|
+
3. Go to http://localhost:3000/ and you'll see:
|
38
|
+
"Welcome aboard: You're riding Ruby on Rails!"
|
39
|
+
|
40
|
+
4. Follow the guidelines to start developing your application. You can find
|
41
|
+
the following resources handy:
|
42
|
+
|
43
|
+
* The Getting Started Guide: http://guides.rubyonrails.org/getting_started.html
|
44
|
+
* Ruby on Rails Tutorial Book: http://www.railstutorial.org/
|
45
|
+
|
46
|
+
|
47
|
+
== Debugging Rails
|
48
|
+
|
49
|
+
Sometimes your application goes wrong. Fortunately there are a lot of tools that
|
50
|
+
will help you debug it and get it back on the rails.
|
51
|
+
|
52
|
+
First area to check is the application log files. Have "tail -f" commands
|
53
|
+
running on the server.log and development.log. Rails will automatically display
|
54
|
+
debugging and runtime information to these files. Debugging info will also be
|
55
|
+
shown in the browser on requests from 127.0.0.1.
|
56
|
+
|
57
|
+
You can also log your own messages directly into the log file from your code
|
58
|
+
using the Ruby logger class from inside your controllers. Example:
|
59
|
+
|
60
|
+
class WeblogController < ActionController::Base
|
61
|
+
def destroy
|
62
|
+
@weblog = Weblog.find(params[:id])
|
63
|
+
@weblog.destroy
|
64
|
+
logger.info("#{Time.now} Destroyed Weblog ID ##{@weblog.id}!")
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
The result will be a message in your log file along the lines of:
|
69
|
+
|
70
|
+
Mon Oct 08 14:22:29 +1000 2007 Destroyed Weblog ID #1!
|
71
|
+
|
72
|
+
More information on how to use the logger is at http://www.ruby-doc.org/core/
|
73
|
+
|
74
|
+
Also, Ruby documentation can be found at http://www.ruby-lang.org/. There are
|
75
|
+
several books available online as well:
|
76
|
+
|
77
|
+
* Programming Ruby: http://www.ruby-doc.org/docs/ProgrammingRuby/ (Pickaxe)
|
78
|
+
* Learn to Program: http://pine.fm/LearnToProgram/ (a beginners guide)
|
79
|
+
|
80
|
+
These two books will bring you up to speed on the Ruby language and also on
|
81
|
+
programming in general.
|
82
|
+
|
83
|
+
|
84
|
+
== Debugger
|
85
|
+
|
86
|
+
Debugger support is available through the debugger command when you start your
|
87
|
+
Mongrel or WEBrick server with --debugger. This means that you can break out of
|
88
|
+
execution at any point in the code, investigate and change the model, and then,
|
89
|
+
resume execution! You need to install ruby-debug to run the server in debugging
|
90
|
+
mode. With gems, use <tt>sudo gem install ruby-debug</tt>. Example:
|
91
|
+
|
92
|
+
class WeblogController < ActionController::Base
|
93
|
+
def index
|
94
|
+
@posts = Post.all
|
95
|
+
debugger
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
So the controller will accept the action, run the first line, then present you
|
100
|
+
with a IRB prompt in the server window. Here you can do things like:
|
101
|
+
|
102
|
+
>> @posts.inspect
|
103
|
+
=> "[#<Post:0x14a6be8
|
104
|
+
@attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>,
|
105
|
+
#<Post:0x14a6620
|
106
|
+
@attributes={"title"=>"Rails", "body"=>"Only ten..", "id"=>"2"}>]"
|
107
|
+
>> @posts.first.title = "hello from a debugger"
|
108
|
+
=> "hello from a debugger"
|
109
|
+
|
110
|
+
...and even better, you can examine how your runtime objects actually work:
|
111
|
+
|
112
|
+
>> f = @posts.first
|
113
|
+
=> #<Post:0x13630c4 @attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>
|
114
|
+
>> f.
|
115
|
+
Display all 152 possibilities? (y or n)
|
116
|
+
|
117
|
+
Finally, when you're ready to resume execution, you can enter "cont".
|
118
|
+
|
119
|
+
|
120
|
+
== Console
|
121
|
+
|
122
|
+
The console is a Ruby shell, which allows you to interact with your
|
123
|
+
application's domain model. Here you'll have all parts of the application
|
124
|
+
configured, just like it is when the application is running. You can inspect
|
125
|
+
domain models, change values, and save to the database. Starting the script
|
126
|
+
without arguments will launch it in the development environment.
|
127
|
+
|
128
|
+
To start the console, run <tt>rails console</tt> from the application
|
129
|
+
directory.
|
130
|
+
|
131
|
+
Options:
|
132
|
+
|
133
|
+
* Passing the <tt>-s, --sandbox</tt> argument will rollback any modifications
|
134
|
+
made to the database.
|
135
|
+
* Passing an environment name as an argument will load the corresponding
|
136
|
+
environment. Example: <tt>rails console production</tt>.
|
137
|
+
|
138
|
+
To reload your controllers and models after launching the console run
|
139
|
+
<tt>reload!</tt>
|
140
|
+
|
141
|
+
More information about irb can be found at:
|
142
|
+
link:http://www.rubycentral.org/pickaxe/irb.html
|
143
|
+
|
144
|
+
|
145
|
+
== dbconsole
|
146
|
+
|
147
|
+
You can go to the command line of your database directly through <tt>rails
|
148
|
+
dbconsole</tt>. You would be connected to the database with the credentials
|
149
|
+
defined in database.yml. Starting the script without arguments will connect you
|
150
|
+
to the development database. Passing an argument will connect you to a different
|
151
|
+
database, like <tt>rails dbconsole production</tt>. Currently works for MySQL,
|
152
|
+
PostgreSQL and SQLite 3.
|
153
|
+
|
154
|
+
== Description of Contents
|
155
|
+
|
156
|
+
The default directory structure of a generated Ruby on Rails application:
|
157
|
+
|
158
|
+
|-- app
|
159
|
+
| |-- assets
|
160
|
+
| |-- images
|
161
|
+
| |-- javascripts
|
162
|
+
| `-- stylesheets
|
163
|
+
| |-- controllers
|
164
|
+
| |-- helpers
|
165
|
+
| |-- mailers
|
166
|
+
| |-- models
|
167
|
+
| `-- views
|
168
|
+
| `-- layouts
|
169
|
+
|-- config
|
170
|
+
| |-- environments
|
171
|
+
| |-- initializers
|
172
|
+
| `-- locales
|
173
|
+
|-- db
|
174
|
+
|-- doc
|
175
|
+
|-- lib
|
176
|
+
| `-- tasks
|
177
|
+
|-- log
|
178
|
+
|-- public
|
179
|
+
|-- script
|
180
|
+
|-- test
|
181
|
+
| |-- fixtures
|
182
|
+
| |-- functional
|
183
|
+
| |-- integration
|
184
|
+
| |-- performance
|
185
|
+
| `-- unit
|
186
|
+
|-- tmp
|
187
|
+
| |-- cache
|
188
|
+
| |-- pids
|
189
|
+
| |-- sessions
|
190
|
+
| `-- sockets
|
191
|
+
`-- vendor
|
192
|
+
|-- assets
|
193
|
+
`-- stylesheets
|
194
|
+
`-- plugins
|
195
|
+
|
196
|
+
app
|
197
|
+
Holds all the code that's specific to this particular application.
|
198
|
+
|
199
|
+
app/assets
|
200
|
+
Contains subdirectories for images, stylesheets, and JavaScript files.
|
201
|
+
|
202
|
+
app/controllers
|
203
|
+
Holds controllers that should be named like weblogs_controller.rb for
|
204
|
+
automated URL mapping. All controllers should descend from
|
205
|
+
ApplicationController which itself descends from ActionController::Base.
|
206
|
+
|
207
|
+
app/models
|
208
|
+
Holds models that should be named like post.rb. Models descend from
|
209
|
+
ActiveRecord::Base by default.
|
210
|
+
|
211
|
+
app/views
|
212
|
+
Holds the template files for the view that should be named like
|
213
|
+
weblogs/index.html.erb for the WeblogsController#index action. All views use
|
214
|
+
eRuby syntax by default.
|
215
|
+
|
216
|
+
app/views/layouts
|
217
|
+
Holds the template files for layouts to be used with views. This models the
|
218
|
+
common header/footer method of wrapping views. In your views, define a layout
|
219
|
+
using the <tt>layout :default</tt> and create a file named default.html.erb.
|
220
|
+
Inside default.html.erb, call <% yield %> to render the view using this
|
221
|
+
layout.
|
222
|
+
|
223
|
+
app/helpers
|
224
|
+
Holds view helpers that should be named like weblogs_helper.rb. These are
|
225
|
+
generated for you automatically when using generators for controllers.
|
226
|
+
Helpers can be used to wrap functionality for your views into methods.
|
227
|
+
|
228
|
+
config
|
229
|
+
Configuration files for the Rails environment, the routing map, the database,
|
230
|
+
and other dependencies.
|
231
|
+
|
232
|
+
db
|
233
|
+
Contains the database schema in schema.rb. db/migrate contains all the
|
234
|
+
sequence of Migrations for your schema.
|
235
|
+
|
236
|
+
doc
|
237
|
+
This directory is where your application documentation will be stored when
|
238
|
+
generated using <tt>rake doc:app</tt>
|
239
|
+
|
240
|
+
lib
|
241
|
+
Application specific libraries. Basically, any kind of custom code that
|
242
|
+
doesn't belong under controllers, models, or helpers. This directory is in
|
243
|
+
the load path.
|
244
|
+
|
245
|
+
public
|
246
|
+
The directory available for the web server. Also contains the dispatchers and the
|
247
|
+
default HTML files. This should be set as the DOCUMENT_ROOT of your web
|
248
|
+
server.
|
249
|
+
|
250
|
+
script
|
251
|
+
Helper scripts for automation and generation.
|
252
|
+
|
253
|
+
test
|
254
|
+
Unit and functional tests along with fixtures. When using the rails generate
|
255
|
+
command, template test files will be generated for you and placed in this
|
256
|
+
directory.
|
257
|
+
|
258
|
+
vendor
|
259
|
+
External libraries that the application depends on. Also includes the plugins
|
260
|
+
subdirectory. If the app has frozen rails, those gems also go here, under
|
261
|
+
vendor/rails/. This directory is in the load path.
|