2

I would like to get all deleted records change by paranoia on admin panel with administrate. My problem is that I'm finding a way to do this stuff but until now without success

Actually what I'm trying to do is to override index method on a specific controller generate with Administrate, in order to get all elements (deleted or not) as resources for the current controller. Like so:

controllers/admin/foo_controller.rb

module Admin
  class FooController < Admin::ApplicationController
    super
    resources = Foo.where("at_delete IS NOT NULL").page(params[:page])
  end
end

But when I create a foo object from the admin panel, and after removed it. The record isn't display anymore and I would like to get it still visible for giving the possibility to admin to change it.

If anyone has an opinion to make this possible, it would be nice.

Thank you in advance for your help.

Snoobie
  • 760
  • 2
  • 12
  • 30
  • I don't understand your question. Please provide a [mcve] of your current code. I **think** what you're asking is "I'd like a page to view soft-deleted records" - is that right? Why are you using such non-standard config for `paranoia`? Normally, deleted records are *automatically* hidden; but you can explicitly display them via `Model.with_deleted` or `acts_as_paranoid without_default_scope: true` - as explained in the project's README. – Tom Lord Jan 09 '18 at 11:51
  • Yes, I already read all the README from `paranoia`, and I saw that it's possible to use `Model.with_deleted` to get the values that I'm finding. But the problem actually is with `Administrate` because, it is not possible for me right now to change the resources that would be displayed on the admin panel of administrate. – Snoobie Jan 09 '18 at 12:41
  • And I'm sorry but I'm not so fluent in english, so it's kind of difficult to explain problems sometimes. I would ask you to show a little indulgence. – Snoobie Jan 09 '18 at 12:46
  • 1
    I just looked at the `administrate` code (I've never used this library before, by the way), and it [looks like you can override the `scoped_resource ` method](https://github.com/thoughtbot/administrate/blob/64c7eba217c2e368865a81b4eab0813331d6774f/app/controllers/administrate/application_controller.rb#L118-L120)? So just something like: `def scoped_resource; Foo.with_deleted; end`? – Tom Lord Jan 09 '18 at 13:02

1 Answers1

2

First of all a big thanks to @Tom Lord, that help me to solved my problem, and that is the solution:

  1. First of all override in your app/controllers/admin/application_controller.rb generate by Administrate as below:
module Admin
  class ApplicationController < Administrate::ApplicationController

    ...

    def index
      search_term = params[:search].to_s.strip
      resources = Administrate::Search.new(scoped_resource,
                                       dashboard_class,
                                       search_term).run
      resources = resources.includes(*resource_includes) if resource_includes.any?
      resources = order.apply(resources)
      resources = resources.page(params[:page]).per(records_per_page)
      resources = finder_chain_additions(resources)
      page = Administrate::Page::Collection.new(dashboard, order: order)

      render locals: {
        resources: resources,
        search_term: search_term,
        page: page,
        show_search_bar: show_search_bar?
      }
    end

  private
    def scoped_resource
      begin
        # Provide resource with deleted_at field generate with Paranoia
        resource_class.unscoped
      rescue
        # Used for models whose don't have Paranoia field
        resource_class
      end
    end

    def finder_chain_additions resources
      begin
        resources.with_deleted
      rescue
        resources
      end
    end
  end
end
  1. In your app/dashboards/foo_dashboard.rb
require "administrate/base_dashboard"

class ArticleDashboard < Administrate::BaseDashboard
  # ATTRIBUTE_TYPES
  # a hash that describes the type of each of the model's fields.
  #
  # Each different type represents an Administrate::Field object,
  # which determines how the attribute is displayed
  # on pages throughout the dashboard.
  ATTRIBUTE_TYPES = {
    id: Field::Number,
    ...
    created_at: Field::DateTime,
    updated_at: Field::DateTime,
    deleted_at: Field::DateTime,
  }.freeze

  # COLLECTION_ATTRIBUTES
  # an array of attributes that will be displayed on the model's index page.
  #
  # By default, it's limited to four items to reduce clutter on index pages.
  # Feel free to add, remove, or rearrange items.
  COLLECTION_ATTRIBUTES = [
    :id,
    ...
    :deleted_at,
    :hide,
  ].freeze

  # SHOW_PAGE_ATTRIBUTES
  # an array of attributes that will be displayed on the model's show page.
  SHOW_PAGE_ATTRIBUTES = [
    :id,
    ...
    :deleted_at,    
    :created_at,
    :updated_at,
  ].freeze

  # FORM_ATTRIBUTES
  # an array of attributes that will be displayed
  # on the model's form (`new` and `edit`) pages.
  FORM_ATTRIBUTES = [
    ...
    :deleted_at,
  ].freeze

  # Overwrite this method to customize how articles are displayed
  # across all pages of the admin dashboard.
  #
  # def display_resource(article)
  #   "Article ##{article.id}"
  # end
end

Of course change deleted_at field depends on the migration you may have done in order to add paranoia gem.

Snoobie
  • 760
  • 2
  • 12
  • 30