Merge pull request #4989 from consul/add_time_to_polls

Allow selecting the time when a poll starts/ends
This commit is contained in:
Javi Martín
2022-09-14 15:31:39 +02:00
committed by GitHub
31 changed files with 312 additions and 203 deletions

View File

@@ -23,6 +23,16 @@
}); });
$(".js-calendar-full").datepicker(); $(".js-calendar-full").datepicker();
if (!App.Datepicker.browser_supports_datetime_local_field()) {
if (App.Datepicker.browser_supports_date_field()) {
$("input[type='datetime-local']").prop("type", "text")
.val(App.Datepicker.datetime_to_date)
.prop("type", "date");
} else {
$("input[type='datetime-local']").val(App.Datepicker.datetime_to_date).datepicker();
}
}
if (!App.Datepicker.browser_supports_date_field()) { if (!App.Datepicker.browser_supports_date_field()) {
$("input[type='date']").datepicker(); $("input[type='date']").datepicker();
} }
@@ -39,11 +49,20 @@
}); });
}, },
browser_supports_date_field: function() { browser_supports_date_field: function() {
var datefield; return App.Datepicker.browser_supports_field_with_type("date");
},
browser_supports_datetime_local_field: function() {
return App.Datepicker.browser_supports_field_with_type("datetime-local");
},
browser_supports_field_with_type: function(field_type) {
var field;
datefield = document.createElement("input"); field = document.createElement("input");
datefield.setAttribute("type", "date"); field.setAttribute("type", field_type);
return datefield.type === "date"; return field.type === field_type;
},
datetime_to_date: function(index, value) {
return value.split("T")[0];
} }
}; };

View File

@@ -12,7 +12,7 @@ class Admin::BudgetPhases::PhasesComponent < ApplicationComponent
end end
def dates(phase) def dates(phase)
Admin::Budgets::DurationComponent.new(phase).dates render Admin::DurationComponent.new(phase)
end end
def enabled_cell(phase) def enabled_cell(phase)

View File

@@ -1,25 +0,0 @@
class Admin::Budgets::DurationComponent < ApplicationComponent
attr_reader :durable
def initialize(durable)
@durable = durable
end
def dates
Admin::DateRangeComponent.new(start_time, end_time).call
end
def duration
distance_of_time_in_words(durable.starts_at, durable.ends_at) if durable.starts_at && durable.ends_at
end
private
def start_time
durable.starts_at
end
def end_time
durable.ends_at - 1.second if durable.ends_at.present?
end
end

View File

@@ -0,0 +1 @@
<%= duration -%>

View File

@@ -0,0 +1,17 @@
class Admin::Budgets::DurationInWordsComponent < ApplicationComponent
attr_reader :durable
def initialize(durable)
@durable = durable
end
def render?
durable.starts_at && durable.ends_at
end
private
def duration
distance_of_time_in_words(durable.starts_at, durable.ends_at)
end
end

View File

@@ -37,11 +37,11 @@ class Admin::Budgets::IndexComponent < ApplicationComponent
end end
def dates(budget) def dates(budget)
Admin::Budgets::DurationComponent.new(budget).dates render Admin::DurationComponent.new(budget)
end end
def duration(budget) def duration(budget)
Admin::Budgets::DurationComponent.new(budget).duration render Admin::Budgets::DurationInWordsComponent.new(budget)
end end
def status_html_class(budget) def status_html_class(budget)

View File

@@ -0,0 +1 @@
<%= dates -%>

View File

@@ -0,0 +1,25 @@
class Admin::DurationComponent < ApplicationComponent
attr_reader :durable
def initialize(durable)
@durable = durable
end
private
def dates
render Admin::DateRangeComponent.new(start_time, end_time)
end
def start_time
durable.starts_at
end
def end_time
if durable.ends_at.present? && durable.ends_at == durable.ends_at.beginning_of_day
durable.ends_at - 1.second
else
durable.ends_at
end
end
end

View File

@@ -42,9 +42,9 @@ class Poll < ApplicationRecord
accepts_nested_attributes_for :questions, reject_if: :all_blank, allow_destroy: true accepts_nested_attributes_for :questions, reject_if: :all_blank, allow_destroy: true
scope :for, ->(element) { where(related: element) } scope :for, ->(element) { where(related: element) }
scope :current, -> { where("starts_at <= ? and ? <= ends_at", Date.current.beginning_of_day, Date.current.beginning_of_day) } scope :current, -> { where("starts_at <= :time and ends_at >= :time", time: Time.current) }
scope :expired, -> { where("ends_at < ?", Date.current.beginning_of_day) } scope :expired, -> { where("ends_at < ?", Time.current) }
scope :recounting, -> { where(ends_at: (Date.current.beginning_of_day - RECOUNT_DURATION)..Date.current.beginning_of_day) } scope :recounting, -> { where(ends_at: (RECOUNT_DURATION.ago)...Time.current) }
scope :published, -> { where(published: true) } scope :published, -> { where(published: true) }
scope :by_geozone_id, ->(geozone_id) { where(geozones: { id: geozone_id }.joins(:geozones)) } scope :by_geozone_id, ->(geozone_id) { where(geozones: { id: geozone_id }.joins(:geozones)) }
scope :public_for_api, -> { all } scope :public_for_api, -> { all }
@@ -67,11 +67,11 @@ class Poll < ApplicationRecord
name name
end end
def current?(timestamp = Date.current.beginning_of_day) def current?(timestamp = Time.current)
starts_at <= timestamp && timestamp <= ends_at starts_at <= timestamp && timestamp <= ends_at
end end
def expired?(timestamp = Date.current.beginning_of_day) def expired?(timestamp = Time.current)
ends_at < timestamp ends_at < timestamp
end end

View File

@@ -6,11 +6,11 @@
<div class="row"> <div class="row">
<div class="clear"> <div class="clear">
<div class="small-12 medium-6 column"> <div class="small-12 medium-6 column">
<%= f.date_field :starts_at %> <%= f.datetime_field :starts_at %>
</div> </div>
<div class="small-12 medium-6 column"> <div class="small-12 medium-6 column">
<%= f.date_field :ends_at %> <%= f.datetime_field :ends_at %>
</div> </div>
</div> </div>
</div> </div>

View File

@@ -1,7 +1,7 @@
<tr id="<%= dom_id(poll) %>" class="poll"> <tr id="<%= dom_id(poll) %>" class="poll">
<td><strong><%= poll.name %></strong></td> <td><strong><%= poll.name %></strong></td>
<td class="text-center"><%= l poll.starts_at.to_date %></td> <td class="text-center"><%= l poll.starts_at, format: :short_datetime %></td>
<td class="text-center"><%= l poll.ends_at.to_date %></td> <td class="text-center"><%= l poll.ends_at, format: :short_datetime %></td>
<% if feature?(:sdg) %> <% if feature?(:sdg) %>
<td class="text-center"><%= poll.sdg_goal_list %></td> <td class="text-center"><%= poll.sdg_goal_list %></td>
<td class="text-center"><%= poll.sdg_target_list %></td> <td class="text-center"><%= poll.sdg_target_list %></td>

View File

@@ -12,7 +12,7 @@
<div class="inline-block"> <div class="inline-block">
<strong><%= t("admin.polls.index.dates") %></strong> <strong><%= t("admin.polls.index.dates") %></strong>
<br> <br>
<%= l @poll.starts_at.to_date %> - <%= l @poll.ends_at.to_date %> <%= render Admin::DurationComponent.new(@poll) %>
</div> </div>
<% if @poll.geozone_restricted %> <% if @poll.geozone_restricted %>

View File

@@ -11,6 +11,7 @@ namespace :consul do
desc "Runs tasks needed to upgrade from 1.5.0 to 1.6.0" desc "Runs tasks needed to upgrade from 1.5.0 to 1.6.0"
task "execute_release_1.6.0_tasks": [ task "execute_release_1.6.0_tasks": [
"db:calculate_tsv" "db:calculate_tsv",
"polls:set_ends_at_to_end_of_day"
] ]
end end

10
lib/tasks/polls.rake Normal file
View File

@@ -0,0 +1,10 @@
namespace :polls do
desc "Changes the polls ending date to the end of the day"
task set_ends_at_to_end_of_day: :environment do
ApplicationLogger.new.info "Adding time to the date where a poll ends"
Poll.find_each do |poll|
poll.update_column :ends_at, poll.ends_at.end_of_day.beginning_of_minute
end
end
end

View File

