adds dates validation to polls
This commit is contained in:
@@ -33,7 +33,7 @@ class Admin::Poll::PollsController < Admin::BaseController
|
|||||||
private
|
private
|
||||||
|
|
||||||
def poll_params
|
def poll_params
|
||||||
params.require(:poll).permit(:name)
|
params.require(:poll).permit(:name, :starts_at, :ends_at)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
@@ -8,6 +8,8 @@ class Poll < ActiveRecord::Base
|
|||||||
|
|
||||||
validates :name, presence: true
|
validates :name, presence: true
|
||||||
|
|
||||||
|
validate :date_range
|
||||||
|
|
||||||
scope :current, -> { where('starts_at <= ? and ? <= ends_at', Time.now, Time.now) }
|
scope :current, -> { where('starts_at <= ? and ? <= ends_at', Time.now, Time.now) }
|
||||||
scope :incoming, -> { where('? < starts_at', Time.now) }
|
scope :incoming, -> { where('? < starts_at', Time.now) }
|
||||||
scope :expired, -> { where('ends_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)
|
def document_has_voted?(document_number, document_type)
|
||||||
voters.where(document_number: document_number, document_type: document_type).exists?
|
voters.where(document_number: document_number, document_type: document_type).exists?
|
||||||
end
|
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
|
end
|
||||||
|
|||||||
@@ -9,13 +9,19 @@
|
|||||||
|
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="small-12 medium-6 column">
|
<div class="small-12 medium-6 column">
|
||||||
<label><%= t("admin.polls.form.starts_at") %></label>
|
<%= f.label :starts_at, t("admin.polls.form.starts_at") %>
|
||||||
<input type="date">
|
<%= 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>
|
||||||
|
|
||||||
<div class="small-12 medium-6 column">
|
<div class="small-12 medium-6 column">
|
||||||
<label><%= t("admin.polls.form.ends_at") %></label>
|
<%= f.label :ends_at, t("admin.polls.form.ends_at") %>
|
||||||
<input type="date">
|
<%= 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>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
<%= render "shared/back_link" %>
|
|
||||||
<div class="clear"></div>
|
|
||||||
|
|
||||||
<h2 class="inline-block">
|
<h2 class="inline-block">
|
||||||
<%= @poll.name %>
|
<%= @poll.name %>
|
||||||
|
<small><%= l @poll.starts_at.to_date %> - <%= l @poll.ends_at.to_date %></small>
|
||||||
</h2>
|
</h2>
|
||||||
|
|
||||||
<%= link_to t("admin.actions.edit"),
|
<%= link_to t("admin.actions.edit"),
|
||||||
|
|||||||
@@ -82,6 +82,7 @@ en:
|
|||||||
description: "Description"
|
description: "Description"
|
||||||
external_url: "Link to additional documentation"
|
external_url: "Link to additional documentation"
|
||||||
errors:
|
errors:
|
||||||
|
invalid_date_range: "Invalid date range"
|
||||||
models:
|
models:
|
||||||
user:
|
user:
|
||||||
attributes:
|
attributes:
|
||||||
|
|||||||
@@ -82,6 +82,7 @@ es:
|
|||||||
description: "Descripción"
|
description: "Descripción"
|
||||||
external_url: "Enlace a documentación adicional"
|
external_url: "Enlace a documentación adicional"
|
||||||
errors:
|
errors:
|
||||||
|
invalid_date_range: "El rango de fechas no es válido"
|
||||||
models:
|
models:
|
||||||
user:
|
user:
|
||||||
attributes:
|
attributes:
|
||||||
|
|||||||
@@ -53,6 +53,8 @@ feature 'Admin polls' do
|
|||||||
click_link "Create poll"
|
click_link "Create poll"
|
||||||
|
|
||||||
fill_in "poll_name", with: "Upcoming 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"
|
click_button "Create poll"
|
||||||
|
|
||||||
expect(page).to have_content "Poll created successfully"
|
expect(page).to have_content "Poll created successfully"
|
||||||
|
|||||||
@@ -13,6 +13,22 @@ describe :poll do
|
|||||||
poll.name = nil
|
poll.name = nil
|
||||||
expect(poll).to_not be_valid
|
expect(poll).to_not be_valid
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
describe "#opened?" do
|
describe "#opened?" do
|
||||||
|
|||||||
Reference in New Issue
Block a user