Sha256: e662e918ddc1290554ff4563a6f1a006c206be1d2c647fcce42f08fa90debdad

Contents?: true

Size: 801 Bytes

Versions: 18

Compression:

Stored size: 801 Bytes

Contents

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

18 entries across 18 versions & 1 rubygems

Version Path
blacklight-spotlight-2.4.1 app/models/concerns/spotlight/translatables.rb
blacklight-spotlight-2.4.0 app/models/concerns/spotlight/translatables.rb
blacklight-spotlight-2.3.3 app/models/concerns/spotlight/translatables.rb
blacklight-spotlight-2.3.2 app/models/concerns/spotlight/translatables.rb
blacklight-spotlight-2.3.1 app/models/concerns/spotlight/translatables.rb
blacklight-spotlight-2.3.0 app/models/concerns/spotlight/translatables.rb
blacklight-spotlight-2.2.1 app/models/concerns/spotlight/translatables.rb
blacklight-spotlight-2.2.0 app/models/concerns/spotlight/translatables.rb
blacklight-spotlight-2.1.0 app/models/concerns/spotlight/translatables.rb
blacklight-spotlight-2.0.2 app/models/concerns/spotlight/translatables.rb
blacklight-spotlight-2.0.1 app/models/concerns/spotlight/translatables.rb
blacklight-spotlight-2.0.0 app/models/concerns/spotlight/translatables.rb
blacklight-spotlight-2.0.0.rc6 app/models/concerns/spotlight/translatables.rb
blacklight-spotlight-2.0.0.rc5 app/models/concerns/spotlight/translatables.rb
blacklight-spotlight-2.0.0.rc4 app/models/concerns/spotlight/translatables.rb
blacklight-spotlight-2.0.0.rc3 app/models/concerns/spotlight/translatables.rb
blacklight-spotlight-2.0.0.rc2 app/models/concerns/spotlight/translatables.rb
blacklight-spotlight-2.0.0.rc1 app/models/concerns/spotlight/translatables.rb