How do I show unscoped models in Rails Admin?
Asked Answered
S

4

6

I needed this myself, so here it is QA-style:

By default, Rails Admin shows a model's default_scope. How do I get it to show every model completely unscoped?

Summerly answered 19/11, 2013 at 10:48 Comment(0)
F
16

Approach 1

If you only need to list the records you can use the scopes method to control which records are returned. The first array element is the default, so if you add the following to your initialiser:

list do
  scopes [:unscoped]
end

you will see all records.

Approach 2

If you want to do more than list models you can create a dummy rails admin model. For example, assuming you have a Post model with a boolean archived flag:

class Post < ActiveRecord::Base
  default_scope { archived: false }
end

You can create a dummy model to use in rails_admin like so (in app/models/rails_admin)

class RailsAdmin::Post < ActiveRecord::Base
  self.table_name = "posts"
end

You then configure rails_admin to use RailsAdmin::Post and all of the Posts will be unscoped.

Forefoot answered 29/1, 2015 at 1:20 Comment(0)
S
5

Add this monkey patch to your rails admin initializer:

### Monkey pactch for unscoped records in admin panel
require 'rails_admin/main_controller'
module RailsAdmin
  class MainController
    alias_method :old_get_collection, :get_collection
    alias_method :old_get_object, :get_object

    def get_collection(model_config, scope, pagination)
      old_get_collection(model_config, model_config.abstract_model.model.unscoped, pagination)
    end

    def get_object
      raise RailsAdmin::ObjectNotFound unless (object = @abstract_model.model.unscoped.find(params[:id]))
      @object = RailsAdmin::Adapters::ActiveRecord::AbstractObject.new(object)
    end
  end
end

Taken from https://github.com/sferik/rails_admin/issues/353.

Summerly answered 19/11, 2013 at 10:48 Comment(0)
E
4

I have a solution similar to Charles' above but that monkey patches the model layer rather than the controller layer. This might be a bit more stable across Rails Admin releases, but is ActiveRecord-specific and doesn't affect Mongoid, though the principle would be easily applied to the other adapter.

Again, put it in the rails admin initializer.

#
# Monkey patch to remove default_scope
#
require 'rails_admin/adapters/active_record'
module RailsAdmin::Adapters::ActiveRecord
  def get(id)
    return unless object = scoped.where(primary_key => id).first
    AbstractObject.new object
  end
  def scoped
    model.unscoped
  end
end
Else answered 8/10, 2015 at 4:21 Comment(2)
I'm making a pull request for the rails_admin project so hopefully we won't have to monkey patch any more.Else
If you're getting an error claiming that object is not defined, like I was, I found adding this line right below def get(id) solved my problem: object = model.with_deleted.find(id) (in my case, I'm using paranoia, so my "un-scoped scope" is with_deleted)Obligor
D
0

My monkey patch, for Mongoid:

module RailsAdminFindUnscopedPatch
  def get(id)
    RailsAdmin::Adapters::Mongoid::AbstractObject.new(model.unscoped.find(id))
  rescue
    super
  end
end

RailsAdmin::Adapters::Mongoid.prepend(RailsAdminFindUnscopedPatch)

I'm reusing original rescue clause (super call).

Doukhobor answered 17/1, 2018 at 4:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.