Class: Avatar::Source::Wrapper::RailsAssetSourceWrapper

Inherits:
AbstractSourceWrapper show all
Defined in:
lib/avatar/source/wrapper/rails_asset_source_wrapper.rb

Overview

Wraps a Source using Rails’ AssetTagHelper#image_path, which can turn path URLs (e.g. ‘/images/my_avatar.png’) into absolute URLs( e.g. ‘’).

Instance Attribute Summary

Attributes inherited from AbstractSourceWrapper

#underlying_source

Instance Method Summary collapse

Methods inherited from AbstractSourceWrapper

#avatar_url_for

Methods included from AbstractSource

#avatar_url_for

Constructor Details

#initialize(source) ⇒ RailsAssetSourceWrapper

Returns a new instance of RailsAssetSourceWrapper.



17
18
19
20
21
# File 'lib/avatar/source/wrapper/rails_asset_source_wrapper.rb', line 17

def initialize(source)
  super
  @url_helper = Object.new
  @url_helper.extend(ActionView::Helpers::AssetTagHelper)
end

Instance Method Details

#wrap(url, person, options = {}) ⇒ Object

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.



27
28
29
30
31
32
# File 'lib/avatar/source/wrapper/rails_asset_source_wrapper.rb', line 27

def wrap(url, person, options = {})
  # url will never be nil b/c of guarantee in AbstractSourceWrapper
  result = url_helper.image_path(url)
  raise "could not generate protocol and host for #{url}.  Have you set ActionController::Base.asset_host?" unless result =~ /^http[s]?\:\/\//
  result
end