Class: Titleist::Title

Inherits:
Object
  • Object
show all
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

ROOT_SCOPE =

Top-level scope in the i18n locale for all title configuration.

:titles

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller: '', action: '', context: {}, root: false) ⇒ Title

Returns a new instance of Title.

Parameters:

  • controller (String) (defaults to: '')

    Current request controller name.

  • action (String) (defaults to: '')

    Current request action name.

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

    Optional params passed in from the helper.

  • root (Boolean) (defaults to: false)

    Whether this is the root path



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

def initialize(controller: '', action: '', context: {}, root: false)
  @controller = controller
  @action = action
  @context = context
  @root = root
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



14
15
16
# File 'lib/titleist/title.rb', line 14

def action
  @action
end

#contextObject (readonly)

Returns the value of attribute context.



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

def context
  @context
end

#controllerObject (readonly)

Returns the value of attribute controller.



10
11
12
# File 'lib/titleist/title.rb', line 10

def controller
  @controller
end

#pageString

Page title from given scope.

Returns:

  • (String)


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

def page
  @page ||= I18n.t action.to_sym, context.merge(
    scope: scope,
    default: default_page_title
  )
end

Instance Method Details

#appString

Global application title.

Returns:

  • (String)


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

def app
  I18n.t :application, context.merge(
    scope: ROOT_SCOPE,
    default: default_app_title
  )
end

#root?Boolean

Whether the page we’re generating a title for is the root path. This will cause no page title to display, and is set when the title object is instantiated.

Returns:

  • (Boolean)


81
82
83
# File 'lib/titleist/title.rb', line 81

def root?
  @root
end

#to_hHash

Format this title as a Hash of attributes.

Returns:

  • (Hash)


68
69
70
71
72
73
74
# File 'lib/titleist/title.rb', line 68

def to_h
  {
    scope: ROOT_SCOPE,
    app: app,
    page: page
  }
end

#to_sString

Format this title as a String.

Returns:

  • (String)


59
60
61
62
63
# File 'lib/titleist/title.rb', line 59

def to_s
  return app if root?

  I18n.t :format, to_h
end