From 9a4aea938100e30c326fe17e66433fac22cd239a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Fri, 8 Nov 2024 11:41:48 +0100 Subject: [PATCH 1/2] Extract method to include stat graphs JavaScript We're going to use this method everywhere in the admin stats section. --- .../admin/stats/budget_supporting_component.html.erb | 4 +--- app/components/admin/stats/budget_supporting_component.rb | 1 + app/helpers/application_helper.rb | 6 ++++++ app/views/admin/stats/graph.html.erb | 4 +--- app/views/admin/stats/show.html.erb | 4 +--- 5 files changed, 10 insertions(+), 9 deletions(-) diff --git a/app/components/admin/stats/budget_supporting_component.html.erb b/app/components/admin/stats/budget_supporting_component.html.erb index 957908e9c..4c518b457 100644 --- a/app/components/admin/stats/budget_supporting_component.html.erb +++ b/app/components/admin/stats/budget_supporting_component.html.erb @@ -1,6 +1,4 @@ -<% content_for :head do %> - <%= javascript_include_tag "stat_graphs", "data-turbolinks-track" => "reload" %> -<% end %> +<% include_stat_graphs_javascript %> <%= back_link_to budgets_admin_stats_path %> diff --git a/app/components/admin/stats/budget_supporting_component.rb b/app/components/admin/stats/budget_supporting_component.rb index 0fdeb7b20..45dd650c2 100644 --- a/app/components/admin/stats/budget_supporting_component.rb +++ b/app/components/admin/stats/budget_supporting_component.rb @@ -1,5 +1,6 @@ class Admin::Stats::BudgetSupportingComponent < ApplicationComponent attr_reader :budget + use_helpers :include_stat_graphs_javascript def initialize(budget) @budget = budget diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 801bb8e16..1440604ee 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -18,6 +18,12 @@ module ApplicationHelper WYSIWYGSanitizer.new.sanitize(text) end + def include_stat_graphs_javascript + content_for :head do + javascript_include_tag "stat_graphs", "data-turbolinks-track" => "reload" + end + end + def author_of?(authorable, user) return false if authorable.blank? || user.blank? diff --git a/app/views/admin/stats/graph.html.erb b/app/views/admin/stats/graph.html.erb index de76aee54..17dcf48fa 100644 --- a/app/views/admin/stats/graph.html.erb +++ b/app/views/admin/stats/graph.html.erb @@ -1,6 +1,4 @@ -<% content_for :head do %> - <%= javascript_include_tag "stat_graphs", "data-turbolinks-track" => "reload" %> -<% end %> +<% include_stat_graphs_javascript %> <%= back_link_to admin_stats_path %> diff --git a/app/views/admin/stats/show.html.erb b/app/views/admin/stats/show.html.erb index b6eccdbc0..2879900aa 100644 --- a/app/views/admin/stats/show.html.erb +++ b/app/views/admin/stats/show.html.erb @@ -1,6 +1,4 @@ -<% content_for :head do %> - <%= javascript_include_tag "stat_graphs", "data-turbolinks-track" => "reload" %> -<% end %> +<% include_stat_graphs_javascript %>
From 02825c22feb6c0cd5ceff780344d37cffc650ab0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Fri, 8 Nov 2024 15:43:20 +0100 Subject: [PATCH 2/2] Include stat graphs JavaScript in all admin stats actions Since the main stats index loads this JavaScript using `"data-turbolinks-track" => "reload"`, going from the stats index to a section that doesn't include this JavaScript did the strange effect Turbolinks does in these situations: it first loaded the page using an AJAX request and, after getting the contents of the page, it reloaded it in order to apply the changes in the included JavaScript. This behavior was a bit confusing, particularly when browsing to a section of the admin stats, clicking the browser's back button to go back to the stats index, the going to another section, ... One of the admin stats tests was failing sometimes with this message: ``` 1) Stats Budget investments Supporting phase Number of users and supports in investment projects Failure/Error: raise ex, cause: cause Selenium::WebDriver::Error::UnknownError: unknown error: unhandled inspector error: {"code":-32000,"message":"Node with given id does not belong to the document"} (Session info: chrome=129.0.6668.89) ``` This was probably caused by the mentioned Turbolinks behavior that loads the page twice. It's possible that Selenium was somehow checking the node related to the first request when the second request had finished. Avoiding that double request solves the issue. --- app/components/admin/stats/budget_balloting_component.html.erb | 2 ++ app/components/admin/stats/budget_balloting_component.rb | 1 + app/views/admin/stats/budgets.html.erb | 2 ++ app/views/admin/stats/direct_messages.html.erb | 2 ++ app/views/admin/stats/polls.html.erb | 2 ++ app/views/admin/stats/proposal_notifications.html.erb | 2 ++ app/views/admin/stats/sdg.html.erb | 2 ++ 7 files changed, 13 insertions(+) diff --git a/app/components/admin/stats/budget_balloting_component.html.erb b/app/components/admin/stats/budget_balloting_component.html.erb index 4e13af338..da66118f3 100644 --- a/app/components/admin/stats/budget_balloting_component.html.erb +++ b/app/components/admin/stats/budget_balloting_component.html.erb @@ -1,3 +1,5 @@ +<% include_stat_graphs_javascript %> + <%= back_link_to budgets_admin_stats_path %>

