Skip to content

Instantly share code, notes, and snippets.

@nthj
Created April 1, 2011 19:32
Show Gist options
  • Save nthj/898717 to your computer and use it in GitHub Desktop.
Save nthj/898717 to your computer and use it in GitHub Desktop.
Rails controller before/after shortcuts (ruby 1.9.2 compatible)
class ApplicationController < ActionController::Base
extend Filterable
after_update -> { user.clear_cache! }
end
module Filterable
[:after, :before].each do |context|
[:index, :show, :new, :create, :update, :destroy].each do |action|
define_method "#{context}_#{action}", ->(filter, options = { }, &block) do
send "#{context}_filter", filter, options.merge!(:only => action), &block
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment