2009-05-07 16:53:18 +00:00
|
|
|
module ApplicationHelper
|
2011-02-08 02:07:40 +00:00
|
|
|
|
|
|
|
FLASH_NOTICE_KEYS = [:error, :notice, :warning]
|
2011-04-13 10:19:51 +00:00
|
|
|
|
|
|
|
def colorize_in_use_locale(locale)
|
|
|
|
@site_in_use_locales.include?(locale)? 'green' : 'red'
|
|
|
|
end
|
|
|
|
|
2011-02-08 02:07:40 +00:00
|
|
|
def flash_messages
|
|
|
|
return unless messages = flash.keys.select{|k| FLASH_NOTICE_KEYS.include?(k)}
|
|
|
|
formatted_messages = messages.map do |type|
|
|
|
|
content_tag :div, :class => type.to_s do
|
|
|
|
message_for_item(flash[type], flash["#{type}_item".to_sym])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
raw(formatted_messages.join)
|
|
|
|
end
|
|
|
|
|
2011-04-13 10:19:51 +00:00
|
|
|
def link_back
|
2011-05-26 05:48:09 +00:00
|
|
|
link_to t('back'), get_go_back, :class => 'nav'
|
2011-04-19 07:05:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Clean the link back
|
|
|
|
def get_go_back
|
|
|
|
begin
|
2011-05-02 05:28:38 +00:00
|
|
|
if request.url.include?('locale=')
|
2011-04-19 07:05:48 +00:00
|
|
|
session[:last_page]
|
|
|
|
else
|
2012-02-28 20:21:56 +00:00
|
|
|
session[:last_page] = remove_locale(request.referer)
|
2011-04-19 07:05:48 +00:00
|
|
|
end
|
|
|
|
rescue
|
|
|
|
eval(params[:controller].split('/').join('_') << '_url')
|
|
|
|
end
|
2011-04-13 10:19:51 +00:00
|
|
|
end
|
|
|
|
|
2012-02-28 20:21:56 +00:00
|
|
|
# Remove the locale but keep all the other params
|
|
|
|
def remove_locale(url)
|
|
|
|
target = url.split('?')
|
|
|
|
vars = target[1].split('&') rescue []
|
|
|
|
vars.delete_if {|var| var.include? 'locale=' }
|
|
|
|
if vars.size > 0
|
|
|
|
target[0].to_s + '?' + vars.join('&')
|
|
|
|
else
|
|
|
|
target[0].to_s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_locale(url, locale)
|
|
|
|
if url.include?('?')
|
|
|
|
url + "&locale=#{locale}"
|
|
|
|
else
|
|
|
|
url + "?locale=#{locale}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-02-08 02:07:40 +00:00
|
|
|
def message_for_item(message, item = nil)
|
|
|
|
if item.is_a?(Array)
|
|
|
|
message % link_to(*item)
|
|
|
|
else
|
|
|
|
message % item
|
|
|
|
end
|
|
|
|
end
|
2011-12-23 10:34:21 +00:00
|
|
|
|
|
|
|
def add_attribute(partial, f, attribute)
|
|
|
|
new_object = f.object.send(attribute).build
|
|
|
|
fields = f.fields_for(attribute, new_object, :child_index => "new_#{attribute}") do |f|
|
|
|
|
render :partial => partial, :object => new_object, :locals => {:f => f}
|
|
|
|
end
|
|
|
|
end
|
2012-02-22 05:44:01 +00:00
|
|
|
|
|
|
|
def active_for_controllers(*controller_names)
|
|
|
|
controller_names.include?(controller.controller_name) ? 'active' : nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def visible_for_controllers(*controller_names)
|
|
|
|
controller_names.include?(controller.controller_name) ? '' : 'hide'
|
|
|
|
end
|
|
|
|
|
|
|
|
def active_for_action(controller_name, action_name)
|
|
|
|
(controller.controller_name.eql?(controller_name) && controller.action_name.eql?(action_name)) ? 'active' : nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def sortable(column)
|
|
|
|
direction = (column == params[:sort] && params[:direction] == "asc") ? "desc" : "asc"
|
|
|
|
{:sort => column, :direction => direction}
|
|
|
|
end
|
|
|
|
|
2012-02-23 07:48:23 +00:00
|
|
|
def is_sort_active?(name)
|
|
|
|
res = ''
|
|
|
|
res << ' select' if params[:sort].eql?(name)
|
|
|
|
res << ' active' if params[:sort].eql?(name) && params[:direction].eql?('asc')
|
|
|
|
res
|
|
|
|
end
|
|
|
|
|
|
|
|
def is_sort?(name)
|
|
|
|
' web-symbol' if params[:sort].eql?(name)
|
|
|
|
end
|
|
|
|
|
2012-02-22 05:44:01 +00:00
|
|
|
def add_filter(param_name, value)
|
|
|
|
filter = params[:filter] rescue nil
|
|
|
|
if filter && filter.has_key(param_name)
|
|
|
|
filter[param_name] << value
|
|
|
|
elsif filter
|
|
|
|
filter.merge({param_name => [value]})
|
|
|
|
else
|
|
|
|
{param_name => [value]}
|
|
|
|
end
|
|
|
|
end
|
2011-02-08 02:07:40 +00:00
|
|
|
|
2009-05-07 16:53:18 +00:00
|
|
|
end
|