Last active
February 6, 2017 12:07
-
-
Save shakemurasan/c0b0d1bb6c5ebd80bca437a824b1e3ab to your computer and use it in GitHub Desktop.
Railsでviewがレンダリングされた際、標準出力にそのファイル名を出力するコード
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 RenderObserver < Rails::Railtie | |
@rendered = Set.new([]) | |
config.after_initialize do | |
ActiveSupport::Notifications.subscribe "render_template.action_view" do |*args| | |
data = args.extract_options! | |
unless @rendered.include?(data[:identifier]) | |
puts "#{data[:identifier]}" | |
@rendered.add(data[:identifier]) | |
end | |
end | |
ActiveSupport::Notifications.subscribe "render_partial.action_view" do |*args| | |
data = args.extract_options! | |
unless @rendered.include?(data[:identifier]) | |
puts "#{data[:identifier]}" | |
@rendered.add(data[:identifier]) | |
end | |
end | |
end | |
end |
このコードをapplication.rb
とかに埋め込めば、とりあえずはすぐに動きます。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@rendered
にレンダリング済みのview名をぶち込んでおくことで、二回目以降にunless
句の中が実行されないようにしている。