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.
37 lines
824 B
Ruby
37 lines
824 B
Ruby
class Admin::Stats::BudgetBallotingComponent < ApplicationComponent
|
|
attr_reader :budget
|
|
use_helpers :include_stat_graphs_javascript
|
|
|
|
def initialize(budget)
|
|
@budget = budget
|
|
end
|
|
|
|
private
|
|
|
|
def stats
|
|
@stats ||= Budget::Stats.new(budget, cache: false)
|
|
end
|
|
|
|
def headings_stats
|
|
@headings_stats ||= stats.headings
|
|
end
|
|
|
|
def vote_count
|
|
stats.total_votes
|
|
end
|
|
|
|
def user_count
|
|
stats.total_participants_vote_phase
|
|
end
|
|
|
|
def vote_count_by_heading
|
|
budget.lines.group(:heading_id).count.map { |k, v| [Budget::Heading.find(k).name, v] }.sort
|
|
end
|
|
|
|
def user_count_by_heading
|
|
budget.headings.map do |heading|
|
|
[heading.name, headings_stats[heading.id][:total_participants_vote_phase]]
|
|
end.select { |_, count| count > 0 }.sort
|
|
end
|
|
end
|