Skip to content

Instantly share code, notes, and snippets.

@brandon-beacher
Created November 21, 2013 21:58
Show Gist options
  • Save brandon-beacher/7590478 to your computer and use it in GitHub Desktop.
Save brandon-beacher/7590478 to your computer and use it in GitHub Desktop.
class User < ActiveRecord::Base
has_many :administered_customers, through: :customer_administrations, source: :customer
has_many :administered_districts, through: :district_administrations, source: :district
has_many :administered_schools, through: :school_administrations, source: :school
has_many :assigned_schools, through: :school_assignments, source: :school
has_many :classroom_teachings
has_many :customer_administrations
has_many :school_administrations
has_many :district_administrations
has_many :administered_district_schools, through: :administered_districts, source: :schools
has_many :school_assignments
has_many :taught_classrooms, through: :classroom_teachings, source: :classroom
has_many :taught_schools, through: :taught_classrooms, source: :school
def administered_customer
administered_customers.first
end
def administered_customer_ids
customer_administrations.pluck(:customer_id)
end
def administered_school
administered_schools.first
end
def administered_school_ids
school_administrations.pluck(:school_id)
end
def administered_schools_customer_ids
administered_schools.pluck(:customer_id)
end
def administered_district
administered_districts.first
end
def administered_district_ids
district_administrations.pluck(:district_id)
end
def administered_districts_customer_ids
administered_districts.pluck(:customer_id)
end
def affiliated_customer
Customer.find(affiliated_customer_id)
end
def affiliated_customer_id
affiliated_customer_ids.first
end
def affiliated_customer_ids
administered_customer_ids +
administered_districts_customer_ids +
administered_schools_customer_ids +
taught_schools_customer_ids +
assigned_schools_customer_ids
end
def affiliated_schools
effective_administered_schools +
assigned_schools
end
def affiliated_school
affiliated_schools.first
end
def affiliated_school_ids
administered_school_ids +
assigned_school_ids +
taught_classrooms_school_ids
end
def assigned_school_ids
school_assignments.pluck(:school_id)
end
def assigned_school
assigned_schools.first
end
def teaches_class?(classroom_id)
classroom_teachings.where(classroom_id: classroom_id).present?
end
def classroom_ids
taught_classrooms.pluck(:classroom_id)
end
def customer_admin?
administered_customers.present?
end
def any_admin?
customer_admin? || school_admin? || district_admin?
end
def effective_administered_customers
if ability.can? :read, Customer
Customer.scoped
else
administered_customers
end
end
def effective_administered_districts
if superuser?
District.scoped
elsif customer_admin?
administered_customer.districts
else
administered_districts
end
end
def effective_administered_schools
if superuser?
School.scoped
elsif customer_admin?
administered_customer.schools
elsif district_admin?
administered_district.schools
else
administered_schools
end
end
def effective_affiliated_schools
if teacher?
assigned_schools
else
effective_administered_schools
end
end
def effective_taught_classrooms
if superuser?
Classroom.scoped
elsif customer_admin?
administered_customer.classrooms
elsif district_admin?
administered_district.classrooms
elsif school_admin?
administered_school.classrooms
else
taught_classrooms
end
end
def school_admin?
administered_schools.present?
end
def district_admin?
administered_districts.present?
end
def taught_classroom
taught_classrooms.first
end
def taught_classrooms_school_ids
taught_classrooms.pluck(:school_id)
end
def taught_schools_customer_ids
taught_schools.pluck(:customer_id)
end
def assigned_schools_customer_ids
assigned_schools.pluck(:customer_id)
end
def teacher?
assigned_schools.present? || taught_classrooms.present?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment