Class: Titleist::Title

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/titleist/title.rb

Overview

Object that turns passed-in scope details into a String of title text derived from the I18n locale configuration.

Constant Summary collapse

FORMAT =

Default title format that can be overridden by changing the i18n key ‘titelist.format`.

'%{page} - %{app}'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller:, action:, **context) ⇒ Titleist::Title

Parameters:

  • controller (String)
    • Current request controller name.

  • action (String)
    • Current request action name.

  • context (Hash)
    • Optional params passed in from the helper.



17
18
19
20
21
# File 'lib/titleist/title.rb', line 17

def initialize(controller:, action:, **context)
  @controller = controller
  @action = action
  @params = context
end

Instance Attribute Details

#pageString

Override the current page title

Returns:

  • (String)


53
54
55
56
57
58
# File 'lib/titleist/title.rb', line 53

def page
  @page ||= I18n.t :title, @params.reverse_merge(
    scope: [*controller_scope, @action],
    default: default_page_title
  )
end

Instance Method Details

#[](key) ⇒ String

Read title context

Parameters:

  • key (Symbol)

Returns:

  • (String)

    the value or ‘nil`



27
28
29
# File 'lib/titleist/title.rb', line 27

def [](key)
  @params[key]
end

#[]=(key, value) ⇒ String

Write title context

Parameters:

  • key (Symbol)
  • value (String)

Returns:

  • (String)

    the value



36
37
38
# File 'lib/titleist/title.rb', line 36

def []=(key, value)
  @params[key] = value
end

#appString

Global application title.

Returns:

  • (String)


43
44
45
46
47
48
# File 'lib/titleist/title.rb', line 43

def app
  @app ||= I18n.t :title, @params.reverse_merge(
    scope: %i[layouts application],
    default: default_app_title
  )
end

#to_sString

Format the full page title.

Returns:

  • (String)


68
69
70
# File 'lib/titleist/title.rb', line 68

def to_s
  I18n.t :format, scope: :titleist, default: FORMAT, app: app, page: page
end