brentd / xray-rails

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

Need ability to disable Xray entirely for a given controller action #75

Open jpegjames opened 8 years ago

jpegjames commented 8 years ago

Maybe, I'm missing something, but I have view that I need to disable xray on. In the controller, I've passed render xray: false, and it does prevent it from rendering.

I've tried a bunch of combinations. This particular controller action has no other functionality. Here's everything that I've tried:

def show
  render xray: false 
end
def show
  render 'show', xray: false 
end
def show
  respond_to do |format|
    format.html{ render 'show', xray: false }
  end
end

Any thoughts? Is this only not working for me? I've tried it on several different actions and controllers, but it initiates every time.


I'm running Rails 3.2.21 and I just updated to xray-rails 0.1.21. I am also using Turbolinks, but have tried by refreshing the page each time and it still stays enabled.

mattbrictson commented 8 years ago

Hi, thanks for the report!

To be honest, I've never used the xray: false feature and this is the first time I've been made aware of it (I currently maintain the gem but am not the original author).

It is quite possible that this feature is broken, either in general or for Rails 3.2 specifically. I would appreciate any additional investigation you could provide on this.

mattbrictson commented 8 years ago

Just to be clear: my expectation of this feature based on the README is that passing xray: false should still render the view. The only difference should be that the special xray HTML comments will be omitted. Thus when you activate xray with the keyboard shortcut, that particular view will not be highlighted.

Intuitively this makes sense for a partial render, but it is not entirely clear to me what is supposed to happen if you pass xray: false for the entire view rendered by the controller. Should it completely disable xray?

What is your expectation?

jpegjames commented 8 years ago

@mattbrictson: Thanks for your response.

I was expecting to pass xray: false to not send down the xray related code in the the HTML views (in other words, command+shift+x should do nothing) but yes, the view should still work as normal. We have one JavaScript plugin that fails to work with xray (so when working in dev on that section, we have to remove x-ray from the Gemfile and restart the server).

Reading through the code, I found this line in engine.rb that seems to handle this option, but I don't see anywhere where render_with_xray is called.

I also found this test.

I'm not too familiar with the inner-workings of xray, so where is the x-ray html interjected into the view? Is it the render_with_xray method or something else?

(Tagging one of my other developers on this: @lebrauncom)

mattbrictson commented 8 years ago

So, my understanding is that xray-rails does two different things wrt HTML injection:

  1. For every HTTP response, it adds some global markup (e.g. the xray toolbar) and a <script> element to load the necessary xray JavaScript. This happens regardless of whether or not you specify xray: false. The code for it is in middleware.rb.
  2. For each view template rendered as part of the response, xray surrounds the rendered view with special HTML comments. These comments tell the xray JavaScript where partials begin and end in the DOM so that it can draw bounding boxes and link to the source files when you press command+shift+x. These injected HTML comments are what you can disable with xray: false (at least, this is my assumption).

Does this help shed some light on what is happening?

To my knowledge there is no way to entirely disable the xray middleware for a given text/html response, so there will always be some xray markup and JavaScript injected.

jpegjames commented 8 years ago

For every HTTP response, it adds some global markup (e.g. the xray toolbar) and a <script> element to load the necessary xray JavaScript. This happens regardless of whether or not you specify xray: false. The code for it is in middleware.rb.

Yes, this is happening whether or not xray: false is passed.

For each view template rendered as part of the response, xray surrounds the rendered view with special HTML comments. These comments tell the xray JavaScript where partials begin and end in the DOM so that it can draw bounding boxes and link to the source files when you press command+shift+x. These injected HTML comments are what you can disable with xray: false (at least, this is my assumption).

What you describe, might be OK but the xray comments are still being passed into the HTML when xray: false. I've rendered the same page xray: false and xray: true and both times the same number of xray comments were in the raw HTML code command+shift+x responded the same way in both browser windows.

jpegjames commented 8 years ago

@mattbrictson: Follow up question: Where does X-Ray inject the HTML comments? The middleware.rb reference above seems to just be for the x-ray bar and assets.

mattbrictson commented 8 years ago

Comments are injected using Xray.augment_template, which is defined here:

https://github.com/brentd/xray-rails/blob/4c050c7a300db3bf750a4961a78d1c0c61c2003c/lib/xray-rails.rb#L81-L93

This happens whenever a view template is rendered, which is accomplished by a monkey patch registered by engine.rb, here:

https://github.com/brentd/xray-rails/blob/48ae158da4a4742f743c65efe82bd7c4eb555c49/lib/xray/engine.rb#L31-L53

EleanorRagone commented 7 years ago

Was there any resolution to this problem? I'm encountering it right now

sildur commented 6 years ago

@jpegjames @pcragone the solution is to use the locals parameter, like this:

render locals: {xray: false} 

As far as I can tell, you use xray: false when rendering a partial:

render partial: 'foo', xray: false

and use the locals parameter otherwhise.

mattbrictson commented 6 years ago

To reiterate: the xray: false option is for disabling Xray for a specific partial template. There is no way to disable Xray for an entire response. I will update the README to clarify this.

mattbrictson commented 6 years ago

I'm re-classifying this issue as a feature request: namely, the ability to disable Xray entirely for a given controller action.

jpegjames commented 6 years ago

@mattbrictson: Thanks for updating the README. I think that caused some confusion as it seemed like it could be disabled completely for a given page/request.

I would personally love to see that feature, but we make heavy use of partials so this may still be a suitable workaround.

stiak commented 5 years ago

I would love to see this available. We have modal JS library that expects one root element that is broken by x-ray instrumentation.