Sha256: 7e58ed6efe6929b0ba8fc1ffdcd9e41157f02c800dc5dd3cbb80bdd3081eda2b

Contents?: true

Size: 1.98 KB

Versions: 28

Compression:

Stored size: 1.98 KB

Contents

module Workarea
  module ApplicationDocument
    extend ActiveSupport::Concern

    include Mongoid::Document
    include Mongoid::AuditLog
    include Mongoid::Timestamps
    include Sidekiq::Callbacks
    include GlobalID::Identification

    included do
      before_validation :clean_array_fields
      before_create :ensure_default_locale_values
    end

    def releasable?
      is_a?(Releasable)
    end

    private

    def ensure_default_locale_values
      return if I18n.locale == I18n.default_locale

      default_locale = I18n.default_locale.to_s

      self.class.localized_fields.each do |name, field|
        translations = send("#{name}_translations")

        unless translations.key?(default_locale)
          send(
            "#{name}_translations=",

            # Grab the current value because we need something here. Fallbacks
            # don't work for the default locale if it doesn't exist.
            translations.merge(
              default_locale => translations[I18n.locale.to_s]
            )
          )
        end
      end
    end

    def clean_array_fields
      # Trying to modify the attributes of a document that is set to
      # to be destroyed will throw an error, as the attributes Hash is frozen.
      # This can occur when an instance of a model has embedded documents that
      # have been marked for deletion.
      return if self.destroyed?

      self.class.fields.each do |name, field|
        next unless field.type == ::Array

        if field.localized?
          translations = send("#{name}_translations")

          cleaned = translations.each_with_object({}) do |(locale, original), memo|
            if original.nil?
              memo[locale] = nil
              next
            end

            memo[locale] = original.reject(&:blank?)
          end

          send("#{name}_translations=", cleaned)
        else
          next unless original = send(name)

          send("#{name}=", original.reject(&:blank?))
        end
      end
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
workarea-core-3.5.27 app/models/workarea/application_document.rb
workarea-core-3.5.26 app/models/workarea/application_document.rb
workarea-core-3.5.25 app/models/workarea/application_document.rb
workarea-core-3.5.23 app/models/workarea/application_document.rb
workarea-core-3.5.22 app/models/workarea/application_document.rb
workarea-core-3.5.21 app/models/workarea/application_document.rb
workarea-core-3.5.20 app/models/workarea/application_document.rb
workarea-core-3.5.19 app/models/workarea/application_document.rb
workarea-core-3.5.18 app/models/workarea/application_document.rb
workarea-core-3.5.17 app/models/workarea/application_document.rb
workarea-core-3.5.16 app/models/workarea/application_document.rb
workarea-core-3.5.15 app/models/workarea/application_document.rb
workarea-core-3.5.14 app/models/workarea/application_document.rb
workarea-core-3.5.13 app/models/workarea/application_document.rb
workarea-core-3.5.12 app/models/workarea/application_document.rb
workarea-core-3.5.11 app/models/workarea/application_document.rb
workarea-core-3.5.10 app/models/workarea/application_document.rb
workarea-core-3.5.9 app/models/workarea/application_document.rb
workarea-core-3.5.8 app/models/workarea/application_document.rb
workarea-core-3.5.7 app/models/workarea/application_document.rb