Wraps a Source using Rails’
AssetTagHelper#image_path
, which can turn path URLs (e.g.
‘/images/my_avatar.png’) into absolute URLs( e.g. ‘’).
For each key in options
replaces ‘#{key}’ in
string
with the corresponding value in options
.
string
should be of the form
‘…#{variable_a}…#{variable_b}…’. Note the single
quotes. Double quotes will cause the variables to be substituted
before this method is run, which is almost certainly
<strong>not</strong> what you want.
# File lib/avatar/source/wrapper/string_substitution_source_wrapper.rb, line 40 def apply_substitution(string, options) returning(string.dup) do |result| options.each do |k,v| result.gsub!(Regexp.new('#\{' + "#{k}" + '\}'), "#{v}") end end end
# File lib/avatar/source/wrapper/string_substitution_source_wrapper.rb, line 29 def default_options=(opts) @default_options = opts || {} end
# File lib/avatar/source/wrapper/string_substitution_source_wrapper.rb, line 48 def substitution_needed?(string) string =~ /#\{.*\}/ end
Passes url
to AssetTagHelper#image_path
. Raises
an error if it cannot generate a fully-qualified URI. Try setting
ActionController::Base.asset_host
to avoid this error.
# File lib/avatar/source/wrapper/string_substitution_source_wrapper.rb, line 23 def wrap(url, person, options = {}) # url will never be nil b/c of guarantee in AbstractSourceWrapper result = apply_substitution(url, self.default_options.merge(options)) substitution_needed?(result) ? nil : result end
# File lib/avatar/source/wrapper/string_substitution_source_wrapper.rb, line 14 def initialize(source, default_options = {}) super(source) self.default_options = default_options || {} end