Included Modules

Merb::AssetsMixin

The AssetsMixin module provides a number of helper methods to views for linking to assets and other pages, dealing with JavaScript and CSS.

Merb provides views with convenience methods for links images and other assets.

Constants

ABSOLUTE_PATH_REGEXP

Public Class Methods

append_random_query_string?(intention, allow_default = true) click to toggle source

This tests whether a random query string shall be appended to a url.

Basically, you tell it your intention and if it’s ok to use default config values, and it will either use your intention or the value set in Merb::Config[:reload_templates]

@example

  Merb::AssetsMixin.append_random_query_string?(options[:reload])
  Merb::AssetsMixin.append_random_query_string?(options[:reload], !absolute)

@param [Boolean] intention: true if a random string shall be appended @param [Boolean] allow_default: true if it’s ok to use Merb::Config[:reload_templates] @return [Boolean] true if a random query string shall be appended

    # File lib/merb-assets/assets_mixin.rb, line 25
25:     def self.append_random_query_string?(intention, allow_default = true)
26:       intention.nil? && allow_default ? Merb::Config[:reload_templates] : intention
27:     end
append_timestamp_query_string?(intention, allow_default = true) click to toggle source

This tests whether a timestamp query string shall be appended to a url.

@see self.append_random_query_string?

@example

  Merb::AssetsMixin.append_timestamp_query_string?(options[:timestamp])
  Merb::AssetsMixin.append_timestamp_query_string?(options[:timestamp], !absolute)

@param [Boolean] intention: true if a timestamp string shall be appended @param [Boolean] allow_default: true if it’s ok to use Merb::Plugins.config[:asset_helpers][:asset_timestamp] @return [Boolean] true if a timestamp query string shall be appended

    # File lib/merb-assets/assets_mixin.rb, line 40
40:     def self.append_timestamp_query_string?(intention, allow_default = true)
41:       intention.nil? && allow_default ? Merb::Plugins.config[:asset_helpers][:asset_timestamp] : intention
42:     end

Public Instance Methods

asset_exists?(asset_type, asset_path) click to toggle source

Parameters

asset_type: A symbol representing the type of the asset. asset_path: The path to the asset

Returns

exists?

Examples

This tests whether a give asset exists in the file system.

     # File lib/merb-assets/assets_mixin.rb, line 98
 98:     def asset_exists?(asset_type, asset_path)
 99:       File.exists?(Merb.root / asset_path(asset_type, asset_path, true))
100:     end
css_include_tag(*stylesheets) click to toggle source

Generate CSS include tag(s).

@param [Array<*String, Hash>] stylesheets

  The stylesheets to include. If the last element is a Hash, it will be
  used as options (see below). If ".css" is left out from the stylesheet
  names, it will be added to them.

Options

@option opts charset

  Charset which will be used as value for charset attribute

@option opts bundle

  The name of the bundle the stylesheets should be combined into.

@option opts media

  The media attribute for the generated link element. Defaults to :all.

@option opts prefix

  prefix to add to include tag, overrides any set in Merb::Plugins.config[:asset_helpers][:css_prefix]

@option opts suffix

  suffix to add to include tag, overrides any set in Merb::Plugins.config[:asset_helpers][:css_suffix]

@option opts reload

  Override the Merb::Config[:reload_templates] value. If true, a random query param will be appended
  to the css url

@option opts [Boolean, String] :timestamp

  Override the Merb::Plugins.config[:asset_helper][:asset_timestamp] value. If true, a timestamp query param will be appended
  to the image url. The value will be File.mtime(Merb.dir_for(:public) / path).
  If String is passed than it will be used as the timestamp.

@example

  css_include_tag 'style'
  # => <link href="/stylesheets/style.css" media="all" rel="Stylesheet" type="text/css" charset="utf-8" />

  css_include_tag 'style.css', 'layout'
  # => <link href="/stylesheets/style.css" media="all" rel="Stylesheet" type="text/css" charset="utf-8" />
  #    <link href="/stylesheets/layout.css" media="all" rel="Stylesheet" type="text/css" charset="utf-8" />

  css_include_tag :menu
  # => <link href="/stylesheets/menu.css" media="all" rel="Stylesheet" type="text/css" charset="utf-8" />

  css_include_tag :style, :screen
  # => <link href="/stylesheets/style.css" media="all" rel="Stylesheet" type="text/css" charset="utf-8" />
  #    <link href="/stylesheets/screen.css" media="all" rel="Stylesheet" type="text/css" charset="utf-8" />

 css_include_tag :style, :media => :print
 # => <link href="/stylesheets/style.css" media="print" rel="Stylesheet" type="text/css" charset="utf-8" />

 css_include_tag :style, :charset => 'iso-8859-1'
 # => <link href="/stylesheets/style.css" media="print" rel="Stylesheet" type="text/css" charset="iso-8859-1" />

 css_include_tag :style, :prefix => "http://static.example.com"
 # => <link href="http://static.example.com/stylesheets/style.css" media="print" rel="Stylesheet" type="text/css" />

 css_include_tag :style, :suffix => ".#{MyApp.version}"
 # => <link href="/stylesheets/style.1.0.0.css" media="print" rel="Stylesheet" type="text/css" />

@return [String] The CSS include tag(s)

     # File lib/merb-assets/assets_mixin.rb, line 622
