1

我有移动设备的会话控制器。我想对其进行一些重构,例如因为我的会话控制器是Devise::SessionsController我必须从BaseController.

class Api::V1::SessionsController < Devise::SessionsController
  include Devise::Controllers::Helpers
  prepend_before_filter :require_no_authentication, only: [:create]
  skip_load_and_authorize_resource
  before_filter :ensure_params_exist, only: [:create]

  respond_to :json

  def create
    if user_find params[:user][:member_id]
      self.resource = warden.authenticate!(auth_options)
      sign_in(resource_name, resource)
      resource.reset_authentication_token!
      resource.save!
    else
      error :not_found
    end
  end

  def destroy
    sign_out(resource_name)
  end

  private

  def user_find(id)
    User.find_by member_id: id
  end

  def ensure_params_exist
    unless params[:user]
      error :bad_request
    end
  end

  def error type, context=nil
    render json: { message: error_message(type, context) }, status: type
  end

  def error_message type, context
    scope = [:api, :error, self.controller_name.to_sym]
    scope.push context if context
    t(type, scope: scope)
  end

end

class Api::V1::BaseController < ApplicationController
  load_and_authorize_resource

  respond_to :json

  rescue_from CanCan::AccessDenied do |exception|
    render_status :unauthorized
  end

  private

  def error type, context=nil
    render json: {message: error_message(type, context)}, status: type
  end

  def error_message type, context
    scope = [:api, :error, self.controller_name.to_sym]
    scope.push context if context
    t(type, scope: scope)
  end

  def render_status state
    render status: state, json: {}
  end
end
4

1 回答 1

1

我建议将常用方法提取到模块中并将其包含在内。

module CommonMethods
  def self.included(controller)
    controller.send :error_message, :error
  end

  def error type, context=nil
    render json: {message: error_message(type, context)}, status: type
  end

  def error_message type, context
    scope = [:api, :error, self.controller_name.to_sym]
    scope.push context if context
    t(type, scope: scope)
  end
end

只需将其包含include CommonMethods在您的控制器中

于 2013-07-09T12:02:02.610 回答