Created
August 9, 2012 13:46
-
-
Save yvesvanbroekhoven/3304298 to your computer and use it in GitHub Desktop.
Frontend Lalala helpers
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
module ApplicationHelper | |
# | |
# Returns css class for <body> | |
# | |
def body_class | |
body_class = [] | |
body_class << (params[:controller].gsub('_','-') rescue nil) | |
body_class << (params[:action].gsub('_','-') rescue nil) | |
body_class << @body_class if @body_class | |
body_class.join(' ') | |
end | |
# | |
# Returns class_name if x equals y | |
# | |
def css_class(class_name, x, y, exact = false) | |
if exact | |
class_name if (x === y) | |
else | |
class_name if (x == y) | |
end | |
end | |
# | |
# Returns page title | |
# Use @page_title in your controller to add that in front | |
def page_title(suffix = '') | |
title = [] | |
title << @page_title unless @page_title.nil? | |
page = @page rescue nil | |
until page.nil? | |
title << page.title unless %w{/home}.include?(page.tree_path) | |
page = page.parent | |
end | |
title << suffix | |
title.join(' | ') | |
end | |
# | |
# Returns meta description depending on current page, with default fallback | |
# Use @meta_description in your controller to overrule | |
def meta_description(default = '') | |
if not @meta_description.nil? | |
@meta_description = strip_tags((first_paragraph(@meta_description)[0] rescue default)) | |
elsif @meta_description.nil? and not @page.nil? and not @page.body.nil? and not @page.body.blank? | |
@meta_description = strip_tags((first_paragraph(@page.intro)[0] rescue default)) | |
else | |
@meta_description = default | |
end | |
@meta_description | |
end | |
# | |
# Returns the first <p> in a string | |
# | |
def first_paragraph(str) | |
str.match(/(<p(.*)>.*<\/p>)/) | |
end | |
# | |
# Returns an url to the Open Graph image | |
# Use @og_image in your controller to overrule | |
def og_image | |
if not @og_image.nil? | |
@og_image | |
else | |
"#{request.protocol}#{request.host_with_port}#{image_path('og-image.png')}" | |
end | |
end | |
# | |
# Returns string of space seperated active page id's (current page, current parent pages, custom pages) | |
# Use @data_active_nav_keys in your controller to add custom page id's | |
def data_active_nav_keys | |
keys = [] | |
keys << @data_active_nav_keys unless @data_active_nav_keys.nil? | |
page = @page || nil | |
while page | |
keys << page.id | |
page = page.parent | |
end | |
keys.map{ |k| "page-#{k}" }.join(' ') | |
end | |
# | |
# Returns file type based on .extension | |
# | |
def file_type(file_url) | |
File.extname(File.basename(file_url)).gsub(/\./, '').gsub(/\?[0-9]*/, '') | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment