class Titleist::Title

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

Constants

FORMAT

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

Attributes

page[W]

@!attribute page [rw]

Override the current page title
@return [String]

Public Class Methods

new(controller:, action:, **context) click to toggle source

@param [String] controller - Current request controller name. @param [String] action - Current request action name. @param [Hash] context - Optional params passed in from the helper. @return [Titleist::Title]

# File lib/titleist/title.rb, line 17
def initialize(controller:, action:, **context)
  @controller = controller
  @action = action
  @params = context
end

Public Instance Methods

[](key) click to toggle source

Read title context

@param [Symbol] key @return [String] the value or `nil`

# File lib/titleist/title.rb, line 27
def [](key)
  @params[key]
end
[]=(key, value) click to toggle source

Write title context

@param [Symbol] key @param [String] value @return [String] the value

# File lib/titleist/title.rb, line 36
def []=(key, value)
  @params[key] = value
end
app() click to toggle source

Global application title.

@return [String]

# 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
page() click to toggle source

Page title from the current scope.

@return [String]

# 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
to_s() click to toggle source

Format the full page title.

@return [String]

# File lib/titleist/title.rb, line 68
def to_s
  I18n.t :format, scope: :titleist, default: FORMAT, app: app, page: page
end

Private Instance Methods

action_title() click to toggle source

@private @return [String]

# File lib/titleist/title.rb, line 90
def action_title
  case @action
  when 'show'
    'View'
  when 'destroy'
    'Delete'
  else
    @action.titleize
  end
end
controller_scope() click to toggle source

@private @return [Array<String>]

# File lib/titleist/title.rb, line 109
def controller_scope
  @controller.split('/')
end
default_app_title() click to toggle source

@private @return [String]

# File lib/titleist/title.rb, line 76
def default_app_title
  Rails.application.class.name&.deconstantize&.titleize.to_s
end
default_page_title() click to toggle source

@private @return [String]

# File lib/titleist/title.rb, line 82
def default_page_title
  return @controller.titleize if @action == 'index'

  [action_title, resource_title].join(' ')
end
resource_title() click to toggle source

@private @return [String]

# File lib/titleist/title.rb, line 103
def resource_title
  @controller.singularize.titleize
end