@@ -1,73 +0,0 @@
require "rails_helper"
describe Admin::Budgets::DurationComponent do
describe "#dates" do
it "shows both dates when both are defined" do
durable = double(
starts_at: Time.zone.local(2015, 8, 1, 12, 0, 0),
ends_at: Time.zone.local(2016, 9, 30, 16, 30, 00)
)
dates = Admin::Budgets::DurationComponent.new(durable).dates
render dates
expect(page.text).to eq "2015-08-01 12:00 - 2016-09-30 16:29"
expect(dates).to be_html_safe
end
it "shows the start date when no end date is defined" do
durable = double(starts_at: Time.zone.local(2015, 8, 1, 12, 0, 0), ends_at: nil)
render Admin::Budgets::DurationComponent.new(durable).dates
expect(page.text).to eq "2015-08-01 12:00 - "
end
it "shows the end date when no start date is defined" do
durable = double(starts_at: nil, ends_at: Time.zone.local(2016, 9, 30, 16, 30, 00))
render Admin::Budgets::DurationComponent.new(durable).dates
expect(page.text).to eq "- 2016-09-30 16:29"
end
end
describe "#duration" do
it "describes the total duration in human language" do
durable = double(
starts_at: Time.zone.local(2015, 8, 1, 12, 0, 0),
ends_at: Time.zone.local(2016, 9, 30, 16, 30, 00)
)
render Admin::Budgets::DurationComponent.new(durable).duration
expect(page.text).to eq "about 1 year"
end
it "is not defined when no end date is defined" do
durable = double(starts_at: Time.zone.local(2015, 8, 1, 12, 0, 0), ends_at: nil)
render Admin::Budgets::DurationComponent.new(durable).duration
expect(page).not_to be_rendered
end
it "is not defined when no start date is defined" do
durable = double(starts_at: nil, ends_at: Time.zone.local(2016, 9, 30, 16, 30, 00))
render Admin::Budgets::DurationComponent.new(durable).duration
expect(page).not_to be_rendered
end
end
attr_reader :content
def render(content)
@content = content
end
def page
Capybara::Node::Simple.new(content.to_s)
end
end

View File

@@ -0,0 +1,30 @@
require "rails_helper"
describe Admin::Budgets::DurationInWordsComponent do
it "describes the total duration in human language" do
durable = double(
starts_at: Time.zone.local(2015, 8, 1, 12, 0, 0),
ends_at: Time.zone.local(2016, 9, 30, 16, 30, 00)
)
render_inline Admin::Budgets::DurationInWordsComponent.new(durable)
expect(page.text).to eq "about 1 year"
end
it "is not defined when no end date is defined" do
durable = double(starts_at: Time.zone.local(2015, 8, 1, 12, 0, 0), ends_at: nil)
render_inline Admin::Budgets::DurationInWordsComponent.new(durable)
expect(page).not_to be_rendered
end
it "is not defined when no start date is defined" do
durable = double(starts_at: nil, ends_at: Time.zone.local(2016, 9, 30, 16, 30, 00))
render_inline Admin::Budgets::DurationInWordsComponent.new(durable)
expect(page).not_to be_rendered
end
end

View File

@@ -0,0 +1,43 @@
require "rails_helper"
describe Admin::DurationComponent do
it "shows both dates when both are defined" do
durable = double(
starts_at: Time.zone.local(2015, 8, 1, 12, 0, 0),
ends_at: Time.zone.local(2016, 9, 30, 16, 30, 00)
)
render_inline Admin::DurationComponent.new(durable)
expect(page.text).to eq "2015-08-01 12:00 - 2016-09-30 16:30"
expect(page).to have_css "time", exact_text: "2015-08-01 12:00"
expect(page).to have_css "time", exact_text: "2016-09-30 16:30"
end
it "shows the moment before the end date when it ends at midnight" do
durable = double(
starts_at: Time.zone.local(2015, 8, 1, 12, 0, 0),
ends_at: Time.zone.local(2016, 9, 30, 00, 00, 00)
)
render_inline Admin::DurationComponent.new(durable)
expect(page.text).to eq "2015-08-01 12:00 - 2016-09-29 23:59"
end
it "shows the start date when no end date is defined" do
durable = double(starts_at: Time.zone.local(2015, 8, 1, 12, 0, 0), ends_at: nil)
render_inline Admin::DurationComponent.new(durable)
expect(page.text).to eq "2015-08-01 12:00 - "
end
it "shows the end date when no start date is defined" do
durable = double(starts_at: nil, ends_at: Time.zone.local(2016, 9, 30, 16, 30, 00))
render_inline Admin::DurationComponent.new(durable)
expect(page.text).to eq "- 2016-09-30 16:30"
end
end

View File

@@ -7,11 +7,6 @@ FactoryBot.define do
starts_at { 1.month.ago } starts_at { 1.month.ago }
ends_at { 1.month.from_now } ends_at { 1.month.from_now }
trait :current do
starts_at { 2.days.ago }
ends_at { 2.days.from_now }
end
trait :expired do trait :expired do
starts_at { 1.month.ago } starts_at { 1.month.ago }
ends_at { 15.days.ago } ends_at { 15.days.ago }
@@ -22,11 +17,6 @@ FactoryBot.define do
ends_at { 2.months.ago } ends_at { 2.months.ago }
end end
trait :recounting do
starts_at { 1.month.ago }
ends_at { Date.current }
end
trait :published do trait :published do
published { true } published { true }
end end

