Skip to content

Instantly share code, notes, and snippets.

@shakemurasan
Last active February 6, 2017 12:07
Show Gist options
  • Save shakemurasan/c0b0d1bb6c5ebd80bca437a824b1e3ab to your computer and use it in GitHub Desktop.
Save shakemurasan/c0b0d1bb6c5ebd80bca437a824b1e3ab to your computer and use it in GitHub Desktop.
Railsでviewがレンダリングされた際、標準出力にそのファイル名を出力するコード
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
@shakemurasan
Copy link
Author

@rendered にレンダリング済みのview名をぶち込んでおくことで、二回目以降にunless句の中が実行されないようにしている。

@shakemurasan
Copy link
Author

このコードをapplication.rbとかに埋め込めば、とりあえずはすぐに動きます。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment