Sha256: 0774886a705fed43155c22a82d62cef678e13596f7d81726b8ccb107a693c2aa

Contents?: true

Size: 1.56 KB

Versions: 3

Compression:

Stored size: 1.56 KB

Contents

# encoding: utf-8

module LocalizableModel
  # = LocalizableModel::ClassMethods
  #
  # Class methods for all Localizable models.
  #
  module ClassMethods
    # Returns a scope where all records will be set to the given locale.
    #
    def in_locale(locale)
      all
        .extending(LocalizableModel::ScopeExtension)
        .localize(locale)
        .includes(:localizations)
    end

    # Returns a scope with only records matching the given locale.
    #
    #  Page.localized('en').first.locale # => 'en'
    #
    def localized(locale)
      in_locale(locale)
        .where("localizations.locale = ?", locale)
        .references(:localizations)
    end

    def localized_attributes
      localizable_configuration.attributes.keys
    end

    # Accessor for the configuration.
    def localizable_configuration
      @localizable_configuration ||= inherited_localizable_configuration
    end

    private

    def define_localizable_methods!
      localizable_configuration.attributes.keys.each do |name|
        define_method name do
          localizer.get(name).to_s
        end
        define_method "#{name}?" do
          localizer.value_for?(name)
        end
        define_method "#{name}=" do |value|
          localizer.set(name, value)
        end
      end
    end

    def inherited_localizable_configuration
      if superclass.respond_to?(:localizable_configuration)
        LocalizableModel::Configuration.new(
          superclass.localizable_configuration.attributes.dup
        )
      else
        LocalizableModel::Configuration.new
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
localizable_model-0.6.4 lib/localizable_model/class_methods.rb
localizable_model-0.6.3 lib/localizable_model/class_methods.rb
localizable_model-0.6.2 lib/localizable_model/class_methods.rb