Skip to content
This repository

add "unscoped" mode to "model config" #1348

Open
ideomix opened this Issue · 6 comments

6 participants

Ryo Matsuo Michael Frederick Benoit Bénézech Gady Curter29 Jamie Phelps
Ryo Matsuo

I want to add a "unscoped" mode to "model config".
I also read the entry in the past, such a way as to create a subclass is difficult to accept.
To "model config" section of the "raid_admin.rb" simple, strongly desired a solution that can be configured as "unscoped = true".

What everyone think?

Ryo Matsuo

I'm sorry in poor English.

Michael Frederick

+1 for support for unscoped.

For example, my app has a BlogPost model and my default_scope is set such that a BlogPost must be marked as published. The only time I would want to be able to see an unpublished BlogPost is in the admin area, so it is less than ideal for me to have to remove my default_scope.

Benoit Bénézech
Collaborator

Can you sublass your model to add the unscoping?

Someone did this once. I wish someone would make this work reliably (routing, naming, etc.) and document it in the wiki.

The whole idea of RailsAdmin is "read my models, show me what they are and don't be smart about it".

I can't think of a nice way to include an 'unscoped' feature.

Gady

+1
any update on this?

Curter29

+1

Jamie Phelps

Any update on this? Here's a particularly compelling use case:

class Product < ActiveRecord::Base
  default_scope -> { where(active: true) }
  # etc
end

Then in my view

<%= render Product.all %>

The workaround is to just not have a default scope:

class Product < ActiveRecord::Base
  scope :active, -> { where(active: true) }
  # etc
end

Then anywhere I want to show products to the user-facing app, I have to remember to call Product.active as part of the AREL chain. This is the opposite of DRY.

But in my admin view, I should be able to edit all products, regardless of whether it's active or not. I have another mechanism for controlling whether the inactive records can be read or updated. This is the power of RailsAdmin but by not unscoping the queries by default, it's hamstringing the simplicity and automatic nature of RailsAdmin.

I was surprised that this wasn't the default or at very least to see a config option for allowing it. Something like this seems perfectly reasonable:

class Product < ActiveRecord::Base
  default_scope :active, -> { where(active: true) }
  rails_admin do
    # list, etc.
    preferred_scope :unscoped
  end
end

should be sufficient. So, if config.preferred_scope is set, before doing any of the model fetching

module RailsAdmin
  module Adapters
    module ActiveRecord
      def scoped
        if config.preferred_scope
          model.send(config.preferred_scope.to_sym)
        else
          model.all
        end
      end
    end
  end
end

This would allow the individual models to scope themselves just like they're allowed to control which attributes are visible and editable. As long as the preferred_scope method returns an ActiveRecord::Relation, then the rest of the engine gets to carry on just as it has. Seems to me like a precise config option to add and conditional into the scoped method with little impact elsewhere.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.