lib/xray/engine.rb in xray-rails-0.1.23 vs lib/xray/engine.rb in xray-rails-0.2.0

- old
+ new

@@ -8,28 +8,16 @@ class Engine < ::Rails::Engine initializer "xray.initialize" do |app| app.middleware.use Xray::Middleware # Required by Rails 4.1 - app.config.assets.precompile += %w(xray.js xray-backbone.js xray.css) + app.config.assets.precompile += %w(xray.js xray.css) end config.after_initialize do |app| ensure_asset_pipeline_enabled! app - # Register as a Sprockets processor to augment JS files, including - # compiled coffeescript, with filepath information. See - # `Xray.augment_js` for details. - app.assets.register_postprocessor 'application/javascript', :xray do |context, data| - path = context.pathname.to_s - if path =~ /^#{app.root}.+\.(js|coffee)(\.|$)/ - Xray.augment_js(data, path) - else - data - end - end - # Monkey patch ActionView::Template to augment server-side templates # with filepath information. See `Xray.augment_template` for details. ActionView::Template.class_eval do def render_with_xray(*args, &block) path = identifier @@ -50,18 +38,42 @@ end end alias_method_chain :render, :xray end - # Augment JS templates - app.assets.register_preprocessor 'application/javascript', :xray do |context, source| - path = context.pathname.to_s - if path =~ /^#{app.root}.+\.(jst)(\.|$)/ - Xray.augment_template(source, path) - else - source + # Sprockets preprocessor interface which supports all versions of Sprockets. + # See: https://github.com/rails/sprockets/blob/master/guides/extending_sprockets.md#supporting-all-versions-of-sprockets-in-processors + class JavascriptPreprocessor + def initialize(filename, &block) + @filename = filename + @source = block.call end + + def render(context, empty_hash_wtf) + self.class.run(@filename, @source, context) + end + + def self.run(filename, source, context) + path = context.pathname.to_s + if path =~ /^#{Rails.root}.+\.(jst)(\.|$)/ + Xray.augment_template(source, path) + else + source + end + end + + def self.call(input) + filename = input[:filename] + source = input[:data] + context = input[:environment].context_class.new(input) + + result = run(filename, source, context) + context.metadata.merge(data: result) + end end + + # Augment JS templates + app.assets.register_preprocessor 'application/javascript', JavascriptPreprocessor # This event is called near the beginning of a request cycle. We use it to # collect information about the controller and action that is responding, for # display in the Xray bar. ActiveSupport::Notifications.subscribe('start_processing.action_controller') do |*args|