module ResourceHelper # Renders a label for the current controller resource and action. Useful for labeling index, show, new, and edit views. # Format: , 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 id = record.new_record? ? 0 : record.id [name, id].compact * '_' end # 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 # 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 # 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 # 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