You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.5 KiB
56 lines
1.5 KiB
class School < ApplicationRecord
|
|
has_many :departments, dependent: :destroy
|
|
|
|
has_many :shixun_schools, :dependent => :destroy
|
|
has_many :shixuns, :through => :shixun_schools
|
|
|
|
has_many :ec_school_users, :dependent => :destroy
|
|
has_many :users, :through => :ec_school_users
|
|
|
|
has_many :ec_major_schools, :dependent => :destroy
|
|
has_many :ec_majors, :through => :ec_major_schools
|
|
|
|
after_create_commit :reset_data_cache
|
|
after_update_commit :reset_data_cache
|
|
|
|
# 学校管理员
|
|
def manager?(user)
|
|
ec_school_users.exists?(user_id: user.id)
|
|
end
|
|
|
|
# 专业管理员
|
|
def major_manager?(user)
|
|
relations = ec_major_schools.not_template.joins(:ec_major_school_users)
|
|
relations.exists?(ec_major_school_users: { user_id: user.id })
|
|
end
|
|
|
|
# 课程管理员
|
|
def course_manager?(user)
|
|
relations = ec_major_schools.not_template.joins(ec_years: :ec_course_users)
|
|
relations.exists?(ec_course_users: { user_id: user.id })
|
|
end
|
|
|
|
def manage_permission?(user)
|
|
manager?(user) || major_manager?(user) || course_manager?(user)
|
|
end
|
|
|
|
def self.cached_names_data
|
|
Rails.cache.fetch(names_data_cache_key, expires_in: 7.days) do
|
|
School.select(:id, :name).as_json
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def reset_data_cache
|
|
# 清除学校名称缓存
|
|
if new_record? || name_previously_changed?
|
|
Rails.cache.delete(self.class.names_data_cache_key)
|
|
end
|
|
end
|
|
|
|
def self.names_data_cache_key
|
|
'schools/names_data'
|
|
end
|
|
end
|