adds dates validation to polls

This commit is contained in:
Juanjo Bazán
2016-12-21 13:50:15 +01:00
parent 04e742ad77
commit bc690748fd
8 changed files with 41 additions and 8 deletions

View File

@@ -33,7 +33,7 @@ class Admin::Poll::PollsController < Admin::BaseController
private
def poll_params
params.require(:poll).permit(:name)
params.require(:poll).permit(:name, :starts_at, :ends_at)
end
end

View File

@@ -8,6 +8,8 @@ class Poll < ActiveRecord::Base
validates :name, presence: true
validate :date_range
scope :current, -> { where('starts_at <= ? and ? <= ends_at', Time.now, Time.now) }
scope :incoming, -> { where('? < starts_at', Time.now) }
scope :expired, -> { where('ends_at < ?', Time.now) }
@@ -38,4 +40,11 @@ class Poll < ActiveRecord::Base
def document_has_voted?(document_number, document_type)
voters.where(document_number: document_number, document_type: document_type).exists?
end
def date_range
unless starts_at.present? && ends_at.present? && starts_at <= ends_at
errors.add(:starts_at, I18n.t('activerecord.errors.invalid_date_range'))
end
end
end

View File

@@ -9,13 +9,19 @@
<div class="row">
<div class="small-12 medium-6 column">
<label><%= t("admin.polls.form.starts_at") %></label>
<input type="date">
<%= f.label :starts_at, t("admin.polls.form.starts_at") %>
<%= f.text_field :starts_at,
label: false,
value: @poll.starts_at.present? ? l(@poll.starts_at.to_date) : nil,
class: "js-calendar-full" %>
</div>
<div class="small-12 medium-6 column">
<label><%= t("admin.polls.form.ends_at") %></label>
<input type="date">
<%= f.label :ends_at, t("admin.polls.form.ends_at") %>
<%= f.text_field :ends_at,
label: false,
value: @poll.ends_at.present? ? l(@poll.ends_at.to_date) : nil,
class: "js-calendar-full" %>
</div>
</div>

View File

@@ -1,8 +1,6 @@
<%= render "shared/back_link" %>
<div class="clear"></div>
<h2 class="inline-block">
<%= @poll.name %>
<small><%= l @poll.starts_at.to_date %> - <%= l @poll.ends_at.to_date %></small>
</h2>
<%= link_to t("admin.actions.edit"),

View File

@@ -82,6 +82,7 @@ en:
description: "Description"
external_url: "Link to additional documentation"
errors:
invalid_date_range: "Invalid date range"
models:
user:
attributes:

View File

@@ -82,6 +82,7 @@ es:
description: "Descripción"
external_url: "Enlace a documentación adicional"
errors:
invalid_date_range: "El rango de fechas no es válido"
models:
user:
attributes:

View File

@@ -53,6 +53,8 @@ feature 'Admin polls' do
click_link "Create poll"
fill_in "poll_name", with: "Upcoming poll"
fill_in 'poll_starts_at', with: 1.week.from_now.strftime("%d/%m/%Y")
fill_in 'poll_ends_at', with: 2.weeks.from_now.strftime("%d/%m/%Y")
click_button "Create poll"
expect(page).to have_content "Poll created successfully"

View File

@@ -13,6 +13,22 @@ describe :poll do
poll.name = nil
expect(poll).to_not be_valid
end
it "should not be valid without a start date" do
poll.starts_at = nil
expect(poll).to_not be_valid
end
it "should not be valid without an end date" do
poll.ends_at = nil
expect(poll).to_not be_valid
end
it "should not be valid without a proper start/end date range" do
poll.starts_at = 1.week.ago
poll.ends_at = 2.months.ago
expect(poll).to_not be_valid
end
end
describe "#opened?" do