Sha256: 67c428adb9a25d31d6ea5af647696c907e4d0e85dd3db1fbcf8381f505c2b830

Contents?: true

Size: 1.16 KB

Versions: 2

Compression:

Stored size: 1.16 KB

Contents

module ActsAsTrashable
  extend ActiveSupport::Concern

  module ActiveRecord
    def acts_as_trashable(*options)
      @acts_as_trashable_options = options.try(:first) || {}

      unless @acts_as_trashable_options.kind_of?(Hash)
        raise ArgumentError.new("invalid arguments passed to (effective_trash) acts_as_trashable. Expecting no options.")
      end

      include ::ActsAsTrashable
    end
  end

  included do
    has_one :trash, -> { where(status: EffectiveLogging.trashable_status) }, as: :associated, class_name: Effective::Log

    before_destroy do
      EffectiveLogging::ActiveRecordLogger.new(self, acts_as_trashable_options).trashed!
      true
    end

    # Parse Options
    acts_as_trashable_options = {
      only: Array(@acts_as_trashable_options[:only]).map { |attribute| attribute.to_s },
      except: Array(@acts_as_trashable_options[:except]).map { |attribute| attribute.to_s },
      additionally: Array(@acts_as_trashable_options[:additionally]).map { |attribute| attribute.to_s }
    }

    self.send(:define_method, :acts_as_trashable_options) { acts_as_trashable_options }
  end

  module ClassMethods
  end

  # Regular instance methods

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
effective_logging-1.7.1 app/models/concerns/acts_as_trashable.rb
effective_logging-1.7.0 app/models/concerns/acts_as_trashable.rb