Question

Currently my navigation partial is as given below.

- if (can? :manage, Invoice) and (can? :manage, InvoiceItem)
  %li{class: is_active?(["invoices", "invoice_items"])}
    = link_to invoices_path do
      %i.icon-file-text
      Invoices

I see that the can method that define ability can accept an array. Is there a way to make the can? helper method more concise?

UPDATE: I have written a small application helper to sort this out. Is this the best way?

def user_can?(actions, resources)
  actions.each do |action|
    resources.each do |resource|
      return false if cannot? action, resource
    end
  end
  true
end

And in the partial:

- if user_can? [:manage], [Invoice, InvoiceItem]
  %li{class: is_active?(["invoices", "invoice_items"])}
    = link_to invoices_path do
      %i.icon-file-text
      Invoices
Was it helpful?

Solution

can? only evaluates one action and one resource at a time as you noted. Your helper is fine, assuming that you don't need to check different combinations of actions and resources, which you typically don't do in a single conditional statement in a view.

You probably also want to consider why you really need to check for :manage access to InvoiceItem when deciding whether to display a list of Invoices.

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