Question

I have a Product model.

Ability class is as follows:

class Ability
  include CanCan::Ability
  def initialize(user)
    if user.has_role? :super_admin
      can :manage, :all
    else
      can :read, Product, Product.all.limit(10) if user.has_role? :brand_manager
      can :access, :rails_admin   # grant access to rails_admin
      can :dashboard              # grant access to the dashboard
    end
  end
end

Here is the code for rails_admin:

RailsAdmin.config do |config|
  config.authorize_with :cancan
 end

I want to hide all the products from list which don't have particular name? Some how rails_admin does not support it. Can anyone help me to resolve this issue?

Was it helpful?

Solution

You can do something like this. 1. Create a new field in model like admin_user_id 2. While creating/updating product from save current admin user details in product data and use it ability class

class Ability
include CanCan::Ability
 def initialize(user)
   if user.has_role? :super_admin
     can :manage, :all
   elsif user.has_role? :brand_manager
     can :manage,Product, :admin_user_id=> user.id
     can :access, :rails_admin   # grant access to rails_admin
     can :dashboard              # grant access to the dashboard
   end
  end
end

OTHER TIPS

What do you mean with hide all the producs from the list ?

If you want that users can't read it, you can try:

can :read, Product do |product|
  [name1, name2].include?( product.name )
end

If you want don't return these products, you can use in your controller:

Product.where( :name.in => [name1, name2] )

I hope I've helped

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top