module ResourceHelper
  # Displays breadcrumb navigation based on an array of hashes.  Each resource
  # hash is expected to have the following keys:
  # * *id* - The resource ID (a.k.a the model ID).
  # * *label* - The link label for display, otherwise the name is used instead.
  # * *name* - The name of the resource (a.k.a. the controller).  Also used as the link lable unless a label has been given.
  # An optional hash of key/values can be supplied, here is what is allowed:
  # * *separator* - The breadcrumb separator link, defaults to:  '>'
  def resource_breadcrumbs resources = [], options = {}
    # Set defaults.
    breadcrumbs = []
    options.reverse_merge! :separator => "»"
    # Process resources.
    if resources.size > 0
      url = '/' + resources.first[:namespaces].join('/')
      parent_id = nil
      resources.each do |resource|
        url = [url, parent_id, resource[:name].pluralize].compact.join('/')
        parent_id = resource[:parent_id]
        breadcrumbs << link_to(resource[:label], url)
      end
    end
 		content_for :breadcrumbs, content_tag(:div, :id => "breadcrumbs") {breadcrumbs.compact.join(" #{options[:separator]} ")}
  end	

	# 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 take 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 REST destroy URL. Example: /posts/1. *NOTE:* The proper HTTP POST request will be handled by the 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 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