0
我正在乾燥API。一切都已經準備好,但我注意到,二十行左右的每個類幾乎是一樣的:幹掉Rails回調
class V1::FooController < V1::BaseController
# documentation
swagger_controller :Foo, "Foo"
# authorization
before_filter :allow_session_user
before_filter :allow_api_key_user, only: [:sample, :index]
before_filter :check_access
# inherit crud endpoints
inherit_resources
actions :all, :except => [ :index, :show ]
# other
before_filter :private_foo_method
respond_to :json
#...other stuff...#
end
我想變幹這些了,但問題是我需要運行在這些回調當前班級的上下文(而不是超級班級)。對於before_filters
我知道你通常可以將這些移動到V1::BaseController
,但在我的情況下,它會導致執行順序的問題,因爲我有其他before_filters
在V1::BaseController
。
class V1::BaseController < ApplicationController
#this needs to execute in the context of the class that called it and NOT V1::BaseController
def super_init(documentation_name, allowed_api_endpoints, except_inherited_crud)
swagger_controller documentation_name, documentation_name
inherit_resources
# authorization
before_filter :allow_session_user
before_filter :allow_api_key_user, only: allowed_api_endpoints
before_filter :check_access
# inherit crud endpoints
inherit_resources
actions :all, :except => except_inherited_crud
# other
respond_to :json
end
end
class V1::FooController < V1::BaseController
before_filter :init
before_filter :private_foo_method
def init
# super_init should run in the context of V1::FooController
super.super_init("Foo", [:sample, :index], [ :index, :show ])
end
#...other stuff...#
end
我試圖self
傳遞給super_init
,並把所有的邏輯class << passed_context
內,但沒有奏效。我可以列出我嘗試過的東西,但我不確定這對任何人有多大幫助...
如果您需要更多信息,請詢問。我有一種感覺,這是一些更高層次的抽象,我以前從來沒有必須在Rails中做過。
謝謝!
我們是在一箇舊版本的Rails,所以我們一直在使用的過濾器。 –
好的,你可以同時使用'prepend_before_filter'。 – rebagliatte
@ party_hat_24有這個工作嗎? – rebagliatte