class Users::BaseController < ApplicationController before_action :check_observed_user_exists! helper_method :observed_logged_user?, :observed_user def observed_user @_observed_user ||= (User.find_by_id(params[:user_id]) || User.find_by_login(params[:user_id])) end def observed_logged_user? observed_user.id == User.current&.id end private def check_observed_user_exists! return if observed_user.present? render_not_found end def private_user_resources! require_login return if current_user.admin? || observed_logged_user? render_forbidden end def paginate(objs, **opts) 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 return Kaminari.paginate_array(objs).page(page).per(per_page) unless observed_logged_user? && opts[:special] # note: 为实现第一页少一条记录,让前端放置新建入口 if page == 1 objs.limit(per_page - 1) else objs.limit(per_page).offset((page - 2) * per_page + per_page - 1) end end end