Module: ActsAsArchived

Extended by:
ActiveSupport::Concern
Defined in:
app/models/concerns/acts_as_archived.rb

Overview

Each controller needs its own archive and unarchive action. To simplify this, use the following route concern.

In your routes.rb:

Rails.application.routes.draw do

acts_as_archived

resource :things, concern: :acts_as_archived
resource :comments, concern: :acts_as_archived

end

and include Effective::CrudController in your resource controller

Defined Under Namespace

Modules: Base, CanCan, ClassMethods, RoutesConcern

Instance Method Summary collapse

Instance Method Details

#archive!Object

Instance methods



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'app/models/concerns/acts_as_archived.rb', line 108

def archive!
  return true if archived?

  strategy = acts_as_archived_options[:strategy]
  cascade = acts_as_archived_options[:cascade]

  transaction do
    run_callbacks :archive do
      update!(archived: true) # Runs validations

      if strategy == :archive_all
        cascade.each { |associated| public_send(associated).update_all(archived: true) }
      end

      if strategy == :archive
        cascade.each { |associated| Array(public_send(associated)).each { |resource| resource.archive! } }
      end
    end
  end

  if strategy == :active_job
    ::ActsAsArchivedArchiveJob.perform_later(self)
  end

  true
end

#unarchive!Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'app/models/concerns/acts_as_archived.rb', line 135

def unarchive!
  return true unless archived?

  strategy = acts_as_archived_options[:strategy]
  cascade = acts_as_archived_options[:cascade]

  transaction do
    run_callbacks :unarchive do
      update!(archived: false) # Runs validations

      if strategy == :archive_all
        cascade.each { |associated| public_send(associated).update_all(archived: false) }
      end

      if strategy == :archive
        cascade.each { |associated| Array(public_send(associated)).each { |resource| resource.unarchive! } }
      end
    end
  end

  if strategy == :active_job
    ::ActsAsArchivedUnarchiveJob.perform_later(self)
  end

  true
end