Skip to content

Commit

Permalink
Implement weighted results display on public interface (#171)
Browse files Browse the repository at this point in the history
* add weighted result

* add tests

* add tests

* add file to the lib/overrides_spec.rb

* add file to the lib/overrides_spec.rb
  • Loading branch information
antopalidi committed Mar 11, 2024
1 parent 504980f commit b481bde
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@ module QuestionOverride
def publishable_results?
(ActionDelegator.admin_preview_results || consultation.finished?) && sorted_results.any?
end

def weighted_responses
@weighted_responses ||= Decidim::ActionDelegator::SumOfWeights.new(consultation).query.group_by(&:question_id)
end

def total_weighted_votes
@total_weighted_votes ||= weighted_responses[id].sum(&:votes_count)
end

def most_weighted_voted_response
weighted_responses[id].max_by(&:votes_count)
end

def responses_sorted_by_weighted_votes
@responses_sorted_by_weighted_votes ||= weighted_responses.transform_values do |responses|
responses.sort_by { |response| -response.votes_count }
end
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<!-- replace_contents "erb[silent]:contains('if question.results_published?')"
closing_selector "erb[silent]:contains('else')" -->

<%= render partial: "decidim/action_delegator/consultations/link_with_results", locals: { question: question } %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- replace_contents ".card--list" -->

<%= render partial: "decidim/action_delegator/consultations/questions/weight_results", locals: { response: response, question: question } %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<%= link_to decidim_consultations.question_path(question), class: "button expanded button--sc" do %>
<%= t "consultations.question.view_results", scope: "decidim" %>
<% if question.most_weighted_voted_response.present? %>
<span class="button__info"><%= translated_attribute question.most_weighted_voted_response.title %></span>
<span class="button__info">
<%= question.most_weighted_voted_response.votes_count %>
<%= t "consultations.question.votes_out_of", scope: "decidim", count: question.most_weighted_voted_response.votes_count %>
<%= question.total_weighted_votes %>
</span>
<% end %>
<% end %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<% question.responses_sorted_by_weighted_votes[question.id].each do |response| %>
<div class=card--list__item>
<div class="card--list__text"><%= translated_attribute response.title %></div>
<div class="card--list__data">
<span class="card--list__data__number"><%= response.votes_count %></span>
</div>
</div>
<% end %>
2 changes: 2 additions & 0 deletions spec/lib/overrides_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ module Decidim::ActionDelegator
"/app/views/decidim/consultations/questions/_vote_modal.html.erb" => "bb4b10e9278cffd8d0d4eb57f5197a89",
"/app/views/decidim/consultations/questions/_vote_modal_confirm.html.erb" => "bac38cece8f1eaf76265fa1ad0ace064",
"/app/views/decidim/consultations/question_multiple_votes/_form.html.erb" => "af610283ce7ee20f5ef786228a263d4a",
"/app/views/decidim/consultations/questions/_results.html.erb" => "2d8196efbf23e2ad7b8c32713c28b240",
# monkeypatches
"/app/commands/decidim/consultations/vote_question.rb" => "bb0489e93d3bd142db19d9f93f556d67",
"/app/commands/decidim/consultations/multiple_vote_question.rb" => "86ac61db829acb4e86a9b6d90bd46333",
"/app/models/decidim/consultations/vote.rb" => "c06286e3f7366d3a017bf69f1c9e3eef",
"/app/models/decidim/consultations/question.rb" => "bc6e8618100f112c1d23bee9aaf5c0ed",
"/app/controllers/decidim/consultations/question_votes_controller.rb" => "69bf764e99dfcdae138613adbed28b84",
"/app/forms/decidim/consultations/vote_form.rb" => "d2b69f479b61b32faf3b108da310081a",
"/app/forms/decidim/consultations/multi_vote_form.rb" => "fc2160f0b5e85c9944d652b568c800f3"
Expand Down
35 changes: 33 additions & 2 deletions spec/models/decidim/consultations/question_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ module Consultations

let(:consultation) { create :consultation, :published, :active }
let(:question) { create :question, consultation: consultation }
let(:response) { create :response, question: question }
let!(:vote) { create :vote, question: question, response: response }
let(:responses) { create_list :response, 3, question: question }
let!(:votes) do
responses.map.with_index do |response, index|
create_list :vote, index + 1, question: question, response: response
end.flatten
end

it { is_expected.to be_publishable_results }

Expand All @@ -21,6 +25,33 @@ module Consultations

it { is_expected.not_to be_publishable_results }
end

describe "#weighted_responses" do
it "groups responses by question and calculates their weight" do
expect(question.weighted_responses[question.id].map(&:votes_count)).to match_array([1, 2, 3])
end
end

describe "#total_weighted_votes" do
it "returns the total weighted vote count for the question" do
# The total number of votes = 1 + 2 + 3
expect(question.total_weighted_votes).to eq(6)
end
end

describe "#most_weighted_voted_response" do
it "returns the response with the highest weighted vote count" do
# The response with the highest vote count has 3 votes
expect(question.most_weighted_voted_response.votes_count).to eq(3)
end
end

describe "#responses_sorted_by_weighted_votes" do
it "returns responses sorted by descending weighted vote count" do
sorted_votes_counts = question.responses_sorted_by_weighted_votes[question.id].map(&:votes_count)
expect(sorted_votes_counts).to eq([3, 2, 1])
end
end
end
end
end
55 changes: 55 additions & 0 deletions spec/system/decidim/action_delegator/weighted_results_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# frozen_string_literal: true

require "spec_helper"

describe "Weighted results", type: :system do
let(:organization) { create :organization, available_locales: [:en] }
let(:user) { create(:user, :admin, :confirmed, organization: organization) }

let(:consultation) { create(:consultation, :finished, :published_results, organization: organization) }
let!(:question) { create(:question, consultation: consultation) }
let!(:responses) do
Array.new(2) { |i| create(:response, question: question, title: { "en" => "Option #{i + 1} Title" }) }
end

let!(:other_user) { create(:user, :confirmed, organization: organization) }

let(:setting) { create(:setting, consultation: consultation) }
let(:ponderation1) { create(:ponderation, setting: setting, name: "consumer", weight: 4) }

before do
# Regular vote
question.votes.create(author: user, response: responses.first)
# Vote of a user with membership
question.votes.create(author: other_user, response: responses.last)

create(:participant, setting: setting, decidim_user: other_user, ponderation: ponderation1)

switch_to_host(organization.host)
login_as user, scope: :user
visit decidim_consultations.consultation_path(consultation)
end

it "displays weighted results" do
expect(page).to have_content("Option 2")
expect(page).to have_content("4 votes out of 5")
end

context "when visiting the question page" do
before do
visit decidim_consultations.question_path(question)
end

it "displays weighted results" do
within first(".card--list__item") do
expect(page).to have_content("Option 2")
expect(page).to have_content(4)
end

within all(".card--list__item")[1] do
expect(page).to have_content("Option 1")
expect(page).to have_content(1)
end
end
end
end

0 comments on commit b481bde

Please sign in to comment.