Show polls with the user's geozone first
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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?
|
||||
if answerable_by?(user)
|
||||
50
|
||||
else
|
||||
100
|
||||
end
|
||||
else
|
||||
0
|
||||
end
|
||||
|
||||
@@ -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...")
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user