Skip to content

Instantly share code, notes, and snippets.

@pascalbetz
Last active December 2, 2015 09:55
Show Gist options
  • Save pascalbetz/1c6481c8c8c8d11edf9e to your computer and use it in GitHub Desktop.
Save pascalbetz/1c6481c8c8c8d11edf9e to your computer and use it in GitHub Desktop.
require 'uri'
require 'pry'
module Lotus
module Helpers
# Helper methods to generate asset-paths
#
# @since 0.6.0
# @api public
module AssetUriHelpers
# HTTP-path-separator according to https://tools.ietf.org/html/rfc1738 - 3.3 HTTP
PATH_SEPARATOR = '/'.freeze
ASSETS_ROOT_DIRECTORY = 'assets'.freeze
# Generates the application-specific relative paths for assets
def asset_path(*args)
assets_prefix = asset_config.prefix
args.push('') if args.empty?
path_elements = ['', ASSETS_ROOT_DIRECTORY]
path_elements.concat(assets_prefix.split(PATH_SEPARATOR).compact) if !assets_prefix.empty?
path_elements.concat(args)
path_elements.join(PATH_SEPARATOR)
end
# Generates the application-specific absolute URL for assets
def asset_url(*args)
url_path = asset_path(args)
url_scheme = application_config.scheme.to_s
url_host = application_config.host.to_s # TODO: to_s should be done in application_config
url_port = application_config.port.to_i # TODO: to_i should be done in application_config
url_port = nil if url_port <= 0 # TODO: why could it <= 0 ?
#binding.pry
URI::Generic.build({scheme: url_scheme, host: url_host, port: url_port, path: url_path}).to_s
end
private
def assets_class_name
"#{application_module_name}::Assets"
end
def application_class_name
"#{application_module_name}::Application"
end
def application_module_name
self.class.name.split('::').first # extract app-name from class-name
end
def asset_config
@_asset_config ||= Kernel.const_get(assets_class_name).configuration
end
def application_config
@_application_config ||= Kernel.const_get(application_class_name).configuration
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment