1

I am trying to filter the index according to the ability. I am using the wice_grid gem to make a table in the index, and to add a condition to tickets, we could use something called :conditions.

I have tried to put it like that:

@tickets_grid = initialize_grid(Ticket,
                                :include => [:user, :employee_department, :state],
                                :conditions => [Ticket.accessible_by(current_ability)])

This is not working, though. I'm looking for any suggestions.

Update: :conditions is working like ActiveRecord so I guess I need a query to look up through roles and detect the current ability

Charles
  • 50,943
  • 13
  • 104
  • 142
Mostafa Hussein
  • 11,063
  • 3
  • 36
  • 61

2 Answers2

1

What you are looking for is current_ability.model_adapter(Ticket, :index).conditions where Ticket is your model and :index is your access type.

so in your case it should be:

@tickets_grid = initialize_grid(Ticket,:include => [:user, :employee_department, :state],:conditions => current_ability.model_adapter(Ticket, :index).conditions)

https://github.com/ryanb/cancan/wiki/Fetching-Records

0

From the section of the documentation you linked to:

Alternatively, instead of a Class object as the first parameter, you can use ActiveRecord::Relation:

@tasks_grid = initialize_grid(Task.where(:archived => false, :projects => {:active => true}).joins(:project) )

Ticket.accessible_by(current_ability) is a relation, not a hash of conditions, so you would do initialize_grid(Ticket.accessible_by(current_ability)). But you should already have @tickets initialized by CanCan, so you just need to initialize_grid(@tickets).

graywh
  • 9,640
  • 2
  • 29
  • 27
  • did not work , i guess thats because wice grid is not accepting `@tickets` – Mostafa Hussein Sep 18 '13 at 19:08
  • i guess its should be in :conditions , you said its a relation so i need a condition (because conditions like an active record ) to make it look through the database fields , i am using the separate role model , users has many roles thorugh assignments – Mostafa Hussein Sep 18 '13 at 23:21