require 'avatar/source/wrapper/abstract_source_wrapper'
require 'action_view/helpers/asset_tag_helper'
module Avatar # :nodoc:
module Source # :nodoc:
module Wrapper
# Wraps a Source using Rails' AssetTagHelper#image_path
,
# which can turn path URLs (e.g. '/images/my_avatar.png')
# into absolute URLs( e.g. 'http://assets.mysite.com/images/my_avatar.png').
class StringSubstitutionSourceWrapper < AbstractSourceWrapper
attr_accessor :default_options
def initialize(source, default_options = {})
super(source)
self.default_options = default_options || {}
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.
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
def default_options=(opts)
@default_options = opts || {}
end
# 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 not what you want.
def apply_substitution(string, options)
returning(string.dup) do |result|
options.each do |k,v|
result.gsub!(Regexp.new('#\{' + "#{k}" + '\}'), "#{v}")
end
end
end
def substitution_needed?(string)
string =~ /#\{.*\}/
end
end
end
end
end