├── lib ├── active_admin_paranoia │ ├── version.rb │ ├── engine.rb │ ├── authorization.rb │ └── dsl.rb └── active_admin_paranoia.rb ├── Gemfile ├── Rakefile ├── README.md ├── .gitignore ├── config └── locales │ └── en.yml ├── LICENSE └── active_admin_paranoia.gemspec /lib/active_admin_paranoia/version.rb: -------------------------------------------------------------------------------- 1 | module ActiveAdminParanoia 2 | VERSION = '1.0.11' 3 | end 4 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | # Specify your gem's dependencies in active_admin_paranoia.gemspec 4 | gemspec 5 | -------------------------------------------------------------------------------- /lib/active_admin_paranoia/engine.rb: -------------------------------------------------------------------------------- 1 | require 'rails' 2 | 3 | module ActiveAdminParanoia 4 | class Engine < ::Rails::Engine 5 | 6 | config.mount_at = '/' 7 | 8 | end 9 | end -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler" 2 | require 'rake' 3 | Bundler.setup 4 | Bundler::GemHelper.install_tasks 5 | 6 | # Import all our rake tasks 7 | FileList['tasks/**/*.rake'].each { |task| import task } -------------------------------------------------------------------------------- /lib/active_admin_paranoia.rb: -------------------------------------------------------------------------------- 1 | require 'active_admin' 2 | require 'active_admin_paranoia/version' 3 | require 'active_admin_paranoia/engine' 4 | require 'active_admin_paranoia/dsl' 5 | require 'active_admin_paranoia/authorization' 6 | ::ActiveAdmin::DSL.send(:include, ActiveAdminParanoia::DSL) 7 | -------------------------------------------------------------------------------- /lib/active_admin_paranoia/authorization.rb: -------------------------------------------------------------------------------- 1 | module ActiveAdminParanoia 2 | # Default Authorization permission for ActiveAdminParanoia 3 | module Authorization 4 | RESTORE = :restore 5 | PERMANENT_DELETE = :permanent_delete 6 | end 7 | 8 | Auth = Authorization 9 | end 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # active_admin_paranoia 2 | This gem extends ActiveAdmin so that batch restore and batch archive actions will be available in resource index page. Also 'All' and 'Archived' scope will be available for resource index page. 'All' scope will show non archived resources and 'Archived' scope will show deleted or archived resources. 3 | 4 | # Installation 5 | This gem assumes that you have already configured [paranoia](https://github.com/radar/paranoia) for you desire resource. Add this line to your application's Gemfile: 6 | 7 | ```ruby 8 | gem "active_admin_paranoia" , '~> 1.0.11' 9 | 10 | ``` 11 | 12 | And then execute: 13 | 14 | $ bundle 15 | 16 | # Usages 17 | 18 | ```ruby 19 | ActiveAdmin.register Post do 20 | active_admin_paranoia 21 | end 22 | ``` 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | *.rbc 3 | /.config 4 | /coverage/ 5 | /InstalledFiles 6 | /pkg/ 7 | /spec/reports/ 8 | /test/tmp/ 9 | /test/version_tmp/ 10 | /tmp/ 11 | 12 | ## Specific to RubyMotion: 13 | .dat* 14 | .repl_history 15 | build/ 16 | 17 | ## Documentation cache and generated files: 18 | /.yardoc/ 19 | /_yardoc/ 20 | /doc/ 21 | /rdoc/ 22 | 23 | ## Environment normalisation: 24 | /.bundle/ 25 | /lib/bundler/man/ 26 | 27 | # for a library or gem, you might want to ignore these files since the code is 28 | # intended to run in multiple environments; otherwise, check them in: 29 | # Gemfile.lock 30 | # .ruby-version 31 | # .ruby-gemset 32 | 33 | # unless supporting rvm < 1.11.0 or doing something fancy, ignore this: 34 | .rvmrc 35 | 36 | /.idea 37 | /.ruby-version 38 | /Gemfile.lock -------------------------------------------------------------------------------- /config/locales/en.yml: -------------------------------------------------------------------------------- 1 | en: 2 | active_admin: 3 | delete_model: "Archive %{model}" 4 | delete: "Archive" 5 | delete_confirmation: "Are you sure you want to archive this?" 6 | batch_actions: 7 | delete_confirmation: "Are you sure you want to archive these %{plural_model}?" 8 | succesfully_destroyed: 9 | one: "Successfully archived 1 %{model}" 10 | other: "Successfully archived %{count} %{plural_model}" 11 | labels: 12 | destroy: "Archive" 13 | active_admin_paranoia: 14 | batch_actions: 15 | restore_confirmation: "Are you sure you want to restore these %{plural_model}?" 16 | succesfully_restored: 17 | one: "Successfully restored 1 %{model}" 18 | other: "Successfully restored %{count} %{plural_model}" 19 | archived: "Archived" 20 | non_archived: "Non Archived" 21 | restore_model: "Restore %{model}" 22 | restore: "Restore" 23 | restore_confirmation: "Are you sure you want to restore this?" 24 | something_wrong: "Something went wrong. Please try again" 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Miah Raihan Mahmud Arman 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /active_admin_paranoia.gemspec: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | require File.expand_path('../lib/active_admin_paranoia/version', __FILE__) 3 | 4 | Gem::Specification.new do |gem| 5 | gem.name = 'active_admin_paranoia' 6 | gem.version = ActiveAdminParanoia::VERSION 7 | gem.license = 'MIT' 8 | gem.authors = ['Miah Raihan Mahmud Arman'] 9 | gem.email = ['raihan2006i@gmail.com'] 10 | gem.description = %q{This gem extends ActiveAdmin so that batch restore and batch archive actions will be available in resource index page. Also 'All' and 'Archived' scope will be available for resource index page. 'All' scope will show non archived resources and 'Archived' scope will show deleted or archived resources.} 11 | gem.summary = %q{Paranoia extension for ActiveAdmin} 12 | gem.homepage = 'https://github.com/raihan2006i/active_admin_paranoia' 13 | 14 | gem.files = `git ls-files`.split($/) 15 | gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) } 16 | gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) 17 | gem.require_paths = %w(lib) 18 | 19 | gem.add_dependency 'rails', '>= 4.0' 20 | gem.add_dependency 'paranoia', '>= 1.0' 21 | end 22 | -------------------------------------------------------------------------------- /lib/active_admin_paranoia/dsl.rb: -------------------------------------------------------------------------------- 1 | module ActiveAdminParanoia 2 | module DSL 3 | def active_admin_paranoia 4 | controller do 5 | def find_resource 6 | resource_class.to_s.camelize.constantize.with_deleted.where(id: params[:id]).first! 7 | end 8 | end 9 | 10 | batch_action :destroy, confirm: proc{ I18n.t('active_admin.batch_actions.delete_confirmation', plural_model: resource_class.to_s.downcase.pluralize) }, if: proc{ authorized?(ActiveAdmin::Auth::DESTROY, resource_class) && params[:scope] != 'archived' } do |ids| 11 | resource_class.to_s.camelize.constantize.where(id: ids).destroy_all 12 | options = { notice: I18n.t('active_admin.batch_actions.succesfully_destroyed', count: ids.count, model: resource_class.to_s.camelize.constantize.model_name, plural_model: resource_class.to_s.downcase.pluralize) } 13 | # For more info, see here: https://github.com/rails/rails/pull/22506 14 | if Rails::VERSION::MAJOR >= 5 15 | redirect_back({ fallback_location: ActiveAdmin.application.root_to }.merge(options)) 16 | else 17 | redirect_to :back, options 18 | end 19 | end 20 | 21 | 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| 22 | resource_class.to_s.camelize.constantize.restore(ids, recursive: true) 23 | options = { notice: I18n.t('active_admin_paranoia.batch_actions.succesfully_restored', count: ids.count, model: resource_class.to_s.camelize.constantize.model_name, plural_model: resource_class.to_s.downcase.pluralize) } 24 | # For more info, see here: https://github.com/rails/rails/pull/22506 25 | if Rails::VERSION::MAJOR >= 5 26 | redirect_back({ fallback_location: ActiveAdmin.application.root_to }.merge(options)) 27 | else 28 | redirect_to :back, options 29 | end 30 | end 31 | 32 | action_item :restore, only: :show do 33 | 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) 34 | end 35 | 36 | member_action :restore, method: :put, confirm: proc{ I18n.t('active_admin_paranoia.restore_confirmation') }, if: proc{ authorized?(ActiveAdminParanoia::Auth::RESTORE, resource_class) } do 37 | resource.restore(recursive: true) 38 | options = { notice: I18n.t('active_admin_paranoia.batch_actions.succesfully_restored', count: 1, model: resource_class.to_s.camelize.constantize.model_name, plural_model: resource_class.to_s.downcase.pluralize) } 39 | # For more info, see here: https://github.com/rails/rails/pull/22506 40 | if Rails::VERSION::MAJOR >= 5 41 | redirect_back({ fallback_location: ActiveAdmin.application.root_to }.merge(options)) 42 | else 43 | redirect_to :back, options 44 | end 45 | end 46 | 47 | scope(I18n.t('active_admin_paranoia.non_archived'), default: true) { |scope| scope.where(resource_class.to_s.camelize.constantize.paranoia_column => resource_class.to_s.camelize.constantize.paranoia_sentinel_value) } 48 | scope(I18n.t('active_admin_paranoia.archived')) { |scope| scope.unscope(:where => resource_class.to_s.camelize.constantize.paranoia_column).where.not(resource_class.to_s.camelize.constantize.paranoia_column => resource_class.to_s.camelize.constantize.paranoia_sentinel_value) } 49 | end 50 | end 51 | end 52 | 53 | module ActiveAdmin 54 | module Views 55 | class IndexAsTable < ActiveAdmin::Component 56 | class IndexTableFor < ::ActiveAdmin::Views::TableFor 57 | alias_method :orig_defaults, :defaults 58 | 59 | def defaults(resource, options = {}) 60 | if resource.respond_to?(:deleted?) && resource.deleted? 61 | if controller.action_methods.include?('restore') && authorized?(ActiveAdminParanoia::Auth::RESTORE, resource) 62 | # TODO: find a way to use the correct path helper 63 | item I18n.t('active_admin_paranoia.restore'), "#{resource_path(resource)}/restore", method: :put, class: "restore_link #{options[:css_class]}", 64 | data: {confirm: I18n.t('active_admin_paranoia.restore_confirmation')} 65 | end 66 | else 67 | orig_defaults(resource, options) 68 | end 69 | end 70 | end 71 | end 72 | end 73 | end 74 | --------------------------------------------------------------------------------