Skip to content

Instantly share code, notes, and snippets.

@ayrton
Last active December 11, 2020 18:33
Show Gist options
  • Save ayrton/f2916a37bf9dbd985437d7c1799fe6f6 to your computer and use it in GitHub Desktop.
Save ayrton/f2916a37bf9dbd985437d7c1799fe6f6 to your computer and use it in GitHub Desktop.
class ApplicationController < ActionController::Base
include EtagWithLayout
end
module EtagWithLayout
extend ActiveSupport::Concern
include ActionController::ConditionalGet
included { etag { layout_etag if request.format.html? } }
private
def layout_etag
layout_name = determine_layout_name
if layout_name
ActionView::Digestor.digest name: layout_name,
format: nil,
finder: lookup_context
end
end
def determine_layout_name
current_layout = _layout lookup_context, %i[html]
case current_layout
when ActionView::Template
current_layout.virtual_path
when String
"layouts/#{current_layout}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment