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 a8f2b6810..48cffaace 100644 --- a/app/models/poll.rb +++ b/app/models/poll.rb @@ -51,17 +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| - 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(user), poll.starts_at, poll.name] <=> [another_poll.weight(user), another_poll.starts_at, another_poll.name] end end @@ -190,4 +182,16 @@ class Poll < ApplicationRecord def self.search(terms) pg_search(terms) end + + def weight(user) + if geozone_restricted? + if answerable_by?(user) + 50 + else + 100 + end + else + 0 + end + end end 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) %> 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..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) @@ -420,6 +438,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