validates user has not already voted
This commit is contained in:
@@ -1,2 +1,4 @@
|
||||
class Poll < ActiveRecord::Base
|
||||
has_many :booths
|
||||
has_many :voters, through: :booths, class_name: "Poll::Voter"
|
||||
end
|
||||
6
app/models/poll/booth.rb
Normal file
6
app/models/poll/booth.rb
Normal file
@@ -0,0 +1,6 @@
|
||||
class Poll
|
||||
class Booth < ActiveRecord::Base
|
||||
belongs_to :poll
|
||||
has_many :voters
|
||||
end
|
||||
end
|
||||
@@ -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
|
||||
@@ -87,6 +87,7 @@ en:
|
||||
attributes:
|
||||
document_number:
|
||||
not_in_census: "Document not in census"
|
||||
has_voted: "Has already voted"
|
||||
proposal:
|
||||
attributes:
|
||||
tag_list:
|
||||
|
||||
@@ -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:
|
||||
|
||||
8
db/migrate/20160914172535_create_poll_booths.rb
Normal file
8
db/migrate/20160914172535_create_poll_booths.rb
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user