Class: PageMeta::Translator

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::TranslationHelper
Defined in:
lib/page_meta/translator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope, naming, options = {}) ⇒ Translator

Returns a new instance of Translator.



9
10
11
12
13
14
# File 'lib/page_meta/translator.rb', line 9

def initialize(scope, naming, options = {})
  @scope = scope
  @naming = naming
  @html = options[:html]
  @options = options
end

Instance Attribute Details

#htmlObject (readonly)

Returns the value of attribute html.



7
8
9
# File 'lib/page_meta/translator.rb', line 7

def html
  @html
end

#namingObject (readonly)

Returns the value of attribute naming.



7
8
9
# File 'lib/page_meta/translator.rb', line 7

def naming
  @naming
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/page_meta/translator.rb', line 7

def options
  @options
end

#scopeObject (readonly)

Returns the value of attribute scope.



7
8
9
# File 'lib/page_meta/translator.rb', line 7

def scope
  @scope
end

Instance Method Details

#override_optionsObject



46
47
48
# File 'lib/page_meta/translator.rb', line 46

def override_options
  options.merge(default: "")
end

#simpleObject



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/page_meta/translator.rb', line 50

def simple
  translation = ""

  translation_scope.each do |scope|
    translation = t("#{scope}_html", **override_options) if html
    translation = t(scope, **override_options) if translation.blank?

    break if translation.present?
  end

  translation
end

#to_sObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/page_meta/translator.rb', line 16

def to_s
  return "" if simple.blank?

  value = simple

  [
    # Set base scope for controller
    t(
      "page_meta.#{naming.controller}.#{scope}_base",
      value:,
      **override_options
    ),

    # Set base scope for action
    t(
      "page_meta.#{naming.controller}.#{naming.action}.#{scope}_base",
      value:,
      **override_options
    ),

    # Set base scope
    t("page_meta.#{scope}_base", value:, **override_options,
                                 default: value)
  ].reject(&:blank?).first || ""
end

#translation_scopeObject



42
43
44
# File 'lib/page_meta/translator.rb', line 42

def translation_scope
  ["page_meta.#{naming.controller}.#{naming.action}.#{scope}"]
end