Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cookies Banner (fixes #3859) #3909

Merged
merged 19 commits into from
Dec 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
# Ignore bundler config.
/.bundle

/vendor/bundle

# Ignore coverage results

/coverage
Expand Down
6 changes: 3 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ GEM
acts_as_tree (2.9.1)
activerecord (>= 3.0.0)
acts_as_votable (0.12.1)
addressable (2.8.1)
addressable (2.8.6)
public_suffix (>= 2.0.2, < 6.0)
ast (2.4.2)
autoprefixer-rails (10.4.13.0)
Expand Down Expand Up @@ -506,7 +506,7 @@ GEM
activerecord (>= 5.0)
i18n (>= 0.5.0)
railties (>= 5.0.0)
public_suffix (5.0.1)
public_suffix (5.0.4)
puffing-billy (3.0.4)
addressable (~> 2.5)
em-http-request (~> 1.1, >= 1.1.0)
Expand Down Expand Up @@ -755,7 +755,7 @@ GEM
hashdiff (>= 0.4.0, < 2.0.0)
webrick (1.8.1)
websocket (1.2.9)
websocket-driver (0.7.5)
websocket-driver (0.7.6)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
will_paginate (3.3.1)
Expand Down
4 changes: 3 additions & 1 deletion app/assets/config/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@
//= link lolex.js
//= link disqus.js
//= link 404.js
//= link subscriptions.css
//= link subscriptions.css
//= link google-analytics.js
//= link cookies_banner.js
2 changes: 1 addition & 1 deletion app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import "trix";
import './global-modules/*.js';
import './documents';
import './users';
import './google-analytics';
import LocalTime from "local-time";
import './controllers/*.js';


LocalTime.start()

$(function() {
Expand Down
8 changes: 8 additions & 0 deletions app/assets/javascripts/cookies_banner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
if (sessionStorage.getItem('banner') === 'hide') {
document.querySelector('.cookies-banner-modal').style.display = 'none';
} else {
document.querySelector('.close').addEventListener('click', () => {
sessionStorage.setItem('banner', 'hide')
document.querySelector('.cookies-banner-modal').style.display = 'none';
})
}
13 changes: 13 additions & 0 deletions app/assets/stylesheets/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,16 @@
float: right !important;
}
}

.cookies-banner-modal {
position: fixed;
padding: 0.5rem;
z-index: 2;
left: 0.5rem;
bottom: 0.5rem;
min-width: 50%;
max-width: 24rem;
word-break: break-word;
border-radius: 6px;
background: #bad5ff;
}
5 changes: 4 additions & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ def modify_user_signup_params
# set current_user.id to a cookie to allow google analytics to access current_user var

def set_user_id
cookies[:user_id] = current_user.id if current_user
case session[:cookies_accepted]
when 'true'
cookies[:user_id] = current_user.id if current_user
end
end
end
8 changes: 8 additions & 0 deletions app/controllers/cookies_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

class CookiesController < ApplicationController
def index
session[:cookies_accepted] = params[:cookies] if params[:cookies]
redirect_back_or_to '/'
end
end
7 changes: 7 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,11 @@ def default_meta_description
def present(model)
yield(model.presenter)
end

def delete_google_cookies
cookies.delete('_ga')
cookies.delete('_gid')
cookies.delete('_gat')
cookies['GA-disable-UA-47795185-1'] = 'true'
end
end
4 changes: 4 additions & 0 deletions app/helpers/cookies_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

module CookiesHelper
end
Empty file.
12 changes: 12 additions & 0 deletions app/views/layouts/_cookies_banner.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<% if session[:cookies_accepted].nil? && !(request.cookies['_ga'].present? || request.cookies['_gid'].present? || request.cookies['_gat'].present?)# don't re-render if a true/false selected %>
<section class="cookies-banner-modal">
<a class="close">X</a>
<h4>We respect your privacy. Cookies are used to analyze traffic.</h4>
<%= link_to "Accept cookies", cookies_path(cookies: true), method: :post, class: 'btn btn-success' %>
<%= link_to "Reject cookies", cookies_path(cookies: false), method: :post, class: 'btn btn-default' %>
<%= link_to 'Privacy statement', '/privacy' %>
</section>
<% end %>

<%= javascript_include_tag 'cookies_banner' %>

10 changes: 8 additions & 2 deletions app/views/layouts/_head.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@
<%= javascript_include_tag 'application', 'data-turbo-track': 'reload' %>
<%= csrf_meta_tags %>
<link rel="shortcut icon" href="/favicon.ico?v=2" />
<% if ENV['ENABLE_GOOGLE_ANALYTICS'] %>
<%= javascript_include_tag 'google-analytics', 'data-turbo-track': 'reload' %>

<% case session[:cookies_accepted] %>
<% when 'true' %>
<%= javascript_include_tag 'google-analytics', 'data-turbo-track': 'reload' %>
<% when 'false' %>
<% delete_google_cookies %>
<% when 'nil' %>
<% delete_google_cookies %>
<% end %>
</head>
1 change: 1 addition & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<%= render 'layouts/require_users_profile' %>
<% end %>
</div>
<%= render 'layouts/cookies_banner' if session[:cookies_accepted].nil? %>

<footer id="footer">
<%= render 'layouts/footer' %>
Expand Down
4 changes: 3 additions & 1 deletion app/views/visitors/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

<%= render 'layouts/navbar' %>


<div id="main" class="container-fluid" style="padding: 0">
<ul id="section-list">
<li>
Expand All @@ -25,7 +26,8 @@
<%= render 'layouts/require_users_profile' %>
<% end %>
<%= render 'text_and_image_trail' %>

<%= render 'layouts/cookies_banner' if session[:cookies_accepted].nil? %>

<footer id="footer">
<%= render 'layouts/footer' %>
</footer>
Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ def loaderio_token
Rails.application.routes.draw do
root 'visitors#index'

get 'cookies' => 'cookies#index'

get '/.well-known/acme-challenge/:id' => 'static_pages#letsencrypt'
get loaderio_token => 'static_pages#loaderio'
get '/get-token' => 'av_dashboard_tokens#create', as: 'get_av_dashboard_token'
Expand Down
2 changes: 1 addition & 1 deletion features/support/capybara.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

Capybara.javascript_driver = :cuprite
Capybara.register_driver(:cuprite) do |app|
Capybara::Cuprite::Driver.new(app, window_size: [1200, 800], browser_options: { 'no-sandbox': nil })
Capybara::Cuprite::Driver.new(app, window_size: [1440, 1100], browser_options: { 'no-sandbox': nil })
end

Capybara.save_path = 'tmp/capybara'