module ResourceHelper # Builds a DOM ID for a given record. Works the same as the dom_id helper found in Rails except that it returns a record ID with # a "_0" suffix for new records instead of a "new_" prefix. This makes attaching JavaScript events easier since all DOM IDs are numbers. def build_dom_id record name = record.class.name.underscore record.new_record? ? name + "_0" : name + '_' + record.id.to_s end # Show a descriptive label based on the current controller action. Useful for new/edit actions. Accepts the following parameters: # * *label* - The action label. def show_action_label label [params[:action].capitalize, label].compact.join ' ' end # Shows an unobtrusive jQuery link where the UJS event is attached to the link via the "destroy" class. # If JavaScript is disabled then the _show_ action will be called instead of the _destroy_ action. Accepts # the following hash arguments: # * *parent_id* - The ID of the parent element for which the link is a child of. Example: post_1. *NOTE:* The parent ID takes precidence over the link ID if defined. # * *id* - The link ID. Example: post_1_link. *NOTE:* The link ID _must_ include the parent ID. # * *label* - The link label. Defaults to "Delete". # * *url* - The destroy URL. Example: /posts/1. *NOTE:* The proper HTTP POST request will be handled by JavaScript. def show_destroy_link options = {} options.reverse_merge! :id => options[:parent_id].to_s + "_destroy" options.reverse_merge! :label => "Delete", :class => "destroy", :url => '#' + options[:id].to_s link_to options[:label], options[:url], :id => options[:id], :class => options[:class] end # Shows a nested, unobtrusive jQuery link where the UJS event is attached to the link via the "destroy-nested" class. # If JavaScript is disabled then the _show_ action will be called instead of the _destroy_ action. Accepts # the following hash arguments: # * *object* - The model object to destroy. # * *parent_id* - The ID of the parent element for which the link is a child of. Example: post_1. *NOTE:* The parent ID takes precidence over the link ID if defined. # * *id* - The link ID. Example: post_1_link. *NOTE:* The link ID _must_ include the parent ID. # * *label* - The link label. Defaults to "Delete". # * *url* - The destroy URL. Example: /posts/1. *NOTE:* The proper HTTP POST request will be handled by JavaScript. def show_destroy_nested_link options = {} unless options[:object].new_record? options.reverse_merge! :id => options[:parent_id].to_s + "_destroy-nested", :class => "destroy-nested" show_destroy_link options end end # Shows an unobtrusive jQuery link based on an array of resource hashes. See the show_destroy_link above # for further details. Accepts the following arguments: # * *resources* - The array of resource hashes. def show_destroy_resource_link resources, parent_dom_id show_destroy_link :parent_id => parent_dom_id, :url => build_resource_url(resources, :destroy) end # Shows an unobtrusive jQuery link based on an array of resource hashes. See the show_destroy_link above # for further details. Accepts the following arguments: # * *resources* - The array of resource hashes. def show_destroy_nested_resource_link resources, parent_dom_id show_destroy_link(:id => parent_dom_id.to_s + "_destroy-nested", :class => "destroy-nested") unless resources.last[:record].new_record? end # Shows edit and delete links for resources. Accepts the following parameters: # * *resources* - The array of resource hashes. # * *parent_dom_id* - The parent ID of the dom object for which the edit and destroy links belong to for UJS manipulation. def show_edit_and_destroy_resource_links resources, parent_dom_id [link_to("Edit", build_resource_url(resources, :edit)), show_destroy_resource_link(resources, parent_dom_id)].join(" | ") end end