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.
educoder/app/controllers/ecs/base_controller.rb

61 lines
1.6 KiB

6 years ago
class Ecs::BaseController < ApplicationController
# model validation error
rescue_from ActiveRecord::RecordInvalid do |ex|
render_error(ex.record.errors.full_messages.join(','))
end
# form validation error
rescue_from ActiveModel::ValidationError do |ex|
render_error(ex.model.errors.full_messages.join(','))
end
before_action :require_login
before_action :check_user_permission!
helper_method :current_user, :current_school
private
# --- 每个子类controller可能有不同的实现查看时需要注意 ---
def current_year
@_current_year ||= EcYear.find(params[:ec_year_id])
end
def current_major_school
@_current_major_school ||= current_year.ec_major_school
end
def current_school
@_current_school ||= current_major_school.school
end
def major_or_course_manager?
!current_user.admin? && !current_school.manager?(current_user)
end
def check_user_permission!
return if current_user.admin? || current_school.manage_permission?(current_user)
render_forbidden
end
def check_manager_permission!
return if current_user.admin? || current_school.manager?(current_user)
render_forbidden
end
def check_major_manager_permission!
return if current_user.admin? || current_school.manager?(current_user)
return if current_major_school.manager?(current_user)
render_forbidden
end
def paginate(objs)
page = params[:page].to_i <= 0 ? 1 : params[:page].to_i
per_page = params[:per_page].to_i > 0 ? params[:per_page].to_i : 20
Kaminari.paginate_array(objs).page(page).per(per_page)
end
end