Last active
August 29, 2015 14:01
-
-
Save CasperLaiTW/d4d5a9a3b7cf5c2bbbeb to your computer and use it in GitHub Desktop.
Extend fireapp viewhelper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This method is base on fireapp docs example. | |
# The url will join {http_path} variable. | |
module ViewHelpers | |
def new_link_to(name, href, options={}) | |
href = ensure_path(href, '') | |
if href == File.join(Helpers::Path.get_http_path, request.path) | |
options[:class] ||='' | |
options[:class] += "active" | |
end | |
link_to name, href, options | |
end | |
def link_to(name, href, html_options = {}) | |
html_options = html_options.stringify_keys | |
href = ensure_path(href, '') | |
confirm = html_options.delete("confirm") | |
onclick = "if (!confirm('#{html_escape(confirm)}')) return false;" if confirm | |
content_tag(:a, name, html_options.merge(:href => href, :onclick=>onclick)) | |
end | |
def image_tag(src, html_options = {}) | |
src = "#{Compass.configuration.images_dir}/#{src}" unless src =~ /^(https?:|\/)/ | |
src = ensure_path(src, '') | |
tag(:img, html_options.merge({:src=>src})) | |
end | |
# Ensures the proper path to the given source. | |
# | |
# If the source begins at the root of the public directory or is a URI | |
# with the http or https scheme name, it is assumed to be absolute and | |
# will not be altered. if compass config would set 'http_path', it will | |
# add path's head. | |
# | |
# Examples | |
# ensure_path('screen.css', 'stylesheets') => '/stylesheets/screen.css' | |
# ensure_path('/screen.css', 'stylesheets') => '/screen.css' | |
# ensure_path('http://cdn/jquery.js', 'javascripts') => 'http://cdn/jquery.js' | |
# | |
def ensure_path(source, path) | |
if source =~ /^(\/|https?)/ | |
return source | |
end | |
# check compass | |
if !defined? Compass | |
return File.join(path, source) | |
end | |
# if relative_build don't defined or value is false | |
# check `http_path` variable and file join to path. | |
if !defined?(Compass.configuration.relative_build) || !Compass.configuration.relative_build | |
return File.join(Compass.configuration.http_path, path, source) if defined? Compass.configuration.http_path else File.join(path, source) | |
end | |
# if `parser.parser_filename` or `parser.layout_filename` don't defined or `parser.parser_filename` is nil | |
# file join to path. | |
if !defined?(parser.parser_filename) || !defined?(parser.layout_filename) || parser.parser_filename === nil | |
return File.join(path, source) | |
end | |
# find relative path. | |
parser_path = Pathname.new File.dirname(parser.parser_filename) | |
source_path = Pathname.new File.dirname(parser.layout_filename) | |
http_path = source_path.relative_path_from(parser_path).to_s | |
http_path = (http_path == '' || http_path == '.' ? '' : http_path) | |
return http_path === '' ? File.join(path, source) : File.join(http_path, path, source) | |
end | |
end | |
# @authors Casper Lai ([email protected]) | |
# @date 2014-01-07 15:02:32 | |
# @version 1.0.0.0 | |
module Helpers | |
# the module get compass config {http_path} varitable. | |
module Path | |
def self.get_http_path | |
if defined? Compass | |
if defined? Compass.configuration.http_path | |
Compass.configuration.http_path | |
else | |
'' | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment