Sha256: 00f392dc4c84296615d4e6c472417027e20cedefddbe5a1de47b4fea53b1eb71

Contents?: true

Size: 749 Bytes

Versions: 2

Compression:

Stored size: 749 Bytes

Contents

module Traco
  class LocalizedReader
    def initialize(record, attribute, options)
      @record = record
      @attribute = attribute
      @fallback = options[:fallback]
    end

    def value
      locales_to_try.each do |locale|
        value = @record.send("#{@attribute}_#{Traco.locale_suffix(locale)}")
        return value if value.present?
      end

      nil
    end

    private

    def locales_to_try
      locale_chain & locales_for_attribute
    end

    def locale_chain
      chain = []
      chain << I18n.locale
      chain << I18n.default_locale if @fallback
      chain.map { |locale| Traco.locale_suffix(locale) }
    end

    def locales_for_attribute
      @record.class.locales_for_attribute(@attribute)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
traco-2.1.0 lib/traco/localized_reader.rb
traco-2.0.0 lib/traco/localized_reader.rb