Use the same texts on event links and graphs

Note we're delegating the `t` method because i18n-tasks doesn't detect
code like `ApplicationController.helpers.t` and so reports we aren't
using the `admin.stats.graph` translations.
This commit is contained in:
Javi Martín
2024-04-25 03:18:12 +02:00
parent f7e2d724dd
commit 328413373e
8 changed files with 24 additions and 15 deletions

View File

@@ -1,4 +1,4 @@
<div id="graph" class="small-12 column"> <div id="graph" class="small-12 column">
<h2><%= t "admin.stats.graph.#{event}" %> (<%= count %>)</h2> <h2><%= title %></h2>
<%= chart_tag %> <%= chart_tag %>
</div> </div>

View File

@@ -18,4 +18,8 @@ class Admin::Stats::ChartComponent < ApplicationComponent
def chart_tag def chart_tag
tag.div("data-graph": admin_api_stats_path(event: event)) tag.div("data-graph": admin_api_stats_path(event: event))
end end
def title
"#{chart.title} (#{count})"
end
end end

View File

@@ -9,11 +9,7 @@ class Admin::Stats::EventLinksComponent < ApplicationComponent
private private
def link_text(event) def link_text(event)
text = t("admin.stats.graph.#{event}") Ahoy::Chart.new(event).title
if text.to_s.match(/translation missing/)
text = event
end
text
end end
def title def title

View File

@@ -2,6 +2,7 @@ module Ahoy
class Chart class Chart
attr_reader :event_name attr_reader :event_name
delegate :count, to: :records delegate :count, to: :records
delegate :t, to: "ApplicationController.helpers"
def initialize(event_name) def initialize(event_name)
@event_name = event_name @event_name = event_name
@@ -24,11 +25,19 @@ module Ahoy
def data_points def data_points
ds = Ahoy::DataSource.new ds = Ahoy::DataSource.new
ds.add event_name.to_s.titleize, records_by_day.count ds.add title, records_by_day.count
ds.build ds.build
end end
def title
text = t("admin.stats.graph.#{event_name}")
if text.to_s.match(/translation missing/)
text = event_name.to_s.titleize
end
text
end
private private
def records def records

View File

@@ -10,8 +10,8 @@ describe Admin::Stats::EventLinksComponent do
expect(page).to have_link count: 2 expect(page).to have_link count: 2
page.find("ul") do |list| page.find("ul") do |list|
expect(list).to have_link "legislation_annotation_created" expect(list).to have_link "Legislation Annotation Created"
expect(list).to have_link "legislation_answer_created" expect(list).to have_link "Legislation Answer Created"
end end
end end
end end

View File

@@ -29,7 +29,7 @@ describe Admin::Api::StatsController, :admin do
get :show, params: { event: "proposal_created" } get :show, params: { event: "proposal_created" }
expect(response).to be_ok expect(response).to be_ok
expect(response.parsed_body).to eq "x" => ["2015-01-01", "2015-01-02"], "Proposal Created" => [2, 1] expect(response.parsed_body).to eq "x" => ["2015-01-01", "2015-01-02"], "Citizen proposals" => [2, 1]
end end
end end
end end

View File

@@ -21,7 +21,7 @@ describe Ahoy::Chart do
chart = Ahoy::Chart.new(:proposal_created) chart = Ahoy::Chart.new(:proposal_created)
expect(chart.data_points).to eq x: ["2015-01-01", "2015-01-02"], "Proposal Created" => [2, 1] expect(chart.data_points).to eq x: ["2015-01-01", "2015-01-02"], "Citizen proposals" => [2, 1]
end end
it "accepts strings as the event name" do it "accepts strings as the event name" do
@@ -30,7 +30,7 @@ describe Ahoy::Chart do
chart = Ahoy::Chart.new("proposal_created") chart = Ahoy::Chart.new("proposal_created")
expect(chart.data_points).to eq x: ["2015-01-01"], "Proposal Created" => [1] expect(chart.data_points).to eq x: ["2015-01-01"], "Citizen proposals" => [1]
end end
it "returns visits data for the visits event" do it "returns visits data for the visits event" do
@@ -70,7 +70,7 @@ describe Ahoy::Chart do
chart = Ahoy::Chart.new(:level_3_user) chart = Ahoy::Chart.new(:level_3_user)
expect(chart.data_points).to eq x: ["2001-01-02"], "Level 3 User" => [2] expect(chart.data_points).to eq x: ["2001-01-02"], "Level 3 users" => [2]
end end
end end
end end

View File

@@ -159,9 +159,9 @@ describe "Stats", :admin do
create(:user, :level_three) create(:user, :level_three)
visit admin_stats_path visit admin_stats_path
click_link "level_3_user" click_link "Level 3 users"
expect(page).to have_content "Level 3 User (1)" expect(page).to have_content "Level 3 users (1)"
within("#graph") do within("#graph") do
expect(page).to have_content Date.current.strftime("%Y-%m-%d") expect(page).to have_content Date.current.strftime("%Y-%m-%d")