diff --git a/app/controllers/admin/poll/polls_controller.rb b/app/controllers/admin/poll/polls_controller.rb index 98d74d24f..5eca311a7 100644 --- a/app/controllers/admin/poll/polls_controller.rb +++ b/app/controllers/admin/poll/polls_controller.rb @@ -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 \ No newline at end of file diff --git a/app/models/poll.rb b/app/models/poll.rb index ceb10d989..4c2fcecb8 100644 --- a/app/models/poll.rb +++ b/app/models/poll.rb @@ -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 diff --git a/app/views/admin/poll/polls/_form.html.erb b/app/views/admin/poll/polls/_form.html.erb index 9926c679a..8de8af670 100644 --- a/app/views/admin/poll/polls/_form.html.erb +++ b/app/views/admin/poll/polls/_form.html.erb @@ -9,13 +9,19 @@
- - + <%= 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" %>
- - + <%= 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" %>
diff --git a/app/views/admin/poll/polls/show.html.erb b/app/views/admin/poll/polls/show.html.erb index 5317b1f0a..8bb975729 100644 --- a/app/views/admin/poll/polls/show.html.erb +++ b/app/views/admin/poll/polls/show.html.erb @@ -1,8 +1,6 @@ -<%= render "shared/back_link" %> -
-

<%= @poll.name %> + <%= l @poll.starts_at.to_date %> - <%= l @poll.ends_at.to_date %>

<%= link_to t("admin.actions.edit"), diff --git a/config/locales/activerecord.en.yml b/config/locales/activerecord.en.yml index b0f06f7df..11e8442e2 100644 --- a/config/locales/activerecord.en.yml +++ b/config/locales/activerecord.en.yml @@ -82,6 +82,7 @@ en: description: "Description" external_url: "Link to additional documentation" errors: + invalid_date_range: "Invalid date range" models: user: attributes: diff --git a/config/locales/activerecord.es.yml b/config/locales/activerecord.es.yml index 2349e9272..8b3407d53 100644 --- a/config/locales/activerecord.es.yml +++ b/config/locales/activerecord.es.yml @@ -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: diff --git a/spec/features/admin/poll/polls_spec.rb b/spec/features/admin/poll/polls_spec.rb index 5095f36d0..95dc85fde 100644 --- a/spec/features/admin/poll/polls_spec.rb +++ b/spec/features/admin/poll/polls_spec.rb @@ -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" diff --git a/spec/models/poll/poll_spec.rb b/spec/models/poll/poll_spec.rb index cc6f2c530..1ce3f95e0 100644 --- a/spec/models/poll/poll_spec.rb +++ b/spec/models/poll/poll_spec.rb @@ -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