ruby on rails - CanCan and ability block -
i have piece of code defines cancan's user permissions, don't understand conditions inside block ?
class ability include cancan::ability def initialize(user) user.permissions.each |permission| can permission.action.to_sym, permission.thing_type.constantize |thing| thing.nil? || permission.thing_id.nil? || permission.thing_id == thing.id end end end end
so, lines of code bugging me:
permission.thing_type.constantize |thing| thing.nil? || permission.thing_id.nil? || permission.thing_id == thing.id end
i understand block used define complex conditions , guess permission.thing_id == thing.id
there permission granted selected objects don't see purpoose of thing.nil? || permission.thing_id.nil?
.what's for?
so, according cancan docs, when pass block can
(which what's going on here), permission given if block returns true. object passed in block object user might have permission to.
what does, then, give user permission perform permission.action
on class permission.thing_type
if:
thing
nil. might happen in case of thingscreate
permission, doesn't take specific object (it takes class instead).permission.thing_id
nil. dealing casepermission
object doesn't point @ specific thing. feels opposite side of item 1. might apply in case of 'blanket' permissions - permission says particular user can perform action on any object of class. or- the
thing
has id specified in permission object.
moral: comment code. if it's obvious right does, might not next person. might you, in few months.
Comments
Post a Comment