}.html_safe
end
def controller?( expression )
!! ( expression.class == Regexp ? controller.controller_name =~ expression : controller.controller_name == expression )
end
# Display CRUD icons or links, according to setting in use_crud_icons method.
#
# In application_helper.rb:
#
# def use_crud_icons
# true
# end
#
# Then use in index views like this:
#
#
#
def crud_links_for_nested_resource(model, nested_model, model_instance_name, nested_model_instance_name, actions, args={})
_html = ""
if use_crud_icons
if actions.include?(:show)
_html << eval("link_to image_tag('/images/icons/view.png', :class => 'crud_icon'), #{model_instance_name}_#{nested_model_instance_name}_path(model, nested_model), :title => 'View'")
end
if actions.include?(:edit)
_html << eval("link_to image_tag('/images/icons/edit.png', :class => 'crud_icon'), edit_#{model_instance_name}_#{nested_model_instance_name}_path(model, nested_model), :title => 'Edit'")
end
if actions.include?(:delete)
_html << eval("link_to image_tag('/images/icons/delete.png', :class => 'crud_icon'), #{model_instance_name}_#{nested_model_instance_name}_path(model, nested_model), :method => :delete, :confirm => 'Are you sure? This action cannot be undone.', :title => 'Delete'")
end
end
_html
end
# DRY way to return a legend tag that renders correctly in all browsers. This variation allows
# for more "stuff" inside the legend tag, e.g. expand/collapse controls, without having to worry
# about escape sequences.
#
# Sample usage:
#
# <%- legend_block do -%>
# Backlinks (<%=
# @google_results.size -%>)
# <%- end -%>
#
def legend_block(&block)
concat content_tag(:div, capture(&block), :class => "faux_legend")
end
# DRY way to return a legend tag that renders correctly in all browsers
def legend_tag(text, args={})
_html = %{
#{text}
\r}
_html.gsub!(/ id=""/,'')
_html.gsub!(/ class=""/,'')
_html
end
def meta_description(content=nil)
content_for(:meta_description) { content } unless content.blank?
end
def meta_keywords(content=nil)
content_for(:meta_keywords) { content } unless content.blank?
end
def models_for_select( models, label = 'name' )
models.map{ |m| [m[label], m.id] }.sort_by{ |e| e[0] }
end
def options_for_array( a, selected = nil, prompt = select_prompt )
"" + a.map{ |_e| _flag = _e[0].to_s == selected ? 'selected="1"' : ''; _e.is_a?(Array) ? "" : "" }.to_s
end
# Create a link that is opaque to search engine spiders.
def obfuscated_link_to(path, image, label, args={})
_html = %{}
_html
end
# Wraps the given HTML in Rails' default style to highlight validation errors, if any.
def required_field_helper( model, element, html )
if model && ! model.errors.empty? && element.is_required
return content_tag( :div, html, :class => 'fieldWithErrors' )
else
return html
end
end
def select_prompt
"Select..."
end
def select_prompt_option
""
end
# Use on index pages to create dropdown list of filtering criteria.
# Populate the filter list using a constant in the model corresponding to named scopes.
#
# Usage:
#
# - item.rb:
#
# scope :active, :conditions => { :is_active => true }
# scope :inactive, :conditions => { :is_active => false }
#
# FILTERS = [
# {:scope => "all", :label => "All"},
# {:scope => "active", :label => "Active Only"},
# {:scope => "inactive", :label => "Inactive Only"}
# ]
#
# - items/index.html.erb:
#
# <%= select_tag_for_filter("items", @filters, params) -%>
#
# - items_controller.rb:
#
# def index
# @filters = Item::FILTERS
# if params[:show] && params[:show] != "all" && @filters.collect{|f| f[:scope]}.include?(params[:show])
# @items = eval("@items.#{params[:show]}.order_by(params[:by], params[:dir])")
# else
# @items = @items.order_by(params[:by], params[:dir])
# end
# ...
# end
#
def select_tag_for_filter(model, nvpairs, params)
return unless model && nvpairs && ! nvpairs.empty?
options = { :query => params[:query] }
_url = url_for(eval("#{model}_url(options)"))
_html = %{ }
_html << %{}
end
# Returns a link_to tag with sorting parameters that can be used with ActiveRecord.order_by.
#
# To use standard resources, specify the resources as a plural symbol:
# sort_link(:users, 'email', params)
#
# To use resources aliased with :as (in routes.rb), specify the aliased route as a string.
# sort_link('users_admin', 'email', params)
#
# You can override the link's label by adding a labels hash to your params in the controller:
# params[:labels] = {'user_id' => 'User'}
def sort_link(model, field, params, html_options={})
if (field.to_sym == params[:by] || field == params[:by]) && params[:dir] == "ASC"
classname = "arrow-asc"
dir = "DESC"
elsif (field.to_sym == params[:by] || field == params[:by])
classname = "arrow-desc"
dir = "ASC"
else
dir = "ASC"
end
options = {
:anchor => html_options[:anchor] || nil,
:by => field,
:dir => dir,
:query => params[:query],
:show => params[:show]
}
options[:show] = params[:show] unless params[:show].blank? || params[:show] == 'all'
html_options = {
:class => "#{classname} #{html_options[:class]}",
:style => "color: white; font-weight: #{params[:by] == field ? "bold" : "normal"}; #{html_options[:style]}",
:title => "Sort by this field"
}
field_name = params[:labels] && params[:labels][field] ? params[:labels][field] : field.titleize
_link = model.is_a?(Symbol) ? eval("#{model}_url(options)") : "/#{model}?#{options.to_params}"
link_to(field_name, _link, html_options)
end
# Tabbed interface helpers =======================================================================
# Returns formatted tabs with appropriate JS for activation. Use in conjunction with tab_body.
#
# Usage:
#
# <%- tabset do -%>
# <%= tab_tag :id => 'ppc_ads', :label => 'PPC Ads', :state => 'active' %>
# <%= tab_tag :id => 'budget' %>
# <%= tab_tag :id => 'geotargeting' %>
# <%- end -%>
#
def tabset(&proc)
concat %{
}
yield
concat %{
}
end
# Returns a tab body corresponding to tabs in a tabset. Make sure that the id of the tab_body
# matches the id provided to the tab_tag in the tabset block.
#
# Usage:
#
# <%- tab_body :id => 'ppc_ads', :label => 'PPC Ad Details' do -%>
# PPC ads form here.
# <%- end -%>
#
# <%- tab_body :id => 'budget' do -%>
# Budget form here.
# <%- end -%>
#
# <%- tab_body :id => 'geotargeting' do -%>
# Geotargeting form here.
# <%- end -%>
#
def tab_body(args, &proc)
concat %{
}
end
# Returns the necessary HTML for a particular tab. Use inside a tabset block.
# Override the default tab label by specifying a :label parameter.
# Indicate that the tab should be active by setting its :state to 'active'.
# (NOTE: You must define a corresponding CSS style for active tabs.)
#
# Usage:
#
# <%= tab_tag :id => 'ppc_ads', :label => 'PPC Ads', :state => 'active' %>
#
def tab_tag(args, *css_class)
%{
#{args[:label] || args[:id].to_s.titleize}
}
end
# ================================================================================================
def tag_for_collapsible_row(obj, params)
_html = ""
if obj && obj.respond_to?(:parent) && obj.parent
_html << %{
}
else
_html << %{
}
end
_html
end
def tag_for_collapsible_row_control(obj)
_base_id = "#{obj.class.name.downcase}_#{obj.id}"
_html = %{}
end
# Create a set of tags for displaying a field label with inline help.
# Field label text is appended with a ? icon, which responds to a click
# by showing or hiding the provided help text.
#
# Sample usage:
#
# <%= tag_for_label_with_inline_help 'Relative Frequency', 'rel_frequency', 'Relative frequency of search traffic for this keyword across multiple search engines, as measured by WordTracker.' %>
#
# Yields:
#
#
#
#
Relative frequency of search traffic for this keyword across multiple search engines, as measured by WordTracker.
}
_html
end
# Create a set of tags for displaying a field label followed by instructions.
# The instructions are displayed on a new line following the field label.
#
# Usage:
#
# <%= tag_for_label_with_instructions 'Status', 'is_active', 'Only active widgets will be visible to the public.' %>
#
# Yields:
#
#