<%= budget.name %> - <%= t("admin.stats.budget_balloting.title") %>

diff --git a/app/components/admin/stats/budget_balloting_component.rb b/app/components/admin/stats/budget_balloting_component.rb index 12addf185..ecb4b98e1 100644 --- a/app/components/admin/stats/budget_balloting_component.rb +++ b/app/components/admin/stats/budget_balloting_component.rb @@ -1,5 +1,6 @@ class Admin::Stats::BudgetBallotingComponent < ApplicationComponent attr_reader :budget + use_helpers :include_stat_graphs_javascript def initialize(budget) @budget = budget diff --git a/app/views/admin/stats/budgets.html.erb b/app/views/admin/stats/budgets.html.erb index 6f7b2a9f0..4b11e72f0 100644 --- a/app/views/admin/stats/budgets.html.erb +++ b/app/views/admin/stats/budgets.html.erb @@ -1,3 +1,5 @@ +<% include_stat_graphs_javascript %> + <%= back_link_to admin_stats_path %>

<%= t("admin.stats.budgets.title") %>

diff --git a/app/views/admin/stats/direct_messages.html.erb b/app/views/admin/stats/direct_messages.html.erb index ff9ccbc8c..fbf807e95 100644 --- a/app/views/admin/stats/direct_messages.html.erb +++ b/app/views/admin/stats/direct_messages.html.erb @@ -1,3 +1,5 @@ +<% include_stat_graphs_javascript %> + <%= back_link_to admin_stats_path %>

<%= t("admin.stats.direct_messages.title") %>

diff --git a/app/views/admin/stats/polls.html.erb b/app/views/admin/stats/polls.html.erb index 7c30c0093..135ec92bb 100644 --- a/app/views/admin/stats/polls.html.erb +++ b/app/views/admin/stats/polls.html.erb @@ -1,3 +1,5 @@ +<% include_stat_graphs_javascript %> + <%= back_link_to admin_stats_path %>

<%= t("admin.stats.polls.title") %>

diff --git a/app/views/admin/stats/proposal_notifications.html.erb b/app/views/admin/stats/proposal_notifications.html.erb index 4c66ca46e..9f18ba400 100644 --- a/app/views/admin/stats/proposal_notifications.html.erb +++ b/app/views/admin/stats/proposal_notifications.html.erb @@ -1,3 +1,5 @@ +<% include_stat_graphs_javascript %> + <%= back_link_to admin_stats_path %>

<%= t("admin.stats.proposal_notifications.title") %>

diff --git a/app/views/admin/stats/sdg.html.erb b/app/views/admin/stats/sdg.html.erb index 52efe8cf4..d8f4d1043 100644 --- a/app/views/admin/stats/sdg.html.erb +++ b/app/views/admin/stats/sdg.html.erb @@ -1 +1,3 @@ +<% include_stat_graphs_javascript %> + <%= render Admin::Stats::SDGComponent.new(@goals) %>