From db59071052f054594de47f777225ccdcb2ce5ec6 Mon Sep 17 00:00:00 2001 From: Pablo Brasero Date: Tue, 9 Dec 2025 12:59:57 +0000 Subject: [PATCH] First stab at hiding the heatmap --- .../profiles/heatmaps_controller.rb | 11 +++++ app/controllers/users/heatmaps_controller.rb | 2 +- app/controllers/users_controller.rb | 2 +- app/views/users/show.html.erb | 11 ++++- config/locales/en.yml | 4 ++ config/routes.rb | 1 + .../profiles/heatmaps_controller_test.rb | 49 +++++++++++++++++++ 7 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 app/controllers/profiles/heatmaps_controller.rb create mode 100644 test/controllers/profiles/heatmaps_controller_test.rb diff --git a/app/controllers/profiles/heatmaps_controller.rb b/app/controllers/profiles/heatmaps_controller.rb new file mode 100644 index 000000000..f01c2b3db --- /dev/null +++ b/app/controllers/profiles/heatmaps_controller.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +module Profiles + class HeatmapsController < ProfileSectionsController + private + + def update_profile + current_user.update(:public_heatmap => params[:user][:public_heatmap]) + end + end +end diff --git a/app/controllers/users/heatmaps_controller.rb b/app/controllers/users/heatmaps_controller.rb index 5ccc352da..3ed2db6eb 100644 --- a/app/controllers/users/heatmaps_controller.rb +++ b/app/controllers/users/heatmaps_controller.rb @@ -13,7 +13,7 @@ module Users def show @user = User.find_by(:display_name => params[:user_display_name]) - if @user && (@user.visible? || current_user&.administrator?) + if @user&.public_heatmap? && (@user.visible? || current_user&.administrator?) @heatmap_data = Rails.cache.fetch("heatmap_data_of_user_#{@user.id}", :expires_at => Time.zone.now.end_of_day) do from = 1.year.ago.beginning_of_day to = Time.zone.now.end_of_day diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index dccff0fc6..29fae6491 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -25,7 +25,7 @@ class UsersController < ApplicationController if @user && (@user.visible? || current_user&.administrator?) @title = @user.display_name - @heatmap_frame = true + @heatmap_frame = @user.public_heatmap? else render_unknown_user params[:display_name] end diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index 72c4a28bd..1111cd26c 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -1,3 +1,5 @@ +<% owned = current_user && @user == current_user %> + <% content_for :head do %> <%= javascript_include_tag "heatmap" %> <% end %> @@ -235,7 +237,6 @@ <% end %> -<% owned = current_user && @user == current_user %> <% if @user.home_location_name&.strip.present? || @user.company&.strip.present? || !@user.social_links.empty? || owned %>
@@ -295,7 +296,13 @@ <% end %> <% if @heatmap_frame %> - <%= turbo_frame_tag "#{dom_id(@user)}_heatmap", :src => user_heatmap_path(@user), :data => { :turbo => false } %> + <%= turbo_frame_tag "#{dom_id(@user)}_heatmap", :src => user_heatmap_path(@user), :data => { :turbo => false }, :class => "heatmap_frame" %> +<% end %> +<% if owned %> + <%= bootstrap_form_for current_user, :url => profile_heatmap_path, :layout => :inline do |f| %> + <%= f.check_box :public_heatmap, :label => "Show the heatmap on your profile page", :checked => current_user.public_heatmap? %> + <%= f.primary "Save" %> + <% end %> <% end %> <%= render :partial => "diary_entries/profile_diaries", :locals => { :diary_entries => @user.diary_entries.visible.order(:created_at => :desc).limit(4) } %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 74a271f1f..6dd693081 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -2214,6 +2214,10 @@ en: update: success: Profile location updated. failure: Couldn't update profile location. + heatmaps: + update: + success: Heatmap updated. + failure: Couldn't update the heatmap. sessions: new: tab_title: "Log In" diff --git a/config/routes.rb b/config/routes.rb index ead0044c0..0ba4f637b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -328,6 +328,7 @@ OpenStreetMap::Application.routes.draw do namespace :profile, :module => :profiles do resource :description, :only => [:show, :update] resource :links, :only => [:show, :update] + resource :heatmap, :only => [:update] resource :image, :only => [:show, :update] resource :company, :only => [:show, :update] resource :location, :only => [:show, :update] diff --git a/test/controllers/profiles/heatmaps_controller_test.rb b/test/controllers/profiles/heatmaps_controller_test.rb new file mode 100644 index 000000000..2b9f743f3 --- /dev/null +++ b/test/controllers/profiles/heatmaps_controller_test.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +require "test_helper" + +module Profiles + class HeatmapsControllerTest < ActionDispatch::IntegrationTest + ## + # test all routes which lead to this controller + def test_routes + assert_routing( + { :path => "/profile/heatmap", :method => :put }, + { :controller => "profiles/heatmaps", :action => "update" } + ) + end + + def test_update + user = create(:user, :public_heatmap => true) + session_for(user) + + heatmap_selector = ".heatmap_frame" + + assert_predicate user.reload, :public_heatmap? + get user_path(user) + assert_select heatmap_selector + + put profile_heatmap_path, :params => { :user => { :public_heatmap => "0" } } + + assert_redirected_to user_path(user) + follow_redirect! + assert_response :success + assert_template :show + assert_dom ".alert-success", :text => "Heatmap updated." + + assert_not_predicate user.reload, :public_heatmap? + refute_select heatmap_selector + + put profile_heatmap_path, :params => { :user => { :public_heatmap => "1" } } + + assert_redirected_to user_path(user) + follow_redirect! + assert_response :success + assert_template :show + assert_dom ".alert-success", :text => "Heatmap updated." + + assert_predicate user.reload, :public_heatmap? + assert_select heatmap_selector + end + end +end -- 2.39.5