From 8dc7285681495646c2a08c141a42fac3a31afca2 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Mon, 19 Sep 2016 18:22:35 +0200 Subject: [PATCH] validates user has not already voted --- app/models/poll.rb | 4 +++- app/models/poll/booth.rb | 6 ++++++ app/models/poll/voter.rb | 11 +++++++++++ config/locales/activerecord.en.yml | 1 + config/locales/activerecord.es.yml | 1 + db/migrate/20160914172535_create_poll_booths.rb | 8 ++++++++ 6 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 app/models/poll/booth.rb create mode 100644 db/migrate/20160914172535_create_poll_booths.rb diff --git a/app/models/poll.rb b/app/models/poll.rb index d815cba49..5a429c49d 100644 --- a/app/models/poll.rb +++ b/app/models/poll.rb @@ -1,2 +1,4 @@ class Poll < ActiveRecord::Base -end \ No newline at end of file + has_many :booths + has_many :voters, through: :booths, class_name: "Poll::Voter" +end diff --git a/app/models/poll/booth.rb b/app/models/poll/booth.rb new file mode 100644 index 000000000..a05f3d547 --- /dev/null +++ b/app/models/poll/booth.rb @@ -0,0 +1,6 @@ +class Poll + class Booth < ActiveRecord::Base + belongs_to :poll + has_many :voters + end +end \ No newline at end of file diff --git a/app/models/poll/voter.rb b/app/models/poll/voter.rb index 92df28443..353d593b3 100644 --- a/app/models/poll/voter.rb +++ b/app/models/poll/voter.rb @@ -1,15 +1,26 @@ class Poll class Voter < ActiveRecord::Base belongs_to :booth + delegate :poll, to: :booth validate :in_census + validate :has_not_voted def in_census errors.add(:document_number, :not_in_census) unless census_api_response.valid? end + def has_not_voted + errors.add(:document_number, :has_voted) if has_voted? + end + def census_api_response CensusApi.new.call(document_type, document_number) end + + def has_voted? + poll.voters.where(document_number: document_number, document_type: document_type).present? + end + end end \ No newline at end of file diff --git a/config/locales/activerecord.en.yml b/config/locales/activerecord.en.yml index 2633a6d77..b719885b5 100644 --- a/config/locales/activerecord.en.yml +++ b/config/locales/activerecord.en.yml @@ -87,6 +87,7 @@ en: attributes: document_number: not_in_census: "Document not in census" + has_voted: "Has already voted" proposal: attributes: tag_list: diff --git a/config/locales/activerecord.es.yml b/config/locales/activerecord.es.yml index 10e53466d..9d51505fe 100644 --- a/config/locales/activerecord.es.yml +++ b/config/locales/activerecord.es.yml @@ -87,6 +87,7 @@ es: attributes: document_number: not_in_census: "Este documento no aparece en el censo" + has_voted: "Ya ha votado" proposal: attributes: tag_list: diff --git a/db/migrate/20160914172535_create_poll_booths.rb b/db/migrate/20160914172535_create_poll_booths.rb new file mode 100644 index 000000000..a474aba49 --- /dev/null +++ b/db/migrate/20160914172535_create_poll_booths.rb @@ -0,0 +1,8 @@ +class CreatePollBooths < ActiveRecord::Migration + def change + create_table :poll_booths do |t| + t.string :name + t.integer :poll_id + end + end +end