Module: ResourceHelper
- Defined in:
- lib/resource_helper.rb
Instance Method Summary collapse
-
#build_action_label(label) ⇒ Object
Builds a more descriptive label based on the current controller action.
-
#build_dom_id(record) ⇒ Object
Builds a DOM ID for a given record.
-
#build_resource_form_submit(resources = [], html_options = {}) ⇒ Object
Builds the submit path for a new or edit form based on current controller action.
-
#resource_breadcrumbs(resources = [], options = {}) ⇒ Object
Displays breadcrumb navigation based on an array of hashes.
-
#show_destroy_link(options = {}) ⇒ Object
Shows an unobtrusive jQuery link where the UJS event is attached to the link via the “destroy” class.
-
#show_destroy_resource_link(resources, parent_dom_id) ⇒ Object
Shows an unobtrusive jQuery link based on an array of resource hashes.
-
#show_edit_and_destroy_resource_links(resources, parent_dom_id) ⇒ Object
Shows edit and delete links for resources.
-
#show_nested_destroy_resource_link(resources, parent_dom_id) ⇒ Object
Shows an unobtrusive jQuery link based on an array of resource hashes.
Instance Method Details
#build_action_label(label) ⇒ Object
Builds a more descriptive label based on the current controller action. Accepts the following parameters:
-
label - The action label.
52 53 54 |
# File 'lib/resource_helper.rb', line 52 def build_action_label label [params[:action].capitalize, label.singularize].compact.join ' ' end |
#build_dom_id(record) ⇒ Object
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.
28 29 30 31 |
# File 'lib/resource_helper.rb', line 28 def build_dom_id record name = record.class.name.underscore record.new_record? ? name + "_0" : name + '_' + record.id.to_s end |
#build_resource_form_submit(resources = [], html_options = {}) ⇒ Object
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 html options).
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/resource_helper.rb', line 37 def build_resource_form_submit resources = [], = {} case params[:action] # New/create flow. when "new" then return :url => build_resource_url(resources, :create), :html => # Validate/create flow. when "create" then return :url => build_resource_url(resources, :create), :html => # Edit/update flow. when "edit" then return :url => build_resource_url(resources, :update), :html => .merge({:method => :put}) # Validate/update flow. when "update" then return :url => build_resource_url(resources, :update), :html => .merge({:method => :put}) end end |
#resource_breadcrumbs(resources = [], options = {}) ⇒ Object
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: ‘>’
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/resource_helper.rb', line 9 def resources = [], = {} # Set defaults. = [] .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] << link_to(resource[:label], url) end end content_for :breadcrumbs, content_tag(:div, :id => "breadcrumbs") {.compact.join(" #{[:separator]} ")} end |
#show_destroy_link(options = {}) ⇒ Object
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.
63 64 65 66 67 |
# File 'lib/resource_helper.rb', line 63 def show_destroy_link = {} .reverse_merge! :id => [:parent_id].to_s + "_destroy" .reverse_merge! :label => "Delete", :class => "destroy", :url => '#' + [:id].to_s link_to [:label], [:url], :id => [:id], :class => [:class] end |
#show_destroy_resource_link(resources, parent_dom_id) ⇒ Object
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.
72 73 74 |
# File 'lib/resource_helper.rb', line 72 def show_destroy_resource_link resources, parent_dom_id show_destroy_link :parent_id => parent_dom_id, :url => build_resource_url(resources, :destroy) end |
#show_edit_and_destroy_resource_links(resources, parent_dom_id) ⇒ Object
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.
86 87 88 |
# File 'lib/resource_helper.rb', line 86 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 |
#show_nested_destroy_resource_link(resources, parent_dom_id) ⇒ Object
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.
79 80 81 |
# File 'lib/resource_helper.rb', line 79 def show_nested_destroy_resource_link resources, parent_dom_id show_destroy_link(:id => parent_dom_id.to_s + "_nested-destroy", :class => "nested-destroy") unless resources.last[:record].new_record? end |