Module: Princely::AssetSupport

Defined in:
lib/princely/asset_support.rb

Instance Method Summary collapse

Instance Method Details

#asset_file_path(asset) ⇒ Object


17
18
19
20
21
# File 'lib/princely/asset_support.rb', line 17

def asset_file_path(asset)
  # Remove /assets/ from generated names and try and find a matching asset
  Rails.application.assets ||= Sprockets::Environment.new
  Rails.application.assets.find_asset(asset.gsub(%r{/assets/}, "")).try(:pathname) || asset
end

#localize_html_string(html_string, asset_path = nil) ⇒ Object


3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/princely/asset_support.rb', line 3

def localize_html_string(html_string, asset_path = nil)
  html_string = html_string.to_str
  # Make all paths relative, on disk paths...
  html_string.gsub!(".com:/",".com/") # strip out bad attachment_fu URLs
  html_string.gsub!( /src=["']+([^:]+?)["']/i ) do |m|
    asset_src = asset_path ? "#{asset_path}/#{$1}" : asset_file_path($1)
    %Q{src="#{asset_src}"} # re-route absolute paths
  end

  # Remove asset ids on images with a regex
  html_string.gsub!( /src=["'](\S+\?\d*)["']/i ) { |m| %Q{src="#{$1.split('?').first}"} }
  html_string
end