Sha256: 741b4f08e6577809494d3e3e3dd42e3d88080f4a58fc3154366787328bc81d0f

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

module PageMeta
  class Translator
    include ::ActionView::Helpers::TranslationHelper

    attr_reader :scope, :naming, :options, :html

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

    def singular_scope
      @singular_scope ||= scope == :keywords ? "keywords" : scope.to_s.singularize
    end

    def to_s
      return "" if simple.blank?

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

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

    def simple
      override_options = options.merge(default: "")

      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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
page_meta-1.1.0 lib/page_meta/translator.rb