Skip to content

Instantly share code, notes, and snippets.

@j-manu
Created January 8, 2012 20:58
Show Gist options
  • Save j-manu/1579673 to your computer and use it in GitHub Desktop.
Save j-manu/1579673 to your computer and use it in GitHub Desktop.
# 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