HTML
# Indent and return the data.
html.gsub!( /^/, indent ) unless ( indent.nil? || indent.empty? ) # Not 'indent.blank?', as this would ignore all-white-space strings
return html
end
# Return a form containing a button with JS deletion confirmation, which
# if activated will delete the given object. Works on the same principles
# as apphelp_protected_link_to but everything runs from the controller
# handling the current request. so its name is used to get a Model and a
# "can_destroy?" method is invoked on it or, if that is missing, a
# "can_modify?" method is invoked. Special case: A current user MUST be
# logged in.
#
# The function returns an empty string if there is no current user or if
# one of the permissions methods returns 'false'. The function returns an
# empty string in production mode or warning string in development mode if
# both methods are missing.
#
# The input parameter gets passed through to button_to's second parameter,
# so you can use any url_for() parameters here, including specifying a URL
# directly if you have unusual requirements, or just passing in an
# ActiveRecord model object instance reference for simple cases.
#
# For a long button name, pass "true" in the optional second parameter -
# else a short version is used based only on the action name.
#
def apphelp_protected_delete_button( obj, short = true )
model = controller.controller_name.classify.constantize
permission = get_permission( model, :destroy, obj )
if ( permission == true )
button_text = apphelp_action_name( :destroy )
button_text = apphelp_heading( controller, :delete, button_text ) unless ( short )
return button_to(
button_text,
obj,
{
:confirm => apphelp_confirm(),
:method => :delete
}
)
else
return permission
end
end
# Return a small table containing action links / buttons for a row in an
# index table. Pass a string (not symbol) to use to construct helper method
# names for URLs for the links (e.g. "language" to use "language_path" for a
# 'show' action link and "edit_language_path" for an 'edit' action link) and
# the Active Record object for which the links / buttons are being generated.
#
# If an optional third parameter is provided, it is assumed to be a user ID -
# usually an owner of the given object - and this ID is used for RESTful
# user-based editing instead of context free editing. That is, rather than
# generating a link based on "edit_foo_path", "edit_user_foo_path" will be
# called instead.
#
# As a special case you can actually pass a model instance instead of a user
# ID in the user ID field. This will be turned into a name and used for the
# resourceful URL; e.g. pass a Currency instance for "currency_..." paths.
#
def apphelp_index_actions( name, obj, user_id_or_obj = nil )
if ( user_id_or_obj.nil? )
show_method = "#{ name }_path"
edit_method = "edit_#{ name }_path"
arg = obj
elsif ( user_id_or_obj.is_a?( String ) || user_id_or_obj.is_a?( Fixnum ) )
show_method = "user_#{ name }_path"
edit_method = "edit_user_#{ name }_path"
arg = { :id => obj.id, :user_id => user_id_or_obj }
else
resname = user_id_or_obj.class.table_name.singularize
show_method = "#{ resname }_#{ name }_path"
edit_method = "edit_#{ resname }_#{ name }_path"
arg = { :id => obj.id }
arg[ "#{ resname }_id".to_sym ] = user_id_or_obj.id
end
show = apphelp_protected_link_to( :show, { :short => true, :method => show_method }, arg )
edit = apphelp_protected_link_to( :edit, { :short => true, :method => edit_method }, arg )
if ( respond_to? "delete_#{ name }_path" )
dsty = apphelp_protected_link_to( :delete, { :short => true, :method => "delete_#{ name }_path" }, obj )
else
dsty = apphelp_protected_delete_button( obj )
end
result = "
"
result << '' << show << ' | ' unless ( show.empty? )
result << '' << edit << ' | ' unless ( edit.empty? )
result << '' << dsty << ' | ' unless ( dsty.empty? )
return result << "
"
end
# As will_paginate, but uses internationalised next/previous link text and
# sets a few common preferred preferences in passing.
#
def apphelp_i18n_will_paginate( what )
will_paginate(
what,
:previous_label => t( :'uk.org.pond.canvass.pagination.previous' ),
:next_label => t( :'uk.org.pond.canvass.pagination.next' )
)
end
# Return the result of 'select_tag' / 'form.select' for a list of values
# given in the first parameter, to set a form field (and thus params hash
# entry) with an ID determined by the second parameter, according to options
# given the optional third parameter.
#
# The values in the first parameter are specified as an array. Each array
# entry is itself another array with a pair of entries - the first gives the
# menu text to show in the selection menu and the second gives the associated
# value to store if the menu entry is selected (often a database object ID),
# e.g.:
#
# [
# [ "Cambridge", "16" ], # Menu text "Cambridge", form field value "16"
# [ "London", "15" ],
# [ "UK", "4" ],
# ]
#
# Values are made HTML-safe by Rails in passing, so don't do this as the
# caller, else you will see double escaping problems.
#
# The other parameters vary according to the two main ways this method should
# be used:
#
# (1) To fill in a field of an object which is being created or edited,
# in which case view code along the lines of
# "form_for