Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@sinsoku
Created October 8, 2020 01:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sinsoku/67dc7144ae1fe5dbacfb0390e565e659 to your computer and use it in GitHub Desktop.
Save sinsoku/67dc7144ae1fe5dbacfb0390e565e659 to your computer and use it in GitHub Desktop.
before/after/around actionsの前後に処理を挟むパッチ
class ApplicationController < ActionController::Base
class << self
[:before, :after, :around].each do |callback|
define_method "#{callback}_action" do |*names, &blk|
options = names.extract_options!
new_blk = -> {
# 本来の処理の前に行う処理を書く。
if blk.is_a?(Proc)
blk.call
else
names.each { |n| send(n) }
end
# 本来の処理の後に行う処理を書く。
}
super(**options, &new_blk)
end
define_method "prepend_#{callback}_action" do |*names, &blk|
# 略
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment