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
@!attribute page [rw]
Override the current page title @return [String]
Public Class Methods
@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
Read title context
@param [Symbol] key @return [String] the value or `nil`
# File lib/titleist/title.rb, line 27 def [](key) @params[key] end
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
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 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
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
@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
@private @return [Array<String>]
# File lib/titleist/title.rb, line 109 def controller_scope @controller.split('/') end
@private @return [String]
# File lib/titleist/title.rb, line 76 def default_app_title Rails.application.class.name&.deconstantize&.titleize.to_s end
@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
@private @return [String]
# File lib/titleist/title.rb, line 103 def resource_title @controller.singularize.titleize end