Sha256: d5f30e2f66767fdf6decaa2c11e68436a7b0467396914a88c236016dfe54c886

Contents?: true

Size: 990 Bytes

Versions: 1

Compression:

Stored size: 990 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] + I18n.available_locales + 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

1 entries across 1 versions & 1 rubygems

Version Path
localizable_model-0.6.0 lib/localizable_model/any_localizer.rb