Mohit Jain
Mohit Jain

Reputation: 43939

How to display scopes based on the role of the user in active admin?

I am getting an error:

 undefined local variable or method `current_admin_user' for #<ActiveAdmin::ResourceDSL

How can I access current_admin_user object here?

  ActiveAdmin.register Job do
    def quick_filters
       if current_admin_user.super_admin?
         [:all, :draft, :scheduling, :rejected]
       else
         [:all, :scheduling, :rejected]
       end
    end

     quick_filters.each do |a|
       scope a
     end
  end

This is working:

controller do
   def scoped_collection
      @jobs = if current_admin_user.super_admin?
            super.includes :trip, :car, :concierge, :creator, :job_template
          else
            super.where(concierge_id: current_admin_user.id).includes :trip, :car, :concierge, :creator, :job_template
          end
   end
end

Upvotes: 0

Views: 1074

Answers (1)

Sjors Branderhorst
Sjors Branderhorst

Reputation: 2182

I think this should work:

ActiveAdmin.register Job do

  scope :all
  scope :draft, :if => proc { current_admin_user.super_admin? }
  scope :scheduling
  scope :rejected

end

The DSL for setting up scopes is evaluated at server startup time and does not have access to request cycle based data (like the logged in user). To get around this ActiveAdmin has the concept of wrapping code code with proc so that the enclosed code evaluation is deferred to request-cycle-time.

Good luck!

Upvotes: 2

Related Questions