From 35a5136d9ff3af1f71f83221921ad42cf7064a18 Mon Sep 17 00:00:00 2001 From: kikito Date: Thu, 3 Nov 2016 17:52:15 +0100 Subject: [PATCH] Implements filtering in polls/index --- app/controllers/polls_controller.rb | 4 ++- app/models/poll.rb | 2 ++ app/views/polls/index.html.erb | 4 +++ spec/features/polls_spec.rb | 46 ++++++++++++++--------------- 4 files changed, 32 insertions(+), 24 deletions(-) diff --git a/app/controllers/polls_controller.rb b/app/controllers/polls_controller.rb index f6f155106..7433ea95b 100644 --- a/app/controllers/polls_controller.rb +++ b/app/controllers/polls_controller.rb @@ -2,8 +2,10 @@ class PollsController < ApplicationController load_and_authorize_resource - def index + has_filters %w{current expired incoming} + def index + @polls = @polls.send(@current_filter).sort_for_list.page(params[:page]) end def show diff --git a/app/models/poll.rb b/app/models/poll.rb index cc731f0b5..a4435d321 100644 --- a/app/models/poll.rb +++ b/app/models/poll.rb @@ -9,6 +9,8 @@ class Poll < ActiveRecord::Base scope :incoming, -> { where('? < starts_at', Time.now) } scope :expired, -> { where('ends_at < ?', Time.now) } + scope :sort_for_list, -> { order(:starts_at) } + def current?(timestamp = DateTime.now) starts_at <= timestamp && timestamp <= ends_at end diff --git a/app/views/polls/index.html.erb b/app/views/polls/index.html.erb index 3a1d7f3b5..4b39e09af 100644 --- a/app/views/polls/index.html.erb +++ b/app/views/polls/index.html.erb @@ -1,3 +1,7 @@ +<%= render 'shared/filter_subnav', i18n_namespace: "polls.index" %> + <% @polls.each do |poll| %> <%= link_to poll.name, poll %> <% end %> + +<%= paginate @polls %> diff --git a/spec/features/polls_spec.rb b/spec/features/polls_spec.rb index a226f8a65..4ef146f72 100644 --- a/spec/features/polls_spec.rb +++ b/spec/features/polls_spec.rb @@ -15,40 +15,40 @@ feature 'Polls' do end end - xscenario 'Filtering enquiries' do - create(:poll_question, poll: poll, title: "Open question") - create(:poll_question, :incoming, poll: poll, title: "Incoming question") - create(:poll_question, :expired, poll: poll, title: "Expired question") + scenario 'Filtering polls' do + create(:poll, name: "Current poll") + create(:poll, :incoming, name: "Incoming poll") + create(:poll, :expired, name: "Expired poll") - visit enquiries_path - expect(page).to have_content('Open question') - expect(page).to_not have_content('Incoming question') - expect(page).to_not have_content('Expired question') + visit polls_path + expect(page).to have_content('Current poll') + expect(page).to_not have_content('Incoming poll') + expect(page).to_not have_content('Expired poll') - visit enquiries_path(filter: 'incoming') - expect(page).to_not have_content('Open question') - expect(page).to have_content('Incoming question') - expect(page).to_not have_content('Expired question') + visit polls_path(filter: 'incoming') + expect(page).to_not have_content('Current poll') + expect(page).to have_content('Incoming poll') + expect(page).to_not have_content('Expired poll') - visit enquiries_path(filter: 'expired') - expect(page).to_not have_content('Open question') - expect(page).to_not have_content('Incoming question') - expect(page).to have_content('Expired question') + visit polls_path(filter: 'expired') + expect(page).to_not have_content('Current poll') + expect(page).to_not have_content('Incoming poll') + expect(page).to have_content('Expired poll') end - xscenario "Current filter is properly highlighted" do - visit enquiries_path - expect(page).to_not have_link('Open') + scenario "Current filter is properly highlighted" do + visit polls_path + expect(page).to_not have_link('Current') expect(page).to have_link('Incoming') expect(page).to have_link('Expired') - visit enquiries_path(filter: 'incoming') - expect(page).to have_link('Open') + visit polls_path(filter: 'incoming') + expect(page).to have_link('Current') expect(page).to_not have_link('Incoming') expect(page).to have_link('Expired') - visit enquiries_path(filter: 'expired') - expect(page).to have_link('Open') + visit polls_path(filter: 'expired') + expect(page).to have_link('Current') expect(page).to have_link('Incoming') expect(page).to_not have_link('Expired') end