Я создал собственный драгоценный камень для управления записями под названием «record_search», который включает в себя контроллер с именем «ApiController». В одном приложении мне нужно было подключить поведение авторизации, чтобы данные не были общедоступными, поэтому я добавил ActiveSupport:: Модуль Concern, позволяющий приложению добавлять файл before_filter. Как я могу правильно протестировать этот фильтр before_filter? (используя rspec)
В драгоценном камне:
приложение/контроллеры/api_controller.rb
module RecordSearch
class ApiController < ApplicationController
respond_to :json
def search
render json: #app-specific code
end
def find
render json: #app-specific code
end
include Extensions #define any authorization logic here
end
end
Локальное приложение:
приложение/контроллеры/концерны/record_search/extensions.rb
module RecordSearch::Extensions
extend ActiveSupport::Concern
include ApplicationHelper #defines current_user method
included do
before_filter :require_premium_user, :only => [:find,:search]
end
private
def require_premium_user
unless current_user
return render :json => {"error" => "not authorized"}
end
end
end