Created
October 8, 2020 01:50
-
-
Save sinsoku/67dc7144ae1fe5dbacfb0390e565e659 to your computer and use it in GitHub Desktop.
before/after/around actionsの前後に処理を挟むパッチ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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