Module: YMDP::ApplicationView
- Extended by:
- ApplicationView
- Included in:
- ApplicationView, Compiler::Template::View, View
- Defined in:
- lib/ymdp/view/application_view.rb
Overview
Contains all the functions which are available from inside a view file, whether that view is HTML, JavaScript or CSS.
Instance Method Summary collapse
- #combo(filenames, options = {}) ⇒ Object
-
#english_languages ⇒ Object
Returns an array of country codes of English-speaking countries supported by the application, based on the language-specific folders located in “app/assets/yrb”.
-
#include_firebug_lite ⇒ Object
Renders a link to include Firebug Lite for debugging JavaScript in Internet Explorer.
-
#javascript_include(filename, options = {}) ⇒ Object
Includes a JavaScript file in a view.
-
#render(params) ⇒ Object
Renders a partial into the current view.
-
#supported_languages ⇒ Object
Returns an array of the country codes of all languages supported by the application, which is determined by the language-specific folders in “app/assets/yrb”.
- #translations ⇒ Object
Instance Method Details
#combo(filenames, options = {}) ⇒ Object
83 84 85 86 87 88 |
# File 'lib/ymdp/view/application_view.rb', line 83 def combo(filenames, ={}) paths = filenames.map do |filename| "javascripts/#{filename}" end.join("&") "<script src=\"/yahoo/mail/combo?#{paths}\"></script>" end |
#english_languages ⇒ Object
Returns an array of country codes of English-speaking countries supported by the application, based on the language-specific folders located in “app/assets/yrb”.
Examples
english_languages
# => ["en-US", "en-AU", "en-SG", "en-MY"]
44 45 46 47 48 |
# File 'lib/ymdp/view/application_view.rb', line 44 def english_languages supported_languages.select do |lang| lang =~ /^en/ end end |
#include_firebug_lite ⇒ Object
Renders a link to include Firebug Lite for debugging JavaScript in Internet Explorer.
92 93 94 |
# File 'lib/ymdp/view/application_view.rb', line 92 def include_firebug_lite javascript_include "http://getfirebug.com/releases/lite/1.2/firebug-lite-compressed.js" end |
#javascript_include(filename, options = {}) ⇒ Object
Includes a JavaScript file in a view. If the filename is a full path, the JavaScript file will be linked as an external asset. If not, the file will linked as a local asset located in the YMDP application’s assets directory.
Local JavaScript Assets
javascript_include("application.js")
will produce:
<script src='/om/assets/3ifh3b2kjf_1/assets/javascripts/application.js'
type='text/javascript charset='utf-8'></script>
External JavaScript Assets
javascript_include("http://www.myserver.com/javascripts/application.js")
will produce:
<script src='http://www.myserver.com/javascripts/application.js' type='text/javascript
charset='utf-8'></script>
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/ymdp/view/application_view.rb', line 72 def javascript_include(filename, ={}) if filename == :defaults render_default_javascripts() filename = "defaults.js" end unless filename =~ /^http/ filename = "#{@assets_directory}/javascripts/#{filename}" end "<script src='#{filename}' type='text/javascript' charset='utf-8'></script>" end |
#render(params) ⇒ Object
Renders a partial into the current view. HTML partial names must be preceded with an underscore.
Rendering an HTML partial
HTML partials are located in app/views
. HTML view files can be Haml or ERB. Haml is recommended and takes preference. HTML partials are named _filename.html.haml
or _filename.html.erb
.
render :partial => 'sidebar'
will find app/views/_sidebar.html.haml
or app/views/_sidebar.html.erb
and render its contents into the current view.
Specify a full path to indicate a specific template.
render :partial => 'sidebar.html.erb'
will find app/views/_sidebar.html.erb'
and render it even if app/views/_sidebar.html.haml
exists.
render :partial => 'shared/sidebar'
will find app/views/shared/_sidebar.html.haml
and render its contents into the current view.
Rendering a JavaScript partial
You can render a single JavaScript file or send an array to concatenate a set of JavaScript files together asone script block.
Rendering a single JavaScript partial
JavaScript partials are located in app/javascripts
and are named filename.js
render :javascript => 'application'
will find app/javascripts/application.js
and render its contents into the current view in an inline script block.
render :javascript => 'shared/sidebar'
will find app/javascripts/shared/sidebar.js
and render its contents into the current view in an inline script block.
Rendering a stylesheet partial
Stylesheets are located at app/stylesheets
and are named filename.css
Rendering multiple partials
Pass an array to render
to combine multiple files into a single inline block. This is useful for compression and validation, as it allows a set of files to be compressed or validated in a single context.
render :javascript => ['application', 'flash', 'debug']
will combine the contents of app/javascripts/application.js
, app/javascripts/application.js
, and app/javascripts/application.js
into a single script block in the current view.
Pass a :filename
parameter to set the name of the combined file. Currently the combined file only exists on disc while it’s being compressed and/or validated, but in the future this may be expanded to save multiple files as a single external asset.
render :javascript => ['application', 'flash', 'debug'], :filename => 'javascripts'
Currently the :filename
parameter is simply a convenience.
Multiple partials of any type can be rendered.
For example:
render :partial => ['header', 'footer', 'sidebar'], :filename => 'html_layouts'
will find app/views/_header.html.haml
, app/views/_footer.html.haml
, and app/views/_sidebar.html.haml
and write them to a temporary file called tmp/html_layouts
before rendering that file into the current view.
This feature is intended mainly for JavaScript and CSS.
For example:
render :stylesheet => ['application', 'colors'], :filename => 'styles'
will render app/stylesheets/application.css
and app/stylesheets/colors.css
as a single temporary file called tmp/styles
before rendering that file into the current view.
If compression and validation options are turned on, the resulting temporary file will be compressed and/or validated before being rendered into the current view. This will result in a more efficient compression and a more effective validation.
188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/ymdp/view/application_view.rb', line 188 def render(params) output = [] unless params.has_key?(:tags) params[:tags] = true end output << render_html_partial(params) output << render_javascript_partial(params) if params[:javascript] output << render_stylesheet_partial(params) if params[:stylesheet] output.flatten.join("\n") end |
#supported_languages ⇒ Object
Returns an array of the country codes of all languages supported by the application, which is determined by the language-specific folders in “app/assets/yrb”.
Examples
supported_languages
# => ["en-US", "de-DE", "es-ES", "es-MX"]
20 21 22 |
# File 'lib/ymdp/view/application_view.rb', line 20 def supported_languages YAML.load_file("./config/idiom.yml")["locales"].keys end |
#translations ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/ymdp/view/application_view.rb', line 24 def translations @translations ||= {} Dir["./config/locales/**/*.yml"].each do |file| @translations.merge!(YAML.load_file(file)) do |key, old, new| old.merge new end end @translations end |