module ActivoateHelper
# Get or set the page title
#
# title - The title to set. (optional)
#
# Example:
# page_title("Hello, world!")
# # => "Hello, world!"
# page_title
# # => "Hello, world!"
#
# Returns the page title, first setting it if title is not nil.
def page_title(title = nil)
@title = title unless title.nil?
@title
end
# Displays a badge
#
# content - The bagde`s content
# color - null for white, or grey, red, yellow, green
#
# Example:
# <%= badge 'New!', 'yellow' %>
#
# Returns a div tag
def badge(content, color = '')
color = "-#{color}" unless color.length == 0
content_tag('div', { :class => "badge#{color}" }) do
content
end
end
# Wraps the button helper, creating an add button
#
# path - The button`s action
#
# Example:
# <%= add_button new_user_path %>
#
# Returns an anchor tag
def add_button(path)
button(path, t("activoate.add", :default => 'Add'), 'add')
end
# Wraps the button helper, creating a cancel button
#
# path - The button`s action
#
# Example:
# <%= cancel_button users_path %>
#
# Returns an anchor tag
def cancel_button(path)
button(path, t("activoate.cancel", :default => 'Cancel'), 'cross')
end
# Wraps the button helper, creating an edit button
#
# path - The button`s action
# alternative_icon - True for the alternative icon
#
# Example:
# <%= edit_button edit_user_path(@user) %>
# <%= edit_button edit_user_path(@user), true %>
#
# Returns an anchor tag
def edit_button(path, alternative_icon = false)
button(path, t("activoate.edit", :default => 'Edit'), alternative_icon ? 'edit' : 'application_edit')
end
# Wraps the button helper, creating a delete button
#
# path - The button`s action
# alternative_icon - True for the alternative icon
#
# Example:
# <%= delete_button users_path(@user) %>
# <%= delete_button users_path(@user), true %>
#
# Returns an anchor tag
def delete_button(path, alternative_icon = false)
options = {
:confirm => t("activoate.confirm", :default => "Are you sure?"),
:method => :delete
}
button(path, t("activoate.delete", :default => 'Delete'), alternative_icon ? 'delete' : 'cross', options)
end
# Displays a button
#
# path - The button`s action
# caption - The button`s caption
# icon - The icon file name, without the extension
# options - A hash of attributes to apply to the wrapping anchor tag
#
# Example:
# <%= button root_path, { :caption => 'home', :icon => 'home' } %>
#
# Returns an anchor tag
def button(path, caption = 'click-me', icon = 'show', options = {})
default_options = { :class => "button", :confirm => false }
options = default_options.merge(options)
link_to path, options do
image_tag("activo/icons/#{icon}.png", :alt => caption) + " " + caption
end
end
# Displays a submit button
#
# caption - The button`s caption
# icon - The icon file name, without the extension
# options - A hash of attributes to apply to the wrapping button tag
#
# Example:
# <%= submit_button root_path, { :caption => 'home', :icon => 'home' } %>
#
# Returns a button tag
def submit_button(caption = 'submit', icon = 'show', options = {})
default_options = { :class => "button", :type => 'submit' }
options = default_options.merge(options)
content_tag("button", options) do
image_tag("activo/icons/#{icon}.png", :alt => caption) + " " + caption
end
end
# Creates a set of buttons
#
# options - A hash of attributes to apply to the wrapping div tag
#
# Example:
#
#
# <%= controls do |c|
# c.item add_button(new_user_path)
# c.item button(root_path, { :caption => 'home', :icon => 'home' })
# c.item delete_button(users_path(@user), true)
# end %>
#
#
#
# Returns a set of controls to be displayed.
def controls(options = {})
options[:class] ||= ""
options[:class] << " control"
options[:class] = options[:class].strip.html_safe
items = ControlsBuilder.new
yield items if block_given?
content_tag("div", options) do
items.to_a.join("").html_safe
end
end
# Displays an icon
#
# name - The icon file name, without the extension
# options - A hash of attributes to apply to the image tag
#
# Example:
# <%= icon 'add', { :alt => "Add item" } %>
#
# Returns an img tag.
def icon(name, options = {})
return "" if name.nil?
options[:alt] ||= name.capitalize.gsub("_", " ")
path = "activo/icons/#{name}.png"
image_tag path, { :alt => options[:alt] }
end
# Displays a navigation menu
#
# options - A hash of attributes to apply to the wrapping div tag
#
# Example:
#
# <%= navigation do |nav|
# nav.item "List People", people_path, :active => true
# nav.item "New Person", new_person_path
# nav.item "Search", search_path(:type => "people")
# end %>
#
#
List People
#
#
#
# Returns a navigation block to be displayed.
def navigation(options = {}, &block)
options[:class] ||= ""
options[:class] = options[:class].strip.html_safe
menu = NavigationBuilder.new
yield menu if block_given?
content_tag("div", options) do
content_tag("ul", "", :class => "wat-cf") do
menu.collect { |item|
content_tag("li", :class => item[:class]) do
link_to(item[:label], item[:href], item[:link_options])
end
}.join("").html_safe
end
end
end
# Displays a secondary navigation menu
def secondary_navigation(options = {}, &block)
options[:class] ||= ""
options[:class] << " secondary-navigation"
navigation(options, &block)
end
# Displays a secondary inner navigation menu
def secondary_inner_navigation(options = {}, &block)
options[:class] ||= ""
options[:class] << " secondary-inner-nav"
navigation(options, &block)
end
# Displays a breadcrumb trail
#
# options - A hash of attributes to apply to the wrapping div tag
#
# Example:
#
#
#
<%= @news_item.title %>
#
<%= @news_item.content %>
#
# <%= breadcrumbs do |b|
# b.item "Home", root_path
# b.item "News", news_path
# b.item "Awesome New Things", news_path(@news_item), :active => true
# %>
#
#
# Returns the breadcrumb trail.
def breadcrumbs(options = {})
items = NavigationBuilder.new
yield items if block_given?
options[:class] ||= ""
options[:class] << " breadcrumb"
options[:class] = options[:class].strip.html_safe
content_tag("div", options) do
content_tag("ul") do
items.collect { |item|
content_tag("li", :class => item[:class]) do
if item[:active]
item[:label]
else
link_to(item[:label], item[:href])
end
end
}.join("").html_safe
end
end
end
# Assists in the creation of navigation menus
class NavigationBuilder
attr_reader :item_list
include Enumerable
def initialize
@item_list = []
end
def each(&blk)
item_list.each(&blk)
end
def item(label, path, options = {})
options[:class] ||= ""
options[:class] << " first" if item_list.empty?
options[:class] << " active" if options[:active]
options[:link_options] ||= {}
options[:link_options].merge!(:method => options[:method]) if options[:method]
item_list << {
:label => label,
:href => path,
:class => options[:class].strip,
:link_options => options[:link_options],
:icon => options[:icon],
:active => !!options[:active]
}
end
end
# Assists in the creation of control bars
class ControlsBuilder
attr_reader :item_list
include Enumerable
def initialize
@item_list = []
end
def each(&blk)
item_list.each(&blk)
end
def item(button)
item_list << button
end
end
end