Created
August 15, 2020 00:11
-
-
Save savroff/5837bb1488944847721f5e10e87c9592 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Inertiable | |
extend ActiveSupport::Concern | |
included do | |
before_action :inertiable | |
end | |
def inertiable | |
response.set_header('Vary', 'Accept') | |
response.set_header('X-Inertia', true) if inertiable? | |
end | |
def inertiable? | |
request.headers['X-Inertia'].present? | |
end | |
def inertia_render(component, props = {}) | |
props = @shared_props.merge(props) if @shared_props | |
component_name = component.to_s.camelize.gsub('::', '/') | |
data = { | |
component: component_name, | |
props: props, | |
url: request.url | |
} | |
if inertiable? | |
render json: data | |
else | |
html = helpers.tag.div( | |
id: 'app', | |
data: { page: data.to_json } | |
) | |
render html: html, layout: true | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment