Sha256: 13fa6e69f99cb0d7927cb5c9ad51eb7167560cf26470460dbeda329944dd136a

Contents?: true

Size: 1.65 KB

Versions: 17

Compression:

Stored size: 1.65 KB

Contents

# frozen_string_literal: true

module Spotlight
  # Mixin for adding translatable ActiveRecord accessors
  module Translatables
    extend ActiveSupport::Concern

    class_methods do
      def translates(*attr_names)
        attr_names.map(&:to_sym)
        attr_names.map(&method(:define_translated_attr_reader))
      end

      ##
      # Set up a reader for the specified attribute that uses the I18n backend,
      # and defaults to the ActiveRecord value
      def define_translated_attr_reader(attr_name)
        # Define a dynamic method for translating database-backed attributes,
        # falling back to the database information as needed.
        #
        # Note: the empty string is provided as the final fallback to avoid i18n blowing
        # up on nil attributes.
        define_method(:"#{attr_name}") do
          send("translated_#{attr_name}", default: [attr_translation(attr_name), ''])
        end

        # Define an accessor that gets the value of the attribute in a given locale,
        # returning `nil` for untranslated values.
        #
        # Note: For the default locale, we actually want to dig into the database,
        # because that is the source of truth for the data.
        define_method(:"translated_#{attr_name}") do |default: [], **options|
          default = Array.wrap(default)
          default.prepend(attr_translation(attr_name)) if I18n.locale == I18n.default_locale
          I18n.t(attr_name, scope: slug, default: default, **options).presence
        end
      end
    end

    private

    ##
    # Will return the default ActiveRecord value for the value
    def attr_translation(attr_name)
      self[attr_name]
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
blacklight-spotlight-4.2.0 app/models/concerns/spotlight/translatables.rb
blacklight-spotlight-4.1.2 app/models/concerns/spotlight/translatables.rb
blacklight-spotlight-4.1.1 app/models/concerns/spotlight/translatables.rb
blacklight-spotlight-4.1.0 app/models/concerns/spotlight/translatables.rb
blacklight-spotlight-4.0.3 app/models/concerns/spotlight/translatables.rb
blacklight-spotlight-4.0.2 app/models/concerns/spotlight/translatables.rb
blacklight-spotlight-4.0.1 app/models/concerns/spotlight/translatables.rb
blacklight-spotlight-4.0.0 app/models/concerns/spotlight/translatables.rb
blacklight-spotlight-3.6.0.beta10 app/models/concerns/spotlight/translatables.rb
blacklight-spotlight-3.6.0.beta9 app/models/concerns/spotlight/translatables.rb
blacklight-spotlight-3.6.0.beta8 app/models/concerns/spotlight/translatables.rb
blacklight-spotlight-3.6.0.beta7 app/models/concerns/spotlight/translatables.rb
blacklight-spotlight-3.6.0.beta6 app/models/concerns/spotlight/translatables.rb
blacklight-spotlight-3.6.0.beta5 app/models/concerns/spotlight/translatables.rb
blacklight-spotlight-3.6.0.beta4 app/models/concerns/spotlight/translatables.rb
blacklight-spotlight-3.6.0.beta3 app/models/concerns/spotlight/translatables.rb
blacklight-spotlight-3.6.0.beta1 app/models/concerns/spotlight/translatables.rb