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:

  1. thing nil. might happen in case of things create permission, doesn't take specific object (it takes class instead).
  2. permission.thing_id nil. dealing case permission 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
  3. 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

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -