Global before and after filters in Padrino
December 5, 2013Adding before and after filters in Padrino is very easy. According to the Padrino documentation you add before and after filters by adding a block named before
at the top of your controller.
# Before filter in Padrino
before do
#insert code or / and function calls here
end
In Padrino the before and after filters are scoped to the controller they are added in. The documentation does not say anything about global filters. After thinking about this for a bit I tried to add before filters to the app.rb
file that is located in the root of your app
folder. Here is an example of how the app.rb
file looks.
module ProjectName
class App < Padrino::Application
register SassInitializer
use ActiveRecord::ConnectionAdapters::ConnectionManagement
register Padrino::Rendering
register Padrino::Mailer
register Padrino::Helpers
enable :sessions
# Before filter
before do
# Code, function and helper calls goes here
end
# After filter
after do
# Code, function and helper calls goes here
end
end
end
I’m not sure if this is the correct or best way of adding global before and after filters in Padrino, but it works just fine and I haven’t seen any side effects of it.