Sha256: b4739f1a6e07d5d68a4f65e3f6e0efcf3b6fda18f0739f769e985864df3722c9

Contents?: true

Size: 965 Bytes

Versions: 4

Compression:

Stored size: 965 Bytes

Contents

# encoding: utf-8

module LocalizableModel
  class AnyLocalizer
    attr_reader :record

    def initialize(record)
      @record = record
      define_localization_methods
    end

    private

    def define_localization_methods
      record.class.localized_attributes.each do |attribute|
        self.class.send(:define_method, attribute) do
          localized(attribute)
        end

        self.class.send(:define_method, "#{attribute}?") do
          localized?(attribute)
        end
      end
    end

    def locales
      (
        [record.locale, I18n.locale] + record.locales
      ).compact.map(&:to_sym).uniq
    end

    def localized?(attribute)
      !localized(attribute).blank?
    end

    def localized(attribute)
      localized = locales.inject(nil) do |str, l|
        str ||= -> {
          value = record.localize(l).send(attribute)
          value.blank? ? nil : value
        }.call()
      end
      localized || ""
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
localizable_model-0.6.4 lib/localizable_model/any_localizer.rb
localizable_model-0.6.3 lib/localizable_model/any_localizer.rb
localizable_model-0.6.2 lib/localizable_model/any_localizer.rb
localizable_model-0.6.1 lib/localizable_model/any_localizer.rb