Module: Guts::ApplicationHelper

Defined in:
app/helpers/guts/application_helper.rb

Overview

Common helpers for the whole application

Instance Method Summary collapse

Instance Method Details

#controller_css_idString

Simply creates an ID to use in the ‘body` of the layout which is a combination of the current controller and action

Examples:

Types#edit

Will produce `guts_types_edit`

Returns:

  • (String)

    the ID



30
31
32
# File 'app/helpers/guts/application_helper.rb', line 30

def controller_css_id
  "#{params[:controller].tr('/', '_')}_#{params[:action]}"
end

#current_site_form_field(f) ⇒ String

Helper for settings the current site ID for a model in the form

Parameters:

  • f (Object)

    the current form object

Returns:

  • (String)

    html for form field



52
53
54
# File 'app/helpers/guts/application_helper.rb', line 52

def current_site_form_field(f)
  f.hidden_field :site_id, value: @current_site.try(:id)
end

Creates a wrapper around ‘link_to` to destroy objects through javascript by creating a hidden form

Parameters:

  • title (String)

    the title to use for the link

  • object (Object)

    the object record to reference

  • link_opts (Hash) (defaults to: {})

    an extension of link_to’s options

Returns:

  • (String)

    html to display



10
11
12
13
14
15
16
# File 'app/helpers/guts/application_helper.rb', line 10

def link_to_destroy(title, object, link_opts = {})
  html = []
  html << link_to(title, '#', link_opts.merge(class: "destroy_resource #{link_opts[:class]}"))
  html << form_for(object, url: object, method: :delete, html: { style: 'display: none' }) {}
  
  html.join('').html_safe
end

Determines if a menu is active in the admin panel

Examples:

Strict (default)

For /guts/types... `menu_active? :types`

Non-strict

For /guts/navigations/main-menu/item... `menu_active? :navigation, false`

Parameters:

  • key (Symbol, String)

    the string to compare against

  • strict (Boolean) (defaults to: true)

    weather to do a direct compare or sub string

Returns:

  • (Boolean)


41
42
43
44
45
46
47
# File 'app/helpers/guts/application_helper.rb', line 41

def menu_active?(key, strict = true)
  if strict
    controller.controller_name == key.to_s
  else
    controller.controller_name.include? key.to_s
  end
end

#sub_title_for(object) ⇒ String

Creates a human-readable version of an object

Parameters:

  • object (Object)

    the object record to reference

Returns:

  • (String)

    the result of conversion



21
22
23
# File 'app/helpers/guts/application_helper.rb', line 21

def sub_title_for(object)
  object.class.to_s.demodulize.underscore.titleize
end