Module: ActiveArchive::Base

Defined in:
lib/active_archive/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object


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

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

  base.instance_eval do
    define_model_callbacks :archive, only: %i[before after]
    define_model_callbacks :unarchive, only: %i[before after]
  end
end

Instance Method Details

#archivable?Boolean

Returns:

  • (Boolean)

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

def archivable?
  respond_to?(:archived_at)
end

#archival?Boolean

Returns:

  • (Boolean)

16
17
18
19
20
# File 'lib/active_archive/base.rb', line 16

def archival?
  return destroyed? if unarchivable?

  (will_save_change_to_archived_at? || saved_change_to_archived_at?) && archived?
end

#archiveObject


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

def archive
  return destroy if unarchivable?

  with_transaction_returning_status do
    run_callbacks :archive do
      mark_as_archived
      mark_relections_as_archived

      self
    end
  end
end

#archived?Boolean

Returns:

  • (Boolean)

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

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

#to_archivalObject


70
71
72
# File 'lib/active_archive/base.rb', line 70

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

#unarchivable?Boolean

Returns:

  • (Boolean)

53
54
55
# File 'lib/active_archive/base.rb', line 53

def unarchivable?
  !archivable?
end

#unarchival?Boolean

Returns:

  • (Boolean)

43
44
45
46
47
# File 'lib/active_archive/base.rb', line 43

def unarchival?
  return !destroyed? if unarchivable?

  (will_save_change_to_archived_at? || saved_change_to_archived_at?) && unarchived?
end

#unarchiveObject


57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/active_archive/base.rb', line 57

def unarchive
  return self if unarchivable?

  with_transaction_returning_status do
    run_callbacks :unarchive do
      mark_as_unarchived
      mark_relections_as_unarchived

      self
    end
  end
end

#unarchived?Boolean

Returns:

  • (Boolean)

49
50
51
# File 'lib/active_archive/base.rb', line 49

def unarchived?
  !archived?
end