3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/active_admin_paranoia/dsl.rb', line 3
def active_admin_paranoia
archived_at_column = config.resource_class.paranoia_column
not_archived_value = config.resource_class.paranoia_sentinel_value
do_archive = proc do |ids, resource_class, controller|
resource_class.where(id: ids).destroy_all
options = { notice: I18n.t('active_admin_paranoia.batch_actions.succesfully_archived', count: ids.count, model: resource_class.model_name, plural_model: resource_class.to_s.downcase.pluralize) }
if Rails::VERSION::MAJOR >= 5
controller.redirect_back(**{ fallback_location: ActiveAdmin.application.root_to }.merge(options))
else
controller.redirect_to :back, options
end
end
controller do
def find_resource
resource_class.with_deleted.public_send(method_for_find, params[:id])
end
def destroy_resource(object)
object.really_destroy!
end
end
batch_action :archive, confirm: proc{ I18n.t('active_admin_paranoia.batch_actions.archive_confirmation', plural_model: resource_class.to_s.downcase.pluralize) }, if: proc{ authorized?(ActiveAdminParanoia::Auth::ARCHIVE, resource_class) && params[:scope] != 'archived' } do |ids|
do_archive.call(ids, resource_class, self)
end
batch_action :destroy, if: proc { false } do
end
batch_action :restore, confirm: proc{ I18n.t('active_admin_paranoia.batch_actions.restore_confirmation', plural_model: resource_class.to_s.downcase.pluralize) }, if: proc{ authorized?(ActiveAdminParanoia::Auth::RESTORE, resource_class) && params[:scope] == 'archived' } do |ids|
resource_class.restore(ids, recursive: true)
options = { notice: I18n.t('active_admin_paranoia.batch_actions.succesfully_restored', count: ids.count, model: resource_class.model_name, plural_model: resource_class.to_s.downcase.pluralize) }
if Rails::VERSION::MAJOR >= 5
redirect_back(**{ fallback_location: ActiveAdmin.application.root_to }.merge(options))
else
redirect_to :back, options
end
end
action_item :archive, only: :show, if: proc { !resource.send(archived_at_column) } do
link_to(I18n.t('active_admin_paranoia.archive_model', model: resource_class.to_s.titleize), "#{resource_path(resource)}/archive", method: :put, data: { confirm: I18n.t('active_admin_paranoia.archive_confirmation') }) if authorized?(ActiveAdminParanoia::Auth::ARCHIVE, resource)
end
action_item :restore, only: :show, if: proc { resource.send(archived_at_column) } do
link_to(I18n.t('active_admin_paranoia.restore_model', model: resource_class.to_s.titleize), "#{resource_path(resource)}/restore", method: :put, data: { confirm: I18n.t('active_admin_paranoia.restore_confirmation') }) if authorized?(ActiveAdminParanoia::Auth::RESTORE, resource)
end
member_action :archive, method: :put, confirm: proc{ I18n.t('active_admin_paranoia.archive_confirmation') }, if: proc{ authorized?(ActiveAdminParanoia::Auth::ARCHIVE, resource_class) } do
do_archive.call([resource.id], resource_class, self)
end
member_action :restore, method: :put, confirm: proc{ I18n.t('active_admin_paranoia.restore_confirmation') }, if: proc{ authorized?(ActiveAdminParanoia::Auth::RESTORE, resource_class) } do
resource.restore(recursive: true)
options = { notice: I18n.t('active_admin_paranoia.batch_actions.succesfully_restored', count: 1, model: resource_class.model_name, plural_model: resource_class.to_s.downcase.pluralize) }
if Rails::VERSION::MAJOR >= 5
redirect_back(**{ fallback_location: ActiveAdmin.application.root_to }.merge(options))
else
redirect_to :back, options
end
end
scope(I18n.t('active_admin_paranoia.non_archived'), default: true, show_count: false) { |scope| scope.unscope(:where => archived_at_column).where(archived_at_column => not_archived_value) }
scope(I18n.t('active_admin_paranoia.archived'), show_count: false) { |scope| scope.unscope(:where => archived_at_column).where.not(archived_at_column => not_archived_value) }
end
|