Skip to content

Instantly share code, notes, and snippets.

Created May 1, 2014 11:45
Show Gist options
  • Save anonymous/004ca57ee5a49f9684bb to your computer and use it in GitHub Desktop.
Save anonymous/004ca57ee5a49f9684bb to your computer and use it in GitHub Desktop.
class Ability
include CanCan::Ability
def initialize(user, *args)
super(*args)
alias_action :new, to: :write
alias_action :create, to: :publish
user ||= User.new # guest user (not logged in)
if user.has_role? :admin
can :manage, :all
else
user.has_role? :user
can :manage, User, user_id: user.id
#As a user you can read a widget which you have permissions for..
can :read, Widget, widget_id: user.permissions.widget_id
can :manage, :all
end
end
undefined method `widget_id' for #<ActiveRecord::Relation:0x00000004e646b0>
=> can :read, Widget, widget_id: user.permissions.widget_id
=begin
Associations are as followed:
User- has_many :widgets, through: :permissions
Permissions - belongs_to :widget
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment