Created
September 4, 2019 11:37
-
-
Save znz/755b1caefce2664cc2d0d80fdd759930 to your computer and use it in GitHub Desktop.
最低限の before_action っぽいもの
This file contains hidden or 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 Foo | |
def self.before_action(hook, only:) | |
actions = Array(only) | |
self.prepend Module.new { | |
actions.each do |action| | |
class_eval <<-RUBY, __FILE__, __LINE__+1 | |
def #{action} | |
#{hook} | |
super | |
end | |
RUBY | |
end | |
} | |
end | |
before_action :foo, only: :show | |
before_action :hook, only: %i[show save] | |
def save | |
p :save | |
end | |
def show | |
p :show | |
end | |
def foo | |
p :foo | |
end | |
def hook | |
p :hook | |
end | |
end | |
Foo.new.show # => :hook, :foo, :show | |
Foo.new.save # => :hook, :save |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment