Move stats graph partial to a component

Note we're naming it "chart" in order to avoid possible conflicts with
the "graph" view when we move it to a component.
This commit is contained in:
Javi Martín
2024-04-24 01:45:48 +02:00
parent b04ac817f6
commit 1edf0d937d
5 changed files with 29 additions and 18 deletions

View File

@@ -30,7 +30,7 @@
</div>
</div>
<%= render "admin/stats/graph", name: "user_supported_budgets", event: "", count: user_count %>
<%= render Admin::Stats::ChartComponent.new(name: "user_supported_budgets", event: "", count: user_count) %>
<table class="investment-projects-summary user-count-by-heading">
<thead>

View File

@@ -0,0 +1,27 @@
class Admin::Stats::ChartComponent < ApplicationComponent
attr_reader :name, :event, :count
def initialize(name:, event:, count:)
@name = name
@event = event
@count = count
end
private
def chart_tag(opt = {})
opt[:data] ||= {}
opt[:data][:graph] = admin_api_stats_path(chart_data(opt))
tag.div(**opt)
end
def chart_data(opt = {})
data = nil
if opt[:id].present?
data = { opt[:id] => true }
elsif opt[:event].present?
data = { event: opt[:event] }
end
data
end
end

View File

@@ -1,20 +1,4 @@
module StatsHelper
def chart_tag(opt = {})
opt[:data] ||= {}
opt[:data][:graph] = admin_api_stats_path(chart_data(opt))
tag.div(**opt)
end
def chart_data(opt = {})
data = nil
if opt[:id].present?
data = { opt[:id] => true }
elsif opt[:event].present?
data = { event: opt[:event] }
end
data
end
def budget_investments_chart_tag(opt = {})
opt[:data] ||= {}
opt[:data][:graph] = admin_api_stats_path(budget_investments: true)

View File

@@ -4,4 +4,4 @@
<%= back_link_to admin_stats_path %>
<%= render "graph", name: @name, event: @event, count: @count %>
<%= render Admin::Stats::ChartComponent.new(name: @name, event: @event, count: @count) %>