Skip to content

Instantly share code, notes, and snippets.

@juggy
Last active November 28, 2016 21:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juggy/18abd8d5370623c6ef41e25955293330 to your computer and use it in GitHub Desktop.
Save juggy/18abd8d5370623c6ef41e25955293330 to your computer and use it in GitHub Desktop.
module Core
module AssetHelper
def self.root_path
String === Rails.root ? Pathname.new(Rails.root) : Rails.root
end
def self.add_extension(filename, extension)
filename.to_s.split('.').include?(extension) ? filename : "#{filename}.#{extension}"
end
def core_stylesheet_link_tag(*sources)
css_dir = AssetHelper.root_path.join('public', 'stylesheets')
css_text = sources.collect do |source|
source = AssetHelper.add_extension(source, 'css')
"<style type='text/css'>#{File.read(css_dir.join(source))}</style>"
end.join("\n")
css_text.respond_to?(:html_safe) ? css_text.html_safe : css_text
end
def core_image_tag(img, options = {})
image_tag "file:///#{AssetHelper.root_path.join('public', 'images', img)}", options
end
def core_javascript_src_tag(jsfile, options = {})
jsfile = AssetHelper.add_extension(jsfile, 'js')
src = "file:///#{AssetHelper.root_path.join('public', 'javascripts', jsfile)}"
content_tag('script', '', { 'type' => Mime::JS, 'src' => path_to_javascript(src) }.merge(options))
end
def core_javascript_include_tag(*sources)
js_text = sources.collect { |source| core_javascript_src_tag(source, {}) }.join("\n")
js_text.respond_to?(:html_safe) ? js_text.html_safe : js_text
end
end
end
require 'open-uri'
module Core
module Assets
ASSET_URL_REGEX = /url\(['"]?([^'"]+?)['"]?\)/
def self.core_asset_base64(path)
asset = Rails.application.assets.find_asset(path)
throw "Could not find asset '#{path}'" if asset.nil?
base64 = Base64.encode64(asset.to_s).gsub(/\s+/, '')
"data:#{asset.content_type};base64,#{Rack::Utils.escape(base64)}"
end
def self.core_stylesheet_link_tag(*sources)
stylesheet_contents = sources.collect do |source|
source = AssetHelper.add_extension(source, 'css')
"<style type='text/css'>#{read_asset(source)}</style>"
end.join("\n")
stylesheet_contents.gsub(ASSET_URL_REGEX) do
if Regexp.last_match[1].starts_with?('data:')
"url(#{Regexp.last_match[1]})"
else
"url(#{core_asset_path(Regexp.last_match[1])})"
end
end.html_safe
end
def self.core_image_tag(img, options = {})
image_tag core_asset_path(img), options
end
def self.core_javascript_src_tag(jsfile, options = {})
jsfile = AssetHelper.add_extension(jsfile, 'js')
javascript_include_tag core_asset_path(jsfile), options
end
def self.core_javascript_include_tag(*sources)
sources.collect do |source|
source = AssetHelper.add_extension(source, 'js')
"<script type='text/javascript'>#{read_asset(source)}</script>"
end.join("\n").html_safe
end
def self.core_asset_path(asset)
if (pathname = asset_pathname(asset).to_s) =~ URI_REGEXP
pathname
else
"file:///#{pathname}"
end
end
private
# borrowed from actionpack/lib/action_view/helpers/asset_url_helper.rb
URI_REGEXP = %r{^[-a-z]+://|^(?:cid|data):|^//}
def self.asset_pathname(source)
if precompiled_or_absolute_asset?(source)
asset = ActionController::Base.helpers.asset_path(source)
if (pathname = set_protocol(asset)) =~ URI_REGEXP
# asset_path returns an absolute URL using asset_host if asset_host is set
pathname
else
::File.join(Rails.public_path, asset.sub(/\A#{Rails.application.config.action_controller.relative_url_root}/, ''))
end
else
asset = Rails.application.assets.find_asset(source)
asset ? asset.pathname : ::File.join(Rails.public_path, source)
end
end
# will prepend a http or default_protocol to a protocol relative URL
# or when no protcol is set.
def self.set_protocol(source)
protocol = 'https'
if source[0, 2] == '//'
source = [protocol, ':', source].join
elsif source[0] != '/' && !source[0, 8].include?('://')
source = [protocol, '://', source].join
end
source
end
def self.precompiled_or_absolute_asset?(source)
Rails.configuration.assets.compile == false ||
source.to_s[0] == '/' ||
source.to_s.match(/\Ahttps?\:\/\//)
end
def self.read_asset(source)
if precompiled_or_absolute_asset?(source)
if (pathname = asset_pathname(source)) =~ URI_REGEXP
read_from_uri(pathname)
elsif ::File.file?(pathname)
IO.read(pathname)
end
else
Rails.application.assets.find_asset(source).to_s
end
end
def self.read_from_uri(uri)
encoding = ':UTF-8' if RUBY_VERSION > '1.8'
asset = open(uri, "r#{encoding}") { |f| f.read }
asset = gzip(asset)
asset
end
def self.gzip(asset)
stringified_asset = StringIO.new(asset)
gzipper = Zlib::GzipReader.new(stringified_asset)
gzipper.read
rescue Zlib::GzipFile::Error
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment