validates user has not already voted

This commit is contained in:
rgarcia
2016-09-19 18:22:35 +02:00
committed by kikito
parent 44df91346a
commit 8dc7285681
6 changed files with 30 additions and 1 deletions

View File

@@ -1,2 +1,4 @@
class Poll < ActiveRecord::Base
end
has_many :booths
has_many :voters, through: :booths, class_name: "Poll::Voter"
end

6
app/models/poll/booth.rb Normal file
View File

@@ -0,0 +1,6 @@
class Poll
class Booth < ActiveRecord::Base
belongs_to :poll
has_many :voters
end
end

View File

@@ -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