From d6639bedb742c18820aa99aacc37fc5aa1f6b228 Mon Sep 17 00:00:00 2001 From: Steve Tuckner Date: Wed, 7 Dec 2016 10:12:03 -0600 Subject: [PATCH] Add before_index proc so that it can be called every time index is hit --- .../grape_swagger_rails/application_controller.rb | 6 ++++++ lib/grape-swagger-rails.rb | 9 +++++++++ 2 files changed, 15 insertions(+) diff --git a/app/controllers/grape_swagger_rails/application_controller.rb b/app/controllers/grape_swagger_rails/application_controller.rb index 6bcd837..6b88797 100644 --- a/app/controllers/grape_swagger_rails/application_controller.rb +++ b/app/controllers/grape_swagger_rails/application_controller.rb @@ -7,6 +7,7 @@ class ApplicationController < ActionController::Base end def index + run_before_index end private @@ -15,5 +16,10 @@ def run_before_action return unless GrapeSwaggerRails.options.before_action instance_exec(request, &GrapeSwaggerRails.options.before_action) end + + def run_before_index + return unless GrapeSwaggerRails.options.before_index + instance_exec(request, &GrapeSwaggerRails.options.before_index) + end end end diff --git a/lib/grape-swagger-rails.rb b/lib/grape-swagger-rails.rb index f8e0294..0fe9739 100644 --- a/lib/grape-swagger-rails.rb +++ b/lib/grape-swagger-rails.rb @@ -15,6 +15,14 @@ def before_action(&block) before_action_proc end end + + def before_index(&block) + if block_given? + self.before_index_proc = block + else + before_index_proc + end + end end mattr_accessor :options @@ -36,6 +44,7 @@ def before_action(&block) supported_submit_methods: %w(get post put delete patch), before_action_proc: nil, # Proc used as a controller before action + before_index_proc: nil, # Proc used as a controller before every index call hide_url_input: false, hide_api_key_input: false