Sha256: e90a02ee20cbf4cac492293af0fbbb24cfb6a6f73a340dc954df0788e67e677d

Contents?: true

Size: 832 Bytes

Versions: 25

Compression:

Stored size: 832 Bytes

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_method(:"#{attr_name}") do
          I18n.translate(attr_name, scope: slug, default: attr_translation(attr_name))
        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

25 entries across 25 versions & 1 rubygems

Version Path
blacklight-spotlight-3.0.0.alpha.9 app/models/concerns/spotlight/translatables.rb
blacklight-spotlight-3.0.0.alpha.8 app/models/concerns/spotlight/translatables.rb
blacklight-spotlight-3.0.0.alpha.7 app/models/concerns/spotlight/translatables.rb
blacklight-spotlight-3.0.0.alpha.6 app/models/concerns/spotlight/translatables.rb
blacklight-spotlight-3.0.0.alpha.5 app/models/concerns/spotlight/translatables.rb
blacklight-spotlight-3.0.0.alpha.4 app/models/concerns/spotlight/translatables.rb
blacklight-spotlight-3.0.0.alpha.3 app/models/concerns/spotlight/translatables.rb
blacklight-spotlight-3.0.0.alpha.2 app/models/concerns/spotlight/translatables.rb
blacklight-spotlight-3.0.0.alpha.1 app/models/concerns/spotlight/translatables.rb
blacklight-spotlight-2.13.0 app/models/concerns/spotlight/translatables.rb
blacklight-spotlight-2.12.1 app/models/concerns/spotlight/translatables.rb
blacklight-spotlight-2.12.0 app/models/concerns/spotlight/translatables.rb
blacklight-spotlight-2.11.0 app/models/concerns/spotlight/translatables.rb
blacklight-spotlight-2.10.0 app/models/concerns/spotlight/translatables.rb
blacklight-spotlight-2.9.0 app/models/concerns/spotlight/translatables.rb
blacklight-spotlight-2.8.0 app/models/concerns/spotlight/translatables.rb
blacklight-spotlight-2.7.2 app/models/concerns/spotlight/translatables.rb
blacklight-spotlight-2.7.1 app/models/concerns/spotlight/translatables.rb
blacklight-spotlight-2.7.0 app/models/concerns/spotlight/translatables.rb
blacklight-spotlight-2.6.1.1 app/models/concerns/spotlight/translatables.rb