-
-
Save tbuehlmann/c9f799c28d9f7b384a77 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 do |widget| | |
user.permissions.where(widget_id: widget.id).exists? | |
end | |
can :manage, :all | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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