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 the submit path for a new or edit form based on current controller action. This assumes
  # that the new/create and edit/update forms are always submitted to the same URL.  Accepts the following parameters:
  # * *resources* - The array of resource hashes.
  # * *html_options* - The html options for a form (same as form_for[http://apidock.com/rails/ActionView/Helpers/FormHelper/form_for] html options).
  def build_resource_form_submit resources = [], html_options = {}
    case params[:action]
    # New/create flow.
    when "new" then return :url => build_resource_url(resources, :create), :html => html_options
    # Validate/create flow.
    when "create" then return :url => build_resource_url(resources, :create), :html => html_options
    # Edit/update flow.
    when "edit" then return :url => build_resource_url(resources, :update), :html => html_options.merge({:method => :put})
    # Validate/update flow.
    when "update" then return :url => build_resource_url(resources, :update), :html => html_options.merge({:method => :put})
    end
  end
  
  # Builds a more descriptive label based on the current controller action.  Accepts the following parameters:
  # * *label* - The action label.
  def build_action_label label
    [params[:action].capitalize, label.singularize].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! :label => "Delete", :class => "destroy"
		options[:id] = options[:parent_id] + "_link" if options[:parent_id]
    content_tag :a, options[:label], :href => 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
		show_destroy_link :parent_id => resources.last[:parent_id], :url => build_resource_url(resources, :destroy)
	end

  # Shows edit and delete links for resources. *NOTE*: Normally the record being edited is not known when
  # resources are first assembled. Make sure to add the record to the last resource prior to calling this
  # method for correct results.  Accepts the following parameters:
  # * *resources* - The array of resource hashes.
  def show_edit_and_destroy_resource_links resources
    html = link_to "Edit", build_resource_url(resources, :edit)
    html += " | "
    html += show_destroy_resource_link resources
  end
end