Class: Upjs::Rails::Inspector
- Inherits:
-
Object
- Object
- Upjs::Rails::Inspector
- Defined in:
- lib/upjs/rails/inspector.rb
Overview
This object allows the server to inspect the current request for Up.js-related concerns such as "is this a page fragment update?".
Available through the #up
method in all controllers, helpers and views.
Instance Method Summary collapse
-
#initialize(controller) ⇒ Inspector
constructor
A new instance of Inspector.
-
#target ⇒ Object
If the current request is a fragment update, this returns the CSS selector of the page fragment that should be updated.
-
#title=(new_title) ⇒ Object
Forces Up.js to use the given string as the document title when processing this response.
-
#up? ⇒ Boolean
Returns whether the current request is an page fragment update triggered by an Up.js frontend.
-
#validate? ⇒ Boolean
Returns whether the current form submission should be validated (and not be saved to the database).
-
#validate_name ⇒ Object
If the current form submission is a validation, this returns the name attribute of the form field that has triggered the validation.
Constructor Details
#initialize(controller) ⇒ Inspector
Returns a new instance of Inspector.
10 11 12 |
# File 'lib/upjs/rails/inspector.rb', line 10 def initialize(controller) @controller = controller end |
Instance Method Details
#target ⇒ Object
If the current request is a fragment update, this returns the CSS selector of the page fragment that should be updated.
The Up.js frontend will expect an HTML response containing an element that matches this selector. If no such element is found, an error is shown to the user.
Server-side code is free to optimize its response by only returning HTML that matches this selector.
32 33 34 |
# File 'lib/upjs/rails/inspector.rb', line 32 def target request.headers['X-Up-Target'] end |
#title=(new_title) ⇒ Object
Forces Up.js to use the given string as the document title when processing this response.
This is useful when you skip rendering the <head>
in an Up.js request.
56 57 58 |
# File 'lib/upjs/rails/inspector.rb', line 56 def title=(new_title) response.headers['X-Up-Title'] = new_title end |
#up? ⇒ Boolean
Returns whether the current request is an page fragment update triggered by an Up.js frontend.
18 19 20 |
# File 'lib/upjs/rails/inspector.rb', line 18 def up? target.present? end |
#validate? ⇒ Boolean
Returns whether the current form submission should be validated (and not be saved to the database).
39 40 41 |
# File 'lib/upjs/rails/inspector.rb', line 39 def validate? validate_name.present? end |
#validate_name ⇒ Object
If the current form submission is a validation, this returns the name attribute of the form field that has triggered the validation.
47 48 49 |
# File 'lib/upjs/rails/inspector.rb', line 47 def validate_name request.headers['X-Up-Validate'] end |