Sha256: cafbf6627ceeed83f66b53aa7b8263d1ad1278d90c2ed3712a563efa0bd00b20

Contents?: true

Size: 1.8 KB

Versions: 62

Compression:

Stored size: 1.8 KB

Contents

module Mongoid
  module AuditLog
    decorate Entry, with: :workarea do
      decorated do
        field :release_id, type: String
        before_save :set_release_id

        index({ action: 1, created_at: 1 })
        index({ action: 1, audited_type: 1, created_at: 1 })
        index({ audited_id: 1, audited_type: 1, release_id: 1 })
        index({ audited_type: 1 })
        index({ 'document_path.id' => 1 })
        index(
          { created_at: 1 },
          { expire_after_seconds: 3.months.seconds.to_i }
        )
      end

      def model_name
        model_attributes['name'][I18n.locale.to_s].presence ||
          model_attributes['name']
      end

      def release
        @release ||= try_scheduled_for_release_change ||
                      try_performed_by_release_change
      end

      def publish?
        tracked_changes.keys == %w(published_at publish_at) ||
          tracked_changes.keys == %w(published_at)
      end

      def restorable?
        super && !orphaned?
      end

      private

      def try_scheduled_for_release_change
        return nil if release_id.blank?
        Workarea::Release.find(release_id) rescue nil
      end

      # HACK to try to match a release for publishing display
      def try_performed_by_release_change
        return unless modifier.try(:system?)
        Workarea::Release.find_by(name: modifier.first_name) rescue nil
      end

      def set_release_id
        if audited_type.present? && audited_type.constantize < Workarea::Releasable
          self.release_id = Workarea::Release.current.try(:id)
        end
      end

      def orphaned?
        return false unless audited_type.constantize.include?(Mongoid::Tree)

        parent = audited_type.constantize.find(model_attributes['parent_id']) rescue nil

        parent.blank?
      end
    end
  end
end

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
workarea-core-3.5.27 lib/workarea/ext/mongoid/audit_log_entry.decorator
workarea-core-3.5.26 lib/workarea/ext/mongoid/audit_log_entry.decorator
workarea-core-3.4.45 lib/workarea/ext/mongoid/audit_log_entry.decorator
workarea-core-3.5.25 lib/workarea/ext/mongoid/audit_log_entry.decorator
workarea-core-3.5.23 lib/workarea/ext/mongoid/audit_log_entry.decorator
workarea-core-3.4.44 lib/workarea/ext/mongoid/audit_log_entry.decorator
workarea-core-3.5.22 lib/workarea/ext/mongoid/audit_log_entry.decorator
workarea-core-3.4.43 lib/workarea/ext/mongoid/audit_log_entry.decorator
workarea-core-3.5.21 lib/workarea/ext/mongoid/audit_log_entry.decorator
workarea-core-3.4.42 lib/workarea/ext/mongoid/audit_log_entry.decorator
workarea-core-3.5.20 lib/workarea/ext/mongoid/audit_log_entry.decorator
workarea-core-3.4.41 lib/workarea/ext/mongoid/audit_log_entry.decorator
workarea-core-3.5.19 lib/workarea/ext/mongoid/audit_log_entry.decorator
workarea-core-3.4.40 lib/workarea/ext/mongoid/audit_log_entry.decorator
workarea-core-3.5.18 lib/workarea/ext/mongoid/audit_log_entry.decorator
workarea-core-3.4.39 lib/workarea/ext/mongoid/audit_log_entry.decorator
workarea-core-3.5.17 lib/workarea/ext/mongoid/audit_log_entry.decorator
workarea-core-3.4.38 lib/workarea/ext/mongoid/audit_log_entry.decorator
workarea-core-3.5.16 lib/workarea/ext/mongoid/audit_log_entry.decorator
workarea-core-3.4.37 lib/workarea/ext/mongoid/audit_log_entry.decorator