Sha256: 20058992d9edebb5e8f82045e09573c5da7c9a45d58cc97df3bc765b66fb861c

Contents?: true

Size: 1.15 KB

Versions: 2

Compression:

Stored size: 1.15 KB

Contents

# frozen_string_literal: true

# rubocop:disable Style/ClassAndModuleChildren
module Maglev::Site::LocalesConcern
  extend ActiveSupport::Concern

  included do
    ## serializers ##
    if Rails::VERSION::MAJOR > 6
      serialize :locales, coder: LocalesSerializer
    else
      serialize :locales, LocalesSerializer
    end

    ## validation ##
    validates :locales, 'maglev/collection': true, length: { minimum: 1 }
  end

  def add_locale(locale)
    return nil if locale_prefixes.include?(locale.prefix.to_sym)

    locales << locale
    locales
  end

  def default_locale
    locales.first
  end

  def default_locale_prefix
    default_locale.prefix.to_sym
  end

  def locale_prefixes
    locales.map { |locale| locale.prefix.to_sym }
  end

  def each_locale
    locale_prefixes.each do |locale|
      Maglev::I18n.with_locale(locale) do
        yield(locale)
      end
    end
  end

  class LocalesSerializer
    def self.dump(array)
      (array || []).map(&:as_json)
    end

    def self.load(array)
      (array || []).map { |attributes| Maglev::Site::Locale.new(**attributes.symbolize_keys) }
    end
  end
end
# rubocop:enable Style/ClassAndModuleChildren

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
maglevcms-1.5.1 app/models/maglev/site/locales_concern.rb
maglevcms-1.4.0 app/models/maglev/site/locales_concern.rb