From b1e13758d3d1c127b4153460b1ec04e70d1ebcd1 Mon Sep 17 00:00:00 2001 From: p31729568 Date: Tue, 2 Jul 2019 13:53:00 +0800 Subject: [PATCH] add school and department api for option --- app/controllers/departments_controller.rb | 11 +++++++++++ app/controllers/schools_controller.rb | 4 ++++ app/models/department.rb | 22 +++++++++++++++++++++ app/models/school.rb | 24 +++++++++++++++++++++++ config/routes.rb | 6 +++++- 5 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 app/controllers/departments_controller.rb diff --git a/app/controllers/departments_controller.rb b/app/controllers/departments_controller.rb new file mode 100644 index 000000000..f0fa8fe2c --- /dev/null +++ b/app/controllers/departments_controller.rb @@ -0,0 +1,11 @@ +class DepartmentsController < ApplicationController + def for_option + render_ok(departments: Department.cached_names_data(current_school)) + end + + private + + def current_school + @_current_school ||= School.find(params[:id]) + end +end \ No newline at end of file diff --git a/app/controllers/schools_controller.rb b/app/controllers/schools_controller.rb index 11932be42..ea94f4c69 100644 --- a/app/controllers/schools_controller.rb +++ b/app/controllers/schools_controller.rb @@ -9,4 +9,8 @@ class SchoolsController < ApplicationController render_ok(school_names: schools.pluck(:name)) end + + def for_option + render_ok(schools: School.cached_names_data) + end end diff --git a/app/models/department.rb b/app/models/department.rb index 15a8a7c1b..bf5c3a90e 100644 --- a/app/models/department.rb +++ b/app/models/department.rb @@ -2,4 +2,26 @@ class Department < ApplicationRecord belongs_to :school has_many :department_members, dependent: :destroy + + after_create_commit :reset_data_cache + after_update_commit :reset_data_cache + + def self.cached_names_data(school) + Rails.cache.fetch(names_data_cache_key(school.id), expires_in: 7.days) do + school.departments.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(school_id)) + end + end + + def self.names_data_cache_key(school_id) + "schools/#{school_id}/department_names_data" + end end diff --git a/app/models/school.rb b/app/models/school.rb index af04ed0c7..983a93735 100644 --- a/app/models/school.rb +++ b/app/models/school.rb @@ -1,4 +1,6 @@ class School < ApplicationRecord + has_many :departments, dependent: :destroy + has_many :shixun_schools, :dependent => :destroy has_many :shixuns, :through => :shixun_schools @@ -8,6 +10,9 @@ class School < ApplicationRecord 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) @@ -28,4 +33,23 @@ class School < ApplicationRecord 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 diff --git a/config/routes.rb b/config/routes.rb index 0f47cda76..a27dde1f3 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -576,9 +576,13 @@ Rails.application.routes.draw do resources :schools do member do + resources :departments, only: [] do + get :for_option, on: :collection + end end collection do - get "school_list" + get :school_list + get :for_option end scope module: :ecs do