module PositionableHelper
%w( move_lower move_down move_higher move_up move_to_bottom insert_at_bottom
move_to_top insert_at_top insert_at remove_from_list).each do |action|
path_method = %Q(
def #{action}_path(resource, list = :default, options={})
url_for({:controller => 'positionable', :action => '#{action}',
:resource => resource.class.to_s.underscore, :id => resource.id, :list => list}.merge(options))
end
)
url_method = %Q(
def #{action}_url(resource, list = :default, options={})
#{action}_path(resource, list, options.merge({:only_path => false}))
end
)
class_eval(path_method)
class_eval(url_method)
end
def positionable_links(resource, list = :default)
resource.in_list?(list) ? positionable_move(resource,list,:link) : positionable_insert(resource,list,:link)
end
def positionable_buttons(resource, list = :default)
resource.in_list?(list) ? positionable_move(resource,list,:button) : positionable_insert(resource,list,:button)
end
private
def positionable_move(resource, list, interface)
tag = "
"
if resource.first?(list)
tag << send("positionable_disabled_#{interface}", 'move_to_top')
tag << send("positionable_disabled_#{interface}", 'move_up')
else
tag << send("positionable_working_#{interface}", 'move_to_top', resource, list)
tag << send("positionable_working_#{interface}", 'move_up', resource, list)
end
if resource.last?(list)
tag << send("positionable_disabled_#{interface}", 'move_down')
tag << send("positionable_disabled_#{interface}", 'move_to_bottom')
else
tag << send("positionable_working_#{interface}", 'move_down', resource, list)
tag << send("positionable_working_#{interface}", 'move_to_bottom', resource, list)
end
tag << '
'
end
def positionable_insert(resource, list, interface)
tag = ""
tag << send("positionable_working_#{interface}", 'insert_at_top', resource, list)
tag << send("positionable_working_#{interface}", 'insert_at_bottom', resource, list)
tag << '
'
end
def positionable_disabled_link(message)
"#{positionable_internationalize(message)}"
end
def positionable_working_link(action, resource, list)
'' << link_to(positionable_internationalize(action), send("#{action}_path",resource,list), :method => :put) << ''
end
def positionable_disabled_button(message)
"" << button_to(positionable_internationalize(message), '/', :disabled => true) << ""
end
def positionable_working_button(action, resource, list)
"" << button_to(positionable_internationalize(action), send("#{action}_path",resource,list), :method => :put) << ""
end
def positionable_internationalize(message)
I18n.t("positionable.#{message}", :default => message.titleize)
end
end