5

Active Admin provides custom filters and scopes that are displayed on the index page like so:

my admin app/admin/compensatory_off.rb code is

ActiveAdmin.register CompensatoryOff do

scope :pending, default: true
scope :approved

filter :claim_date, collection: proc { CompensatoryOff.all.map(&:claim_date) }

My Controller for index action : 

controller do
  def index
    @page_title = "Comp-Off"
    index! do |format|
       if (current_user.has_role? :HRMS_Supervisor)
           @compensatory_offs = CompensatoryOff.get_subordinate_employees_comp_offs(current_user).page(params[:page]).per(10)
         else
           @compensatory_offs = current_user.employee.compensatory_offs.order("compensatory_offs.claim_date DESC").page(params[:page]).per(10) if current_user.employee
         end

         format.html {
           render "active_admin/resource/index"
         }
       end
     end
  end
end

i used both filter and scope in the above code.

in my model i defined the scopes also ,

class CompensatoryOff < ActiveRecord::Base
  scope :pending, ->{where(status: "Pending Approval")}
  scope :approved, ->{where(status: "Approved")}

  #scope :claim_date_eq, -> (claim_date) {where(claim_date: claim_date)}
end 

but in the above code both scope and filters are not working. the index page is not resulting anything it displaying all the records.

Note : when i remove my custom index action from the controller both the scope and filters working fine.

my admin app/admin/compensatory_off.rb code is

ActiveAdmin.register CompensatoryOff do

scope :pending, default: true
scope :approved

filter :claim_date, collection: proc { CompensatoryOff.all.map(&:claim_date) }

My Controller for index action : 

controller do
end

end

How the index action resource is getting fetched by default and i want to override the index resource with my own condition.

any thing need to add with the index action,

Please any help me to achieve this, am struggling a lot to find the result.

Thanks.

2 Answers 2

4

using scoped_collection its working,

controller do
    def scoped_collection
      if (current_user.has_role? :HRMS_Supervisor)
        @leave_availed_details = LeaveAvailedDetail.get_subordinate_employees_leaves(current_user).page(params[:page]).per(10)
      else
        @leave_availed_details = current_user.employee.leave_availed_details.order("leave_availed_details.status DESC").page(params[:page]).per(10) if current_user.employee
      end
    end

    # def index
    #   #@page_title = "Leave Details"
    # end
end 

for pagination use the below code.

before_filter :only => :index do
      @per_page = 10
    end
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

def index
  if (current_user.has_role? :HRMS_Supervisor)
    @compensatory_offs = CompensatoryOff.get_subordinate_employees_comp_offs(current_user)
  else
    @compensatory_offs = current_user.employee.compensatory_offs.order("compensatory_offs.claim_date DESC") if current_user.employee
  end

  index!
end

The pagination will be handled by ActiveAdmin

3 Comments

this code gives the below error, undefined local variable or method `collection_before_scope' for #<ActiveAdmin::Views::Scopes:0x007f3f82178180>
Yep, scoped_collection is the solution.
Pagination can only work if the collection returned from scoped_collection is an ActiveRecord::Relation (not an Array).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.