Module: TitleHelper

Included in:
ScavengerHunt::ApplicationHelper
Defined in:
app/helpers/title_helper.rb

Instance Method Summary collapse

Instance Method Details

#title_for(options = {}) ⇒ Object

Returns a title tag if a 'title' option is present. Also modifies options to point to the title tag as `aria-labelledby`.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/helpers/title_helper.rb', line 4

def title_for(options = {})
  title = options.delete(:title)
  return "".html_safe unless title.present?

  case title
  when Hash
    title_options = title
    title = title[:text]
  when String
    title_options = {}
  end

  tag = title_options.fetch(:tag, :h2)
  sr_only = title_options.key?(:sr_only) ? title_options.delete(:sr_only) : true

  id = id_for(title)

  # Update the options hash so that subsequent uses include the label
  combine_options(options, aria: { labelledby: id })

  # Configure the title tag
  title_options = combine_options(title_options, { class: sr_only ? 'sr-only' : nil, id: id })
  (tag, title_options) { title }
end