Sha256: 03521bc6c58d6ed2696c5d33a93156900625f6fed458fa902ebaf5ba0e43b2f0

Contents?: true

Size: 791 Bytes

Versions: 2

Compression:

Stored size: 791 Bytes

Contents

module Cmor
  module Core
    module Model
      module LocalizationConcern
        extend ActiveSupport::Concern

        included do
          validates :locale, inclusion: { in: I18n.available_locales.map(&:to_s) }, allow_nil: true

          scope :for_locale, ->(locale = I18n.locale, options = {}) do
            options.reverse_merge!(include_without_locale: true)

            if options[:include_without_locale]
              where(locale: locale).or(where(locale: nil))
            else
              where(locale: locale)
            end
          end

          # We have to override the setter for the locale to avoid empty strings as locale value.
          def locale=(value)
            super(value.blank? ? nil : value)
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cmor_core-0.0.60.pre lib/cmor/core/model/localization_concern.rb
cmor_core-0.0.59.pre lib/cmor/core/model/localization_concern.rb