lib/resource_helper.rb in aeonscope-rest-1.0.0 vs lib/resource_helper.rb in aeonscope-rest-1.1.0

- old
+ new

@@ -1,63 +1,146 @@ 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. + # Renders a label for the current controller resource and action. Useful for labeling index, show, new, and edit views. + # Format: <action> <suffix || resource name>, Example: "New Post" + # *suffix* - Optional. The suffix to be applied to the action name. Defaults to the controller name (i.e. resource). + def render_action_label suffix = nil + [params[:action].capitalize, (suffix || controller_name.singularize.capitalize)] * ' ' + end + + # Answers whether the given action is equal to the current action. + def is_action? action + params[:action] == action.to_s + end + + # Answers if the current action is an index action. + def index_action? + is_action? :index + end + + # Answers if the current action is show action. + def show_action? + is_action? :show + end + + # Answers if the current action is a new action. + def new_action? + is_action? :new + end + + # Answers if the current action is a create action. + def create_action? + is_action? :create + end + + # Answers if the current action is an edit action. + def edit_action? + is_action? :edit + end + + # Answers if the current action is an update action. + def update_action? + is_action? :update + end + + # Answers if the current action is a destroy action. + def destroy_action? + is_action? :destroy + end + + # Toggles hiding a group of records if the given collection is empty. Use as a class attribute. + # * *collection* - The collection of records. + def toggle_group_class collection + "hidden" if collection.empty? + end + + # Builds a DOM ID for a given record. Works the same as the Rails dom_id helper except that it returns a record ID with + # a "_0" suffix for new records instead of a "new_" prefix. This makes record IDs consistent and attaching JavaScript + # events easier. def build_dom_id record name = record.class.name.underscore - record.new_record? ? name + "_0" : name + '_' + record.id.to_s + id = record.new_record? ? 0 : record.id + [name, id].compact * '_' 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 ' ' + # Renders a descriptive label based on the current controller action. Useful for new/edit actions. + # * *suffix* - The action label. + def render_render_action_label suffix + [params[:action].capitalize, suffix].compact * ' ' 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 + # Renders a label or an icon. + # * *label* - Optional (if use_icon=true). The link label. Defaults to "Unknown". + # * *use_icon* - Optional. Boolean for using a text or icon link. Defaults to false. + # * *icon_path* - Optional. The icon path. Defaults to "#{THEME_ROOT}/images/icons/unknown.png". + # * *icon_alt* - Optional. The icon text. Defaults to "Unknown Icon". + # * *icon_size* - Optional. The icon dimensions. Example: "{width}x{height}". Defaults to nil. + def render_label_or_icon options = {} + options.delete_if {|key, value| value.blank?} + options.reverse_merge! :label => "Uknown", :icon_path => "#{THEME_ROOT}/images/icons/unknown.png", :icon_alt => "Unknown Icon" + options[:use_icon] ? image_tag(options[:icon_path], :alt => options[:icon_alt], :size => options[:icon_size]) : options[:label] end + + # Renders an UJS show link. + # * *label* - Optional. The link label. Defaults to "Show". + # * *id* - Optional. The link ID. Defaults to nil. + # * *class* - Optional. The link class. Defaults to "show". + # * *url* - Required. The link URL (example: /posts/1). Defaults to "#show". + # * *use_icon* - Optional. Boolean for using a text or icon link. Defaults to false. + # * *icon_path* - Optional. The icon path. Defaults to "#{THEME_ROOT}/images/icons/rest/show.png". + # * *icon_alt* - Optional. The icon text. Defaults to "Show Icon". + # * *icon_size* - Optional. The icon dimensions. Example: "{width}x{height}". Defaults to nil. + def render_show_link options = {} + options.delete_if {|key, value| value.blank?} + options.reverse_merge! :label => "Show", :icon_path => "#{THEME_ROOT}/images/icons/rest/show.png", :icon_alt => "Show Icon", :class => "show", :url => "#show" + link_to render_label_or_icon(options), 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) + # Renders an UJS new link. + # * *label* - Optional. The link label. Defaults to "New". + # * *id* - Optional. The link ID. Defaults to nil. + # * *class* - Optional. The link class. Defaults to "new". + # * *type* - Optional. Determines the type of object be created. Specifiy "input" as the type for form additions. Defaults to nil. + # * *ogid* = Optional (if type=nil). The outer group ID. Defaults to nil. + # * *igid* = Optional (if type=nil). The inner group ID. Defaults to nil. + # * *position* = Optional (if type=nil). A number position of the ID to be modified. Useful when dealing with complex nested forms. Defaults to 0. + # * *url* - Optional (if type="input"). The link URL (example: /posts/new). Defaults to "#new". + # * *use_icon* - Optional. Boolean for using a text or icon link. Defaults to false. + # * *icon_path* - Optional. The icon path. Defaults to "#{THEME_ROOT}/images/icons/rest/new.png". + # * *icon_alt* - Optional. The icon text. Defaults to "New Icon". + # * *icon_size* - Optional. The icon dimensions. Example: "{width}x{height}". Defaults to nil. + def render_new_link options = {} + options.delete_if {|key, value| value.blank?} + options.reverse_merge! :label => "New", :icon_path => "#{THEME_ROOT}/images/icons/rest/new.png", :icon_alt => "New Icon", :class => "new", :url => "#new", "data-position" => 0 + link_to render_label_or_icon(options), options[:url], :id => options[:id], :class => options[:class], "data-type" => options[:type], "data-ogid" => options[:ogid], "data-igid" => options[:igid], "data-position" => options[:position] 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(" | ") + # Renders an UJS edit link. + # * *label* - Optional. The link label. Defaults to "Edit". + # * *id* - Optional. The link ID. Defaults to nil. + # * *class* - Optional. The link class. Defaults to "edit". + # * *url* - Required. The link URL (example: /posts/1/edit). Defaults to "#edit". + # * *use_icon* - Optional. Boolean for using a text or icon link. Defaults to false. + # * *icon_path* - Optional. The icon path. Defaults to "#{THEME_ROOT}/images/icons/rest/edit.png". + # * *icon_alt* - Optional. The icon text. Defaults to "Edit Icon". + # * *icon_size* - Optional. The icon dimensions. Example: "{width}x{height}". Defaults to nil. + def render_edit_link options = {} + options.delete_if {|key, value| value.blank?} + options.reverse_merge! :label => "Edit", :icon_path => "#{THEME_ROOT}/images/icons/rest/edit.png", :icon_alt => "Edit Icon", :class => "edit", :url => "#edit" + link_to render_label_or_icon(options), options[:url], :id => options[:id], :class => options[:class] + end + + # Renders an UJS destroy link. + # * *label* - Optional. The link label. Defaults to "Delete". + # * *id* - Optional. The link ID. Defaults to nil. + # * *class* - Optional. The link class. Defaults to "destroy". + # * *type* - Optional. Determines the type of object be deleted. Specifiy "input" as the type for form deletions. Defaults to nil. + # * *url* - Required. The link URL (example: /posts/1). Defaults to "#destroy". + # * *use_icon* - Optional. Boolean for using a text or icon link. Defaults to false. + # * *icon_path* - Optional. The icon path. Defaults to "#{THEME_ROOT}/images/icons/rest/destroy.png". + # * *icon_alt* - Optional. The icon text. Defaults to "Delete Icon". + # * *icon_size* - Optional. The icon dimensions. Example: "{width}x{height}". Defaults to nil. + def render_destroy_link options = {} + options.delete_if {|key, value| value.blank?} + options.reverse_merge! :label => "Delete", :icon_path => "#{THEME_ROOT}/images/icons/rest/destroy.png", :icon_alt => "Delete Icon", :class => "destroy", :url => "#destroy" + link_to render_label_or_icon(options), options[:url], :id => options[:id], :class => options[:class], "data-type" => options[:type] end end