Module: ActiveArchive::Base

Defined in:
lib/active_archive/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



8
9
10
11
12
13
# File 'lib/active_archive/base.rb', line 8

def self.included(base)
  base.extend Methods
  base.extend Scopes

  base.instance_eval { define_model_callbacks(:unarchive) }
end

Instance Method Details

#archivable?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/active_archive/base.rb', line 15

def archivable?
  respond_to?(:archived_at)
end

#archived?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/active_archive/base.rb', line 19

def archived?
  archivable? ? !archived_at.nil? : destroyed?
end

#destroy(force = nil) ⇒ Object Also known as: archive



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/active_archive/base.rb', line 42

def destroy(force = nil)
  with_transaction_returning_status do
    if unarchivable? || should_force_destroy?(force)
      permanently_delete_records_after { super() }
    else
      destroy_with_active_archive(force)

      if ::ActiveRecord::VERSION::MAJOR >= 5
        archived_at_will_change!
      elsif ::ActiveRecord::VERSION::MAJOR >= 4
        attribute_will_change!('archived_at')
      end
    end
  end
end

#to_archivalObject



60
61
62
# File 'lib/active_archive/base.rb', line 60

def to_archival
  I18n.t("active_archive.archival.#{archived? ? :archived : :unarchived}")
end

#unarchivable?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/active_archive/base.rb', line 27

def unarchivable?
  !archivable?
end

#unarchive(opts = nil) ⇒ Object Also known as: undestroy



31
32
33
34
35
36
37
38
# File 'lib/active_archive/base.rb', line 31

def unarchive(opts = nil)
  with_transaction_returning_status do
    records = should_unarchive_parent_first?(opts) ? unarchival.reverse : unarchival
    records.each { |rec| rec.call(opts) }

    self
  end
end

#unarchived?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/active_archive/base.rb', line 23

def unarchived?
  !archived?
end