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.
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
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
asset_type
exists?
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
Automatically generates link for CSS and JS
We want all possible matches in the FileSys up to the action name
Given: controller_name = "namespace/controller" action_name = "action"
@example If all files are present should generate link/script tags for:
namespace.(css|js) namespace/controller.(css|js) namespace/controller/action.(css|js)
@return [String] html
# File lib/merb-assets/assets_mixin.rb, line 56 56: def auto_link 57: [auto_link_css, auto_link_js].join(Merb::Const::NEWLINE) 58: end
We want all possible matches in the file system upto the action name for CSS. The reason for separating auto_link for CSS and JS is performance concerns with page loading. See Yahoo performance rules (developer.yahoo.com/performance/rules.html)
@return [String] html
# File lib/merb-assets/assets_mixin.rb, line 66 66: def auto_link_css 67: auto_link_paths.map do |path| 68: asset_exists?(:stylesheet, path) ? css_include_tag(path) : nil 69: end.compact.join(Merb::Const::NEWLINE) 70: end
none
html
We want all possible matches in the file system upto the action name for JS. The reason for separating auto_link for CSS and JS is performance concerns with page loading. See Yahoo performance rules (developer.yahoo.com/performance/rules.html)
# File lib/merb-assets/assets_mixin.rb, line 83 83: def auto_link_js 84: auto_link_paths.map do |path| 85: asset_exists?(:javascript, path) ? js_include_tag(path) : nil 86: end.compact.join(Merb::Const::NEWLINE) 87: end
none
paths
This is an auxiliary method which returns an array of all possible asset paths for the current controller/action.
# File lib/merb-assets/assets_mixin.rb, line 111 111: def auto_link_paths 112: paths = (controller_name / action_name).split(Merb::Const::SLASH) 113: first = paths.shift 114: paths.inject( [first] ) do |memo, val| 115: memo.push [memo.last, val].join(Merb::Const::SLASH) 116: end 117: end
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.
@option opts
Charset which will be used as value for charset attribute
@option opts
The name of the bundle the stylesheets should be combined into.
@option opts
The media attribute for the generated link element. Defaults to :all.
@option opts
prefix to add to include tag, overrides any set in Merb::Plugins.config[:asset_helpers][:css_prefix]
@option opts
suffix to add to include tag, overrides any set in Merb::Plugins.config[:asset_helpers][:css_suffix]
@option opts
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
javascript | Text to escape for use in JavaScript. |
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
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
A method used in the layout of an application to create ++ tags for CSS stylesheets required in in templates and subtemplates using require_css.
options | Options to pass to css_include_tag. |
String | The CSS tag. |
: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. |
# 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