From 1664ebe8eb7e14631987b99a2a4b7204ef2a4e75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Fri, 17 Sep 2021 18:36:00 +0200 Subject: [PATCH 1/3] Refactor code comparing polls --- app/models/poll.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/app/models/poll.rb b/app/models/poll.rb index a8f2b6810..7ef78b2a8 100644 --- a/app/models/poll.rb +++ b/app/models/poll.rb @@ -53,15 +53,7 @@ class Poll < ApplicationRecord def self.sort_for_list all.sort do |poll, another_poll| - if poll.geozone_restricted? == another_poll.geozone_restricted? - [poll.starts_at, poll.name] <=> [another_poll.starts_at, another_poll.name] - else - if poll.geozone_restricted? - 1 - else - -1 - end - end + [poll.weight, poll.starts_at, poll.name] <=> [another_poll.weight, another_poll.starts_at, another_poll.name] end end @@ -190,4 +182,12 @@ class Poll < ApplicationRecord def self.search(terms) pg_search(terms) end + + def weight + if geozone_restricted? + 100 + else + 0 + end + end end From 25aa77c4c3ae01f2570a8d45533b3ac970bf25c5 Mon Sep 17 00:00:00 2001 From: decabeza Date: Fri, 30 Jul 2021 11:51:13 +0200 Subject: [PATCH 2/3] Show polls with the user's geozone first --- app/controllers/polls_controller.rb | 2 +- app/models/poll.rb | 12 ++++++++---- spec/models/poll/poll_spec.rb | 13 +++++++++++++ spec/system/polls/polls_spec.rb | 12 ++++++++++++ 4 files changed, 34 insertions(+), 5 deletions(-) diff --git a/app/controllers/polls_controller.rb b/app/controllers/polls_controller.rb index c04fab13b..154961a6b 100644 --- a/app/controllers/polls_controller.rb +++ b/app/controllers/polls_controller.rb @@ -14,7 +14,7 @@ class PollsController < ApplicationController def index @polls = Kaminari.paginate_array( - @polls.created_by_admin.not_budget.send(@current_filter).includes(:geozones).sort_for_list + @polls.created_by_admin.not_budget.send(@current_filter).includes(:geozones).sort_for_list(current_user) ).page(params[:page]) end diff --git a/app/models/poll.rb b/app/models/poll.rb index 7ef78b2a8..48cffaace 100644 --- a/app/models/poll.rb +++ b/app/models/poll.rb @@ -51,9 +51,9 @@ class Poll < ApplicationRecord scope :not_budget, -> { where(budget_id: nil) } scope :created_by_admin, -> { where(related_type: nil) } - def self.sort_for_list + def self.sort_for_list(user = nil) all.sort do |poll, another_poll| - [poll.weight, poll.starts_at, poll.name] <=> [another_poll.weight, another_poll.starts_at, another_poll.name] + [poll.weight(user), poll.starts_at, poll.name] <=> [another_poll.weight(user), another_poll.starts_at, another_poll.name] end end @@ -183,9 +183,13 @@ class Poll < ApplicationRecord pg_search(terms) end - def weight + def weight(user) if geozone_restricted? - 100 + if answerable_by?(user) + 50 + else + 100 + end else 0 end diff --git a/spec/models/poll/poll_spec.rb b/spec/models/poll/poll_spec.rb index 329cbd64e..18cf75c4a 100644 --- a/spec/models/poll/poll_spec.rb +++ b/spec/models/poll/poll_spec.rb @@ -370,6 +370,19 @@ describe Poll do expect(Poll.sort_for_list).to eq [poll1, poll2] end + it "returns polls for the user's geozone first" do + geozone = create(:geozone) + poll1 = create(:poll, geozone_restricted: true) + poll2 = create(:poll, geozone_restricted: true) + poll3 = create(:poll) + poll_geozone_1 = create(:poll, geozone_restricted: true, geozones: [geozone]) + poll_geozone_2 = create(:poll, geozone_restricted: true, geozones: [geozone]) + geozone_user = create(:user, :level_two, geozone: geozone) + + expect(Poll.sort_for_list).to eq [poll3, poll1, poll2, poll_geozone_1, poll_geozone_2] + expect(Poll.sort_for_list(geozone_user)).to eq [poll3, poll_geozone_1, poll_geozone_2, poll1, poll2] + end + it "returns polls earlier to start first" do starts_at = Time.current + 1.day poll1 = create(:poll, geozone_restricted: false, starts_at: starts_at - 1.hour, name: "Zzz...") diff --git a/spec/system/polls/polls_spec.rb b/spec/system/polls/polls_spec.rb index e0d175db1..74b5f641c 100644 --- a/spec/system/polls/polls_spec.rb +++ b/spec/system/polls/polls_spec.rb @@ -420,6 +420,18 @@ describe "Polls" do expect(page).to have_selector "img[alt='1. No Poverty']" expect(page).to have_content "target 1.1" end + + scenario "Polls with users same-geozone listed first" do + create(:poll, geozone_restricted: true, name: "A Poll") + create(:poll, name: "Not restricted") + create(:poll, geozone_restricted: true, geozones: [geozone], name: "Geozone Poll") + + login_as(create(:user, :level_two, geozone: geozone)) + visit polls_path(poll) + + expect("Not restricted").to appear_before("Geozone Poll") + expect("Geozone Poll").to appear_before("A Poll") + end end context "Booth & Website", :with_frozen_time do From 9709b267a25d7c1f350f87e415104e4536839728 Mon Sep 17 00:00:00 2001 From: decabeza Date: Sun, 1 Aug 2021 00:33:27 +0200 Subject: [PATCH 3/3] Always show order poll questions by created at PostgreSQL doesn't guarantee the order of the records, so we have to specify it if we want the questions to be displayed in a consistent order. --- app/views/admin/poll/polls/_questions.html.erb | 2 +- app/views/polls/_poll_group.html.erb | 2 +- spec/system/polls/polls_spec.rb | 18 ++++++++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/app/views/admin/poll/polls/_questions.html.erb b/app/views/admin/poll/polls/_questions.html.erb index b13a86be2..e7f1fc7a0 100644 --- a/app/views/admin/poll/polls/_questions.html.erb +++ b/app/views/admin/poll/polls/_questions.html.erb @@ -15,7 +15,7 @@ <%= t("admin.actions.actions") %> - <% @poll.questions.each do |question| %> + <% @poll.questions.sort_for_list.each do |question| %> <%= question.title %> diff --git a/app/views/polls/_poll_group.html.erb b/app/views/polls/_poll_group.html.erb index bcc58aa28..3a4d66296 100644 --- a/app/views/polls/_poll_group.html.erb +++ b/app/views/polls/_poll_group.html.erb @@ -37,7 +37,7 @@ <%= poll_dates(poll) %>
    - <% poll.questions.each do |question| %> + <% poll.questions.sort_for_list.each do |question| %>
  • <%= question.title %>
  • <% end %>
diff --git a/spec/system/polls/polls_spec.rb b/spec/system/polls/polls_spec.rb index 74b5f641c..b0eb0ccaa 100644 --- a/spec/system/polls/polls_spec.rb +++ b/spec/system/polls/polls_spec.rb @@ -173,6 +173,24 @@ describe "Polls" do expect(page).to have_content(proposal_question.title) end + scenario "Questions appear by created at order" do + question = create(:poll_question, poll: poll, title: "First question") + create(:poll_question, poll: poll, title: "Second question") + create(:poll_question, poll: poll, title: "Third question") + + question.update!(title: "First question edited") + + visit polls_path + + expect("First question edited").to appear_before("Second question") + expect("Second question").to appear_before("Third question") + + visit poll_path(poll) + + expect("First question edited").to appear_before("Second question") + expect("Second question").to appear_before("Third question") + end + scenario "Question answers appear in the given order" do question = create(:poll_question, poll: poll) answer1 = create(:poll_question_answer, title: "First", question: question, given_order: 2)