How do I before_filter a Module::Class::method?
I have a custom Module on my Rails project, and I want it to be a before_f开发者_运维知识库ilter, is there a way I can do?
before_filter TokenGenerator::Generator.generate_token("mytoken")
You can use a block as a before_filter
:
before_filter do |controller|
TokenGenerator::Generator.generate_token("mytoken")
end
Or you could create a separate class:
class TokenFilter
def self.filter(controller)
TokenGenerator::Generator.generate_token("mytoken")
end
end
#...
before_filter TokenFilter
Or the usual method:
before_filter :token_generator
#...
private
def token_generator
TokenGenerator::Generator.generate_token("mytoken")
end
You could create a delegator method to generate_token, then use the delegator method with the before filter...
before_filter :generate_token
def generate_token
TokenGenerator::Generator.generate_token("mytoken")
end
精彩评论