622:     def css_include_tag(*stylesheets)
623:       options = stylesheets.last.is_a?(Hash) ? stylesheets.pop : {}
624:       return nil if stylesheets.empty?
625: 
626:       css_prefix = options[:prefix] || Merb::Plugins.config[:asset_helpers][:css_prefix]
627:       css_suffix = options[:suffix] || Merb::Plugins.config[:asset_helpers][:css_suffix]
628: 
629:       if (bundle_name = options[:bundle]) && Merb::Assets.bundle? && stylesheets.size > 1
630:         bundler = Merb::Assets::StylesheetAssetBundler.new(bundle_name, *stylesheets)
631:         bundled_asset = bundler.bundle!
632:         return css_include_tag(bundled_asset)
633:       end
634: 
635:       tags = ""
636: 
637:       reload = options.delete(:reload)
638:       timestamp = options.delete(:timestamp)
639:       for stylesheet in stylesheets
640:         href = css_prefix.to_s + asset_path(:stylesheet, stylesheet)
641:         
642:         if css_suffix
643:           ext_length = ASSET_FILE_EXTENSIONS[:stylesheet].length + 1
644:           href.insert(-ext_length,css_suffix)
645:         end
646:         
647:         href = append_query_string(href, reload, timestamp)
648: 
649:         attrs = {
650:           :href => href,
651:           :type => "text/css",
652:           :rel => "Stylesheet",
653:           :charset => options[:charset] || "utf-8",
654:           :media => options[:media] || :all
655:         }
656:         tags << %{<link #{attrs.to_xml_attributes} />}
657:       end
658: 
659:       return tags
660:     end
escape_js(javascript) click to toggle source

Parameters

javascript

Text to escape for use in JavaScript.

Examples

  escape_js("'Lorem ipsum!' -- Some guy")
    # => "\\'Lorem ipsum!\\' -- Some guy"

  escape_js("Please keep text\nlines as skinny\nas possible.")
    # => "Please keep text\\nlines as skinny\\nas possible."
     # File lib/merb-assets/assets_mixin.rb, line 211
211:     def escape_js(javascript)
212:       (javascript || '').gsub('\','\0\0').gsub(/\r\n|\n|\r/, "\\n").gsub(/["']/) { |m| "\\#{m}" }
213:     end
image_tag(img, opts={}) click to toggle source

Generate IMG tag

@param [String, #] img The image path. @param [Hash] opts Additional options for the image tag (see below). @option opts [String] :path

  Sets the path prefix for the image. Defaults to "/images/" or whatever
  is specified in Merb::Config. This is ignored if img is an absolute
  path or full URL.

@option opts [Boolean] :reload

  Override the Merb::Config[:reload_templates] value. If true, a random query param will be appended
  to the image url

@option opts [Boolean, String] :timestamp

  Override the Merb::Plugins.config[:asset_helpers][:asset_timestamp] value. If true, a timestamp query param will be appended
  to the image url. The value will be File.mtime(Merb.dir_for(:public) / path).
  If String is passed than it will be used as the timestamp.

All other options set HTML attributes on the tag.

@example

  image_tag('foo.gif')
  # => <img src='/images/foo.gif' />

  image_tag('foo.gif', :class => 'bar')
  # => <img src='/images/foo.gif' class='bar' />

  image_tag('foo.gif', :path => '/files/')
  # => <img src='/files/foo.gif' />

  image_tag('http://test.com/foo.gif')
  # => <img src="http://test.com/foo.gif">

  image_tag('charts', :path => '/dynamic/')
  or
  image_tag('/dynamic/charts')
  # => <img src="/dynamic/charts">

@return [String]

     # File lib/merb-assets/assets_mixin.rb, line 176
176:     def image_tag(img, opts={})
177:       return "" if img.blank?
178:       if img[0].chr == Merb::Const::SLASH
179:         opts[:src] = "#{Merb::Config[:path_prefix]}#{img}"
180:       else
181:         opts[:path] ||=
182:           if img =~ ABSOLUTE_PATH_REGEXP
183:             absolute = true
184:             ''
185:           else
186:             "#{Merb::Config[:path_prefix]}/images/"
187:           end
188:         opts[:src] ||= opts.delete(:path) + img
189:       end
190: 
191:       opts[:src] = append_query_string(opts[:src],
192:                                        opts.delete(:reload),
193:                                        opts.delete(:timestamp),
194:                                        !absolute) 
195:       
196:       %{<img #{ opts.to_xml_attributes } />}
197:     end
include_required_css(options = {}) click to toggle source

A method used in the layout of an application to create ++ tags for CSS stylesheets required in in templates and subtemplates using require_css.

Parameters

options

Options to pass to css_include_tag.

Returns

String

The CSS tag.

Options

:bundle<~to_s>

The name of the bundle the stylesheets should be combined into.

:media<~to_s>

The media attribute for the generated link element. Defaults to :all.

Examples

  # my_action.herb has a call to require_css 'style'
  # File: layout/application.html.erb
  include_required_css
  # => <link href="/stylesheets/style.css" media="all" rel="Stylesheet" type="text/css"/>

  # my_action.herb has a call to require_css 'style', 'ie-specific'
  # File: layout/application.html.erb
  include_required_css
  # => <link href="/stylesheets/style.css" media="all" rel="Stylesheet" type="text/css"/>
  #    <link href="/stylesheets/ie-specific.css" media="all" rel="Stylesheet" type="text/css"/>
     # File lib/merb-assets/assets_mixin.rb, line 475
475:     def include_required_css(options = {})
476:       required_css(options).map { |req_js| css_include_tag(*req_js) }.join
477:     end
include_required_js(options = {}) click to toggle source

A method used in the layout of an application to create +