View File

@@ -0,0 +1,23 @@
require "rails_helper"
describe "rake polls:set_ends_at_to_end_of_day" do
before { Rake::Task["polls:set_ends_at_to_end_of_day"].reenable }
let :run_rake_task do
Rake.application.invoke_task("polls:set_ends_at_to_end_of_day")
end
it "updates existing polls" do
travel_to(Time.zone.local(2015, 7, 15, 13, 32, 13))
poll = create(:poll, ends_at: 2.years.from_now)
date_poll = create(:poll, ends_at: 3.years.from_now.to_date)
expect(I18n.l(poll.reload.ends_at, format: :datetime)).to eq "2017-07-15 13:32:13"
expect(I18n.l(date_poll.reload.ends_at, format: :datetime)).to eq "2018-07-15 00:00:00"
run_rake_task
expect(I18n.l(poll.reload.ends_at, format: :datetime)).to eq "2017-07-15 23:59:00"
expect(I18n.l(date_poll.reload.ends_at, format: :datetime)).to eq "2018-07-15 23:59:00"
end
end

View File

@@ -33,12 +33,12 @@ describe Abilities::Everyone do
it { should be_able_to(:results, create(:poll, :expired, results_enabled: true)) } it { should be_able_to(:results, create(:poll, :expired, results_enabled: true)) }
it { should_not be_able_to(:results, create(:poll, :expired, results_enabled: false)) } it { should_not be_able_to(:results, create(:poll, :expired, results_enabled: false)) }
it { should_not be_able_to(:results, create(:poll, :current, results_enabled: true)) } it { should_not be_able_to(:results, create(:poll, results_enabled: true)) }
it { should_not be_able_to(:results, create(:poll, :for_budget, :expired, results_enabled: true)) } it { should_not be_able_to(:results, create(:poll, :for_budget, :expired, results_enabled: true)) }
it { should be_able_to(:stats, create(:poll, :expired, stats_enabled: true)) } it { should be_able_to(:stats, create(:poll, :expired, stats_enabled: true)) }
it { should_not be_able_to(:stats, create(:poll, :expired, stats_enabled: false)) } it { should_not be_able_to(:stats, create(:poll, :expired, stats_enabled: false)) }
it { should_not be_able_to(:stats, create(:poll, :current, stats_enabled: true)) } it { should_not be_able_to(:stats, create(:poll, stats_enabled: true)) }
it { should_not be_able_to(:stats, create(:poll, :for_budget, :expired, stats_enabled: true)) } it { should_not be_able_to(:stats, create(:poll, :for_budget, :expired, stats_enabled: true)) }
it { should be_able_to(:read_results, create(:budget, :finished, results_enabled: true)) } it { should be_able_to(:read_results, create(:budget, :finished, results_enabled: true)) }

View File

@@ -45,7 +45,7 @@ describe Poll::Booth do
describe ".available" do describe ".available" do
it "returns booths associated to current polls" do it "returns booths associated to current polls" do
booth_for_current_poll = create(:poll_booth, polls: [create(:poll, :current)]) booth_for_current_poll = create(:poll_booth, polls: [create(:poll)])
booth_for_expired_poll = create(:poll_booth, polls: [create(:poll, :expired)]) booth_for_expired_poll = create(:poll_booth, polls: [create(:poll, :expired)])
expect(Poll::Booth.available).to eq [booth_for_current_poll] expect(Poll::Booth.available).to eq [booth_for_current_poll]
@@ -53,7 +53,7 @@ describe Poll::Booth do
end end
it "returns polls with multiple translations only once" do it "returns polls with multiple translations only once" do
create(:poll_booth, polls: [create(:poll, :current, name: "English", name_es: "Spanish")]) create(:poll_booth, polls: [create(:poll, name: "English", name_es: "Spanish")])
expect(Poll::Booth.available.count).to eq 1 expect(Poll::Booth.available.count).to eq 1
end end

View File

@@ -86,17 +86,31 @@ describe Poll do
end end
end end
describe "#opened?" do describe "#current?", :with_frozen_time do
it "returns true only when it isn't too late" do it "returns true only when it isn't too late" do
expect(create(:poll, :expired)).not_to be_current about_to_start = create(:poll, starts_at: 1.second.from_now)
expect(create(:poll)).to be_current just_started = create(:poll, starts_at: Time.current)
about_to_end = create(:poll, ends_at: Time.current)
just_ended = create(:poll, ends_at: 1.second.ago)
expect(just_started).to be_current
expect(about_to_end).to be_current
expect(about_to_start).not_to be_current
expect(just_ended).not_to be_current
end end
end end
describe "#expired?" do describe "#expired?", :with_frozen_time do
it "returns true only when it is too late" do it "returns true only when it is too late" do
expect(create(:poll, :expired)).to be_expired about_to_start = create(:poll, starts_at: 1.second.from_now)
expect(create(:poll)).not_to be_expired about_to_end = create(:poll, ends_at: Time.current)
just_ended = create(:poll, ends_at: 1.second.ago)
recounting_ended = create(:poll, starts_at: 3.years.ago, ends_at: 2.years.ago)
expect(just_ended).to be_expired
expect(recounting_ended).to be_expired
expect(about_to_start).not_to be_expired
expect(about_to_end).not_to be_expired
end end
end end
@@ -107,33 +121,6 @@ describe Poll do
end end
end end
describe "#recounting" do
it "returns polls in recount & scrutiny phase" do
current = create(:poll, :current)
expired = create(:poll, :expired)
recounting = create(:poll, :recounting)
recounting_polls = Poll.recounting
expect(recounting_polls).to eq [recounting]
expect(recounting_polls).not_to include(current)
expect(recounting_polls).not_to include(expired)
end
end
describe "#current_or_recounting" do
it "returns current or recounting polls" do
current = create(:poll, :current)
expired = create(:poll, :expired)
recounting = create(:poll, :recounting)
current_or_recounting = Poll.current_or_recounting
expect(current_or_recounting).to match_array [current, recounting]
expect(current_or_recounting).not_to include(expired)
end
end
describe "answerable_by" do describe "answerable_by" do
let(:geozone) { create(:geozone) } let(:geozone) { create(:geozone) }
@@ -198,7 +185,7 @@ describe Poll do
end end
end end
describe "votable_by" do describe ".votable_by" do
it "returns polls that have not been voted by a user" do it "returns polls that have not been voted by a user" do
user = create(:user, :level_two) user = create(:user, :level_two)
@@ -337,8 +324,70 @@ describe Poll do
end end
end end
context "scopes" do describe "scopes" do
describe "#not_budget" do describe ".current", :with_frozen_time do
it "returns polls which have started but not ended" do
about_to_start = create(:poll, starts_at: 1.second.from_now)
just_started = create(:poll, starts_at: Time.current)
about_to_end = create(:poll, ends_at: Time.current)
just_ended = create(:poll, ends_at: 1.second.ago)
current_polls = Poll.current
expect(current_polls).to match_array [just_started, about_to_end]
expect(current_polls).not_to include(about_to_start)
expect(current_polls).not_to include(just_ended)
end
end
describe ".expired", :with_frozen_time do
it "returns polls which have already ended" do
about_to_start = create(:poll, starts_at: 1.second.from_now)
about_to_end = create(:poll, ends_at: Time.current)
just_ended = create(:poll, ends_at: 1.second.ago)
recounting_ended = create(:poll, starts_at: 3.years.ago, ends_at: 2.years.ago)
expired_polls = Poll.expired
expect(expired_polls).to match_array [just_ended, recounting_ended]
expect(expired_polls).not_to include(about_to_start)
expect(expired_polls).not_to include(about_to_end)
end
end
describe ".recounting", :with_frozen_time do
it "returns polls in recount & scrutiny phase" do
about_to_start = create(:poll, starts_at: 1.second.from_now)
about_to_end = create(:poll, ends_at: Time.current)
just_ended = create(:poll, ends_at: 1.second.ago)
recounting_ended = create(:poll, starts_at: 3.years.ago, ends_at: 2.years.ago)
recounting_polls = Poll.recounting
expect(recounting_polls).to eq [just_ended]
expect(recounting_polls).not_to include(about_to_start)
expect(recounting_polls).not_to include(about_to_end)
expect(recounting_polls).not_to include(recounting_ended)
end
end
describe ".current_or_recounting", :with_frozen_time do
it "returns current or recounting polls" do
about_to_start = create(:poll, starts_at: 1.second.from_now)
just_started = create(:poll, starts_at: Time.current)
about_to_end = create(:poll, ends_at: Time.current)
just_ended = create(:poll, ends_at: 1.second.ago)
recounting_ended = create(:poll, starts_at: 3.years.ago, ends_at: 2.years.ago)
current_or_recounting = Poll.current_or_recounting
expect(current_or_recounting).to match_array [just_started, about_to_end, just_ended]
expect(current_or_recounting).not_to include(about_to_start)
expect(current_or_recounting).not_to include(recounting_ended)
end
end
describe ".not_budget" do
it "returns polls not associated to a budget" do it "returns polls not associated to a budget" do
poll1 = create(:poll) poll1 = create(:poll)
poll2 = create(:poll) poll2 = create(:poll)
@@ -350,7 +399,7 @@ describe Poll do
end end
end end
describe "#sort_for_list" do describe ".sort_for_list" do
it "returns polls sorted by name ASC" do it "returns polls sorted by name ASC" do
starts_at = 1.day.from_now starts_at = 1.day.from_now
poll1 = create(:poll, geozone_restricted: true, starts_at: starts_at, name: "Zzz...") poll1 = create(:poll, geozone_restricted: true, starts_at: starts_at, name: "Zzz...")
@@ -419,13 +468,13 @@ describe Poll do
describe "#recounts_confirmed" do describe "#recounts_confirmed" do
it "is false for current polls" do it "is false for current polls" do
poll = create(:poll, :current) poll = create(:poll)
expect(poll.recounts_confirmed?).to be false expect(poll.recounts_confirmed?).to be false
end end
it "is false for recounting polls" do it "is false for recounting polls" do
poll = create(:poll, :recounting) poll = create(:poll, ends_at: 1.second.ago)
expect(poll.recounts_confirmed?).to be false expect(poll.recounts_confirmed?).to be false
end end

View File

@@ -32,7 +32,7 @@ describe "Admin booths", :admin do
end end
scenario "Available" do scenario "Available" do
booth_for_current_poll = create(:poll_booth, polls: [create(:poll, :current)]) booth_for_current_poll = create(:poll_booth, polls: [create(:poll)])
booth_for_expired_poll = create(:poll_booth, polls: [create(:poll, :expired)]) booth_for_expired_poll = create(:poll_booth, polls: [create(:poll, :expired)])
visit admin_root_path visit admin_root_path
@@ -74,7 +74,7 @@ describe "Admin booths", :admin do
end end
scenario "Edit" do scenario "Edit" do
poll = create(:poll, :current) poll = create(:poll)
booth = create(:poll_booth, polls: [poll]) booth = create(:poll_booth, polls: [poll])
visit admin_booths_path visit admin_booths_path
@@ -99,7 +99,7 @@ describe "Admin booths", :admin do
end end
scenario "Back link go back to available list when manage shifts" do scenario "Back link go back to available list when manage shifts" do
poll = create(:poll, :current) poll = create(:poll)
booth = create(:poll_booth, polls: [poll]) booth = create(:poll_booth, polls: [poll])
visit available_admin_booths_path visit available_admin_booths_path

View File

@@ -47,15 +47,14 @@ describe "Admin polls", :admin do
end end
scenario "Create" do scenario "Create" do
travel_to(Time.zone.local(2015, 7, 15, 13, 32, 13))
visit admin_polls_path visit admin_polls_path
click_link "Create poll" click_link "Create poll"
start_date = 1.week.from_now.to_date
end_date = 2.weeks.from_now.to_date
fill_in "Name", with: "Upcoming poll" fill_in "Name", with: "Upcoming poll"
fill_in "poll_starts_at", with: start_date fill_in "Start Date", with: 1.week.from_now
fill_in "poll_ends_at", with: end_date fill_in "Closing Date", with: 2.weeks.from_now
fill_in "Summary", with: "Upcoming poll's summary. This poll..." fill_in "Summary", with: "Upcoming poll's summary. This poll..."
fill_in "Description", with: "Upcomming poll's description. This poll..." fill_in "Description", with: "Upcomming poll's description. This poll..."
@@ -67,8 +66,8 @@ describe "Admin polls", :admin do
expect(page).to have_content "Poll created successfully" expect(page).to have_content "Poll created successfully"
expect(page).to have_content "Upcoming poll" expect(page).to have_content "Upcoming poll"
expect(page).to have_content I18n.l(start_date) expect(page).to have_content "2015-07-22 13:32"
expect(page).to have_content I18n.l(end_date) expect(page).to have_content "2015-07-29 13:32"
visit poll_path(id: "upcoming-poll") visit poll_path(id: "upcoming-poll")
@@ -76,24 +75,23 @@ describe "Admin polls", :admin do
end end
scenario "Edit" do scenario "Edit" do
poll = create(:poll, :with_image) travel_to(Time.zone.local(2015, 7, 15, 13, 32, 13))
poll = create(:poll, :with_image, ends_at: 1.month.from_now.beginning_of_minute)
visit admin_poll_path(poll) visit admin_poll_path(poll)
click_link "Edit poll" click_link "Edit poll"
end_date = 1.year.from_now.to_date
expect(page).to have_css("img[alt='#{poll.image.title}']") expect(page).to have_css("img[alt='#{poll.image.title}']")
expect(page).to have_link "Go back", href: admin_polls_path expect(page).to have_link "Go back", href: admin_polls_path
fill_in "Name", with: "Next Poll" fill_in "Name", with: "Next Poll"
fill_in "poll_ends_at", with: end_date fill_in "Closing Date", with: 1.year.from_now
click_button "Update poll" click_button "Update poll"
expect(page).to have_content "Poll updated successfully" expect(page).to have_content "Poll updated successfully"
expect(page).to have_content "Next Poll" expect(page).to have_content "Next Poll"
expect(page).to have_content I18n.l(end_date.to_date) expect(page).to have_content "2016-07-15 13:32"
end end
scenario "Edit from index" do scenario "Edit from index" do

View File

@@ -27,7 +27,7 @@ describe "Admin shifts", :admin do
scenario "Create Vote Collection Shift and Recount & Scrutiny Shift on same date" do scenario "Create Vote Collection Shift and Recount & Scrutiny Shift on same date" do
create(:poll) create(:poll)
poll = create(:poll, :current) poll = create(:poll)
booth = create(:poll_booth, polls: [poll, create(:poll, :expired)]) booth = create(:poll_booth, polls: [poll, create(:poll, :expired)])
officer = create(:poll_officer) officer = create(:poll_officer)
vote_collection_dates = (Date.current..poll.ends_at.to_date).to_a.map { |date| I18n.l(date, format: :long) } vote_collection_dates = (Date.current..poll.ends_at.to_date).to_a.map { |date| I18n.l(date, format: :long) }
@@ -89,7 +89,7 @@ describe "Admin shifts", :admin do
end end
scenario "Vote Collection Shift and Recount & Scrutiny Shift don't include already assigned dates to officer" do scenario "Vote Collection Shift and Recount & Scrutiny Shift don't include already assigned dates to officer" do
poll = create(:poll, :current) poll = create(:poll)
booth = create(:poll_booth, polls: [poll]) booth = create(:poll_booth, polls: [poll])
officer = create(:poll_officer) officer = create(:poll_officer)
@@ -137,7 +137,7 @@ describe "Admin shifts", :admin do
end end
scenario "Error on create" do scenario "Error on create" do
poll = create(:poll, :current) poll = create(:poll)
booth = create(:poll_booth, polls: [poll]) booth = create(:poll_booth, polls: [poll])
officer = create(:poll_officer) officer = create(:poll_officer)
@@ -158,7 +158,7 @@ describe "Admin shifts", :admin do
end end
scenario "Destroy" do scenario "Destroy" do
poll = create(:poll, :current) poll = create(:poll)
booth = create(:poll_booth, polls: [poll]) booth = create(:poll_booth, polls: [poll])
officer = create(:poll_officer) officer = create(:poll_officer)

View File

@@ -12,8 +12,8 @@ describe "Admin Budgets", :admin do
expect(page).to have_current_path(/admin\/polls\/\d+/) expect(page).to have_current_path(/admin\/polls\/\d+/)
expect(page).to have_content(budget.name) expect(page).to have_content(budget.name)
expect(page).to have_content(balloting_phase.starts_at.to_date) expect(page).to have_content("#{balloting_phase.starts_at.to_date} 00:00")
expect(page).to have_content(balloting_phase.ends_at.to_date) expect(page).to have_content("#{balloting_phase.ends_at.to_date - 1.day} 23:59")
end end
scenario "Create poll in current locale if the budget does not have a poll associated" do scenario "Create poll in current locale if the budget does not have a poll associated" do

View File

@@ -3,7 +3,7 @@ require "rails_helper"
describe "BudgetPolls", :with_frozen_time do describe "BudgetPolls", :with_frozen_time do
let(:budget) { create(:budget, :balloting) } let(:budget) { create(:budget, :balloting) }
let(:investment) { create(:budget_investment, :selected, budget: budget) } let(:investment) { create(:budget_investment, :selected, budget: budget) }
let(:poll) { create(:poll, :current, budget: budget) } let(:poll) { create(:poll, budget: budget) }
let(:booth) { create(:poll_booth) } let(:booth) { create(:poll_booth) }
let(:officer) { create(:poll_officer) } let(:officer) { create(:poll_officer) }
let(:admin) { create(:administrator) } let(:admin) { create(:administrator) }

View File

@@ -124,7 +124,7 @@ describe "Polls" do
end end
scenario "Edit poll is not allowed for current polls" do scenario "Edit poll is not allowed for current polls" do
poll = create(:poll, :current, related: proposal) poll = create(:poll, related: proposal)
visit proposal_dashboard_polls_path(proposal) visit proposal_dashboard_polls_path(proposal)
@@ -232,7 +232,7 @@ describe "Polls" do
end end
scenario "View results available for current polls" do scenario "View results available for current polls" do
poll = create(:poll, :current, related: proposal) poll = create(:poll, related: proposal)
visit proposal_dashboard_polls_path(proposal) visit proposal_dashboard_polls_path(proposal)

