Skip to content

Commit

Permalink
[feature] integrate CMS with cookie consent UI (#1328)
Browse files Browse the repository at this point in the history
  • Loading branch information
donrestarone and alis-khadka authored Jan 2, 2023
1 parent 47e1451 commit 11b4b93
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
16 changes: 15 additions & 1 deletion app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ def show_cookies_consent_banner?
end

def render_cookies_consent_ui
render partial: 'shared/cookies_consent_ui'
if show_cookies_consent_banner?
if @cms_page.present?
context = @cms_page
else
cms_site = @site || cms_site_detect
[:pages, :blog_posts, :snippets, :layouts].any? do |association|
cms_site.send(association).present? && (context = cms_site.send(association).first)
end
end

r = ComfortableMexicanSofa::Content::Renderer.new(context)
html_text = r.render(r.nodes(r.tokenize(Subdomain.current.cookies_consent_ui)))

render partial: 'shared/cookies_consent_ui', locals: {html_text: html_text}
end
end
end
2 changes: 1 addition & 1 deletion app/views/shared/_cookies_consent_ui.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div id="cookie-consent-wrapper">
<%= render html: Subdomain.current.cookies_consent_ui.html_safe if show_cookies_consent_banner? %>
<%= render html: html_text.html_safe %>
</div>

<script>
Expand Down
28 changes: 28 additions & 0 deletions test/controllers/admin/comfy/cms/content_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,32 @@ class Comfy::Cms::ContentControllerTest < ActionDispatch::IntegrationTest
get root_url(subdomain: @restarone_subdomain.name)
assert_response :success
end

test "get root page with cookies_consent_ui rendering cms snippets of file" do
Apartment::Tenant.switch @restarone_subdomain.name do
@site = Comfy::Cms::Site.first
file = @site.files.create(
label: "test",
description: "test file",
file: fixture_file_upload("fixture_image.png", "image/jpeg")
)

file_link_snippet = "{{ cms:file_link #{file.id} }}"
file_image_tag = "{{ cms:file_link #{file.id}, as: image }}"

@restarone_subdomain.update!(tracking_enabled: true, cookies_consent_ui: file_link_snippet + file_image_tag)
@user.update(can_view_restricted_pages: true)

sign_in(@user)
perform_enqueued_jobs do
get root_url(subdomain: @restarone_subdomain.name)

# file_image_tag gets converted into img tag
assert_select "#cookie-consent-wrapper img", { count: 1 }
# cms:file_link snippets are not rendered as it is.
assert_select "#cookie-consent-wrapper", { count: 0, text: file_link_snippet }
assert_select "#cookie-consent-wrapper", { count: 0, text: file_image_tag }
end
end
end
end

0 comments on commit 11b4b93

Please sign in to comment.