Search code examples
ruby-on-railsrubycancancancancan

CanCanCan Ability Viewing in the UI


I'm working with a large list of abilities for users with different Role types on a rails application. This is with the CanCanCan gem.

It has gotten quite large, and I have non-dev users who want to be able to see a run-down of abilities for different users. How can I represent the existing abilities on a user interface?

i.e.

class MyUserAbilityController < ActionController::Base

  def show
    @abilities = current_user.abilities
  end

  def index

    @full_abilities = Hash.new

    Role.all.each do |role|
      @full_abilities[role.name] = role.abilities
    end

  end

end

And then pass it along to the view?

Further, although not necessary, is there a way I can dynamically change or alter the CanCanCan abilities from an external source?

I have been playing around with it in the Terminal for now, with an eventual goal to be able to have some basic rule-setting (not creation, but perhaps changing 'can's to 'cannot's) on an admin user interface with radio buttons.

-Schwad


Solution

  • I have decided to develop a gem to solve this answer, it's still very much in development, but soon fit for purpose. It's called CanCanCanSee.

    Check it out here

    It brings you the method CanCanCanSee.all_abilities to render a hash of existing abilities and roles.

    Also if you want to run a pretty printed version of your abilities from the Terminal (think like you do with rake routes) you have CanCanCanSee.pretty_print_abilities

    Thanks!