brentd / xray-rails

☠️ A development tool that reveals your UI's bones
MIT License
1.22k stars 79 forks source link

xray-rails 0.3.2 and sprockets 4 #113

Open rocket-turtle opened 3 years ago

rocket-turtle commented 3 years ago

We had some trouble to upgrade to sprockets 4 so I want to share our solution.

Include this MP in your application.rb (xray-rails must allready be required)

      config.after_initialize do
        if Gem::Version.new(Xray::VERSION) > Gem::Version.new('0.3.2')
          raise('This monkey patch is no longer necessary.')
        end

        Xray::Engine::JavascriptPreprocessor.class_eval do
          # See also https://github.com/brentd/xray-rails/issues/88
          # This is fixed in https://github.com/brentd/xray-rails/pull/103
          def self.run(filename, source, context)
            path = Pathname.new(context.filename).to_s
            if path =~ /^#{Rails.root}.+\.(jst)(\.|$)/
              Xray.augment_template(source, path)
            else
              source
            end
          end
        end
      end

This will no longer be necessary with the next release of xray-rails. You could include

Add = javascript_include_tag 'xray', nonce: true if Rails.env.development? as last element of the head tag. You need to make sure jQuery is already available.

Add = render partial: '/xray_bar' if Rails.env.development? as first element of the body tag. This will no longer be necessary if this MR is merged https://github.com/brentd/xray-rails/pull/101

Please let me know if you made some different changes.