muhammedabuali
muhammedabuali

Reputation: 611

How to hide an item from a list based on some attribute in rails_admin

in the admin view in the rails_admin gem, I have the Cart model

Cart(id: integer, user_id: integer, status: string, created_at: datetime, updated_at: datetime)

and I don't want the admin to see it until it's status is confirmed. how do I hide it from the list view in rails_admin?

so I want something like this

if cart.status == 'new'
  hide

Upvotes: 1

Views: 679

Answers (2)

muhammedabuali
muhammedabuali

Reputation: 611

the only way i saw possible is to stick with the cancan gem for this case the following code can do it

if user.admin?
  cannot :read, Cart, :status=> 'new'
end

Upvotes: 2

Carl
Carl

Reputation: 991

Does the unconfirmed cart need to be in the list?

If not then it would be better to not put it in there. As an example:

# carts_controller.rb
def index
  @carts = Cart.where( status: 'confirmed' )
end

You can also set up scopes, so your Cart has a method named 'confirmed':
Scoping

Otherwise you can also call 'group'

@carts = Cart.group( :status )

Which will group your carts by status.

Grouping

Upvotes: 0

Related Questions