Created
January 8, 2012 20:58
-
-
Save j-manu/1579673 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
# Let's assume these models Department, Subject, User, Allotment, Test | |
# Allotment table has department_id, subject_id, user_id | |
# Test table has department_id, :subject_id | |
# Problem: allow the user to edit a test if he is alloted the subject | |
# for that department OR if he is special (dean) OR if the number of allotments | |
# of that user in that department exceeds 3 | |
# | |
class Ability | |
include CanCan::Ability | |
def initialize(user) | |
can :edit, Test do |test| | |
department = test.department | |
user_allotments = department.allotments.select {|a| a.user_id == user.id} | |
department.deans.include?(user) || user_allotments.size > 3 || | |
user_allotments.select {|a| a.subject_id == test.subject_id}.present? | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment