Sha256: f78c0c1ba892bc57dd363265397d6c30a0c3393ec005b623448d41735d4152a5

Contents?: true

Size: 1.61 KB

Versions: 3

Compression:

Stored size: 1.61 KB

Contents

module EducodeSales
  class ApplicationController < ActionController::Base
    protect_from_forgery with: :exception
    before_action :authenticate_request

    def render_success
      render json: { success: true }
    end

    def render_failure(msg)
      render json: { success: false, msg: msg.is_a?(String) ? msg : msg.errors.full_messages.join(",") }, status: 403
    end

    def current_user
      @current_admin ||= Staff.find_by_id(session[:admin_id])
    end

    def authenticate_request
      if current_user
        current_user.check_login_status(request)
      else
        redirect_to login_path
      end
    end

    def authenticate_admin
      unless current_user&.is_admin
        redirect_to no_permission_path
      end
    end

    def filter
      params[:check] = params[:check] == "true" ? 0:1
      begin
        filter = Filter.find_or_create_by!(staff_id: @current_admin.id, clazz: params[:type])
        filter.extras["#{params[:name]}"] = params[:check]
        filter.save!
        render json: {success: true ,hidden: params[:check]}
      rescue => e
        render_failure("操作失败")
      end
    end

    def paginate(relation)
      limit = params[:limit] || params[:per_page]
      limit  = (limit.to_i.zero? || limit.to_i > 100) ? 20 : limit.to_i
      page   = params[:page].to_i.zero? ? 1 : params[:page].to_i
      offset = (page - 1) * limit

      if relation.is_a?(Array)
        relation[offset, limit]
      else
        relation.limit(limit).offset(offset)
      end
    end



    rescue_from CanCan::AccessDenied do |exception|
      redirect_to no_permission_path
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
educode_sales-1.10.48 app/controllers/educode_sales/application_controller.rb
educode_sales-1.10.46 app/controllers/educode_sales/application_controller.rb
educode_sales-1.10.41 app/controllers/educode_sales/application_controller.rb