2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Rails6での before_render

Posted at

render メソッドを、run_callbacks で包むだけのモンキーパッチ

# frozen_string_literal: true

module BeforeRender
  extend ActiveSupport::Concern

  included do
    define_callbacks :render
    prepend RenderPatch
  end

  module RenderPatch
    def render(*options, &block)
      run_callbacks(:render) do
        super(*options, &block)
      end
    end
  end

  class_methods do
    def before_render(*names, &block)
      _insert_callbacks(names, block) do |name, options|
        set_callback(:render, :before, name, options)
      end
    end
  end
end
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?