View File

@@ -1,7 +1,7 @@
require "rails_helper" require "rails_helper"
describe "Voters" do describe "Voters" do
let(:poll) { create(:poll, :current) } let(:poll) { create(:poll) }
let(:booth) { create(:poll_booth) } let(:booth) { create(:poll_booth) }
let(:officer) { create(:poll_officer) } let(:officer) { create(:poll_officer) }
@@ -35,7 +35,7 @@ describe "Voters" do
end end
scenario "Cannot vote" do scenario "Cannot vote" do
unvotable_poll = create(:poll, :current, geozone_restricted: true, geozones: [create(:geozone, census_code: "02")]) unvotable_poll = create(:poll, geozone_restricted: true, geozones: [create(:geozone, census_code: "02")])
create(:poll_officer_assignment, officer: officer, poll: unvotable_poll, booth: booth) create(:poll_officer_assignment, officer: officer, poll: unvotable_poll, booth: booth)
set_officing_booth(booth) set_officing_booth(booth)
@@ -49,7 +49,7 @@ describe "Voters" do
end end
scenario "Already voted" do scenario "Already voted" do
poll2 = create(:poll, :current) poll2 = create(:poll)
create(:poll_officer_assignment, officer: officer, poll: poll2, booth: booth) create(:poll_officer_assignment, officer: officer, poll: poll2, booth: booth)
user = create(:user, :level_two) user = create(:user, :level_two)
@@ -84,7 +84,7 @@ describe "Voters" do
context "Polls displayed to officers" do context "Polls displayed to officers" do
scenario "Display current polls assigned to a booth" do scenario "Display current polls assigned to a booth" do
poll = create(:poll, :current) poll = create(:poll)
create(:poll_officer_assignment, officer: officer, poll: poll, booth: booth) create(:poll_officer_assignment, officer: officer, poll: poll, booth: booth)
set_officing_booth(booth) set_officing_booth(booth)
@@ -96,7 +96,7 @@ describe "Voters" do
end end
scenario "Display polls that the user can vote" do scenario "Display polls that the user can vote" do
votable_poll = create(:poll, :current, geozone_restricted: true, geozones: [Geozone.first]) votable_poll = create(:poll, geozone_restricted: true, geozones: [Geozone.first])
create(:poll_officer_assignment, officer: officer, poll: votable_poll, booth: booth) create(:poll_officer_assignment, officer: officer, poll: votable_poll, booth: booth)
set_officing_booth(booth) set_officing_booth(booth)
@@ -108,7 +108,7 @@ describe "Voters" do
end end
scenario "Display polls that the user cannot vote" do scenario "Display polls that the user cannot vote" do
unvotable_poll = create(:poll, :current, geozone_restricted: true, geozones: [create(:geozone, census_code: "02")]) unvotable_poll = create(:poll, geozone_restricted: true, geozones: [create(:geozone, census_code: "02")])
create(:poll_officer_assignment, officer: officer, poll: unvotable_poll, booth: booth) create(:poll_officer_assignment, officer: officer, poll: unvotable_poll, booth: booth)
set_officing_booth(booth) set_officing_booth(booth)
@@ -132,8 +132,8 @@ describe "Voters" do
end end
scenario "Do not display polls from other booths" do scenario "Do not display polls from other booths" do
poll1 = create(:poll, :current) poll1 = create(:poll)
poll2 = create(:poll, :current) poll2 = create(:poll)
booth1 = create(:poll_booth) booth1 = create(:poll_booth)
booth2 = create(:poll_booth) booth2 = create(:poll_booth)

View File

@@ -543,7 +543,7 @@ describe "Polls" do
end end
scenario "Don't show poll results and stats if is not expired" do scenario "Don't show poll results and stats if is not expired" do
poll = create(:poll, :current, results_enabled: true, stats_enabled: true) poll = create(:poll, results_enabled: true, stats_enabled: true)
user = create(:user) user = create(:user)
login_as user login_as user

View File

@@ -2,7 +2,7 @@ require "rails_helper"
describe "Voter" do describe "Voter" do
context "Origin", :with_frozen_time do context "Origin", :with_frozen_time do
let(:poll) { create(:poll, :current) } let(:poll) { create(:poll) }
let(:question) { create(:poll_question, poll: poll) } let(:question) { create(:poll_question, poll: poll) }
let(:booth) { create(:poll_booth) } let(:booth) { create(:poll_booth) }
let(:officer) { create(:poll_officer) } let(:officer) { create(:poll_officer) }