Merge pull request #4642 from consul/poll_questions

Order polls by user geozone and questions by creation
This commit is contained in:
Javi Martín
2021-10-18 13:50:49 +02:00
committed by GitHub
6 changed files with 60 additions and 13 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -15,7 +15,7 @@
<th><%= t("admin.actions.actions") %></th>
</tr>
</thead>
<% @poll.questions.each do |question| %>
<% @poll.questions.sort_for_list.each do |question| %>
<tr id="<%= dom_id(question) %>">
<td>
<strong><%= question.title %></strong>

View File

@@ -37,7 +37,7 @@
<%= poll_dates(poll) %>
<ul class="margin-top">
<% poll.questions.each do |question| %>
<% poll.questions.sort_for_list.each do |question| %>
<li><%= question.title %></li>
<% end %>
</ul>

View File

@@ -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...")

View File

@@ -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