refactors poll::voter

now belongs_to booth_assignment instead of to booth
This commit is contained in:
Juanjo Bazán
2016-12-06 20:20:18 +01:00
parent ad2dd7d267
commit 45d26f6dee
9 changed files with 73 additions and 23 deletions

View File

@@ -1,10 +1,9 @@
class Poll < ActiveRecord::Base
has_many :booth_assignments, class_name: "Poll::BoothAssignment"
has_many :booths, through: :booth_assignments
has_many :voters, through: :booth_assignments
has_many :officer_assignments, through: :booth_assignments
has_many :officers, through: :officer_assignments
has_many :voters, through: :booths, class_name: "Poll::Voter"
has_many :questions
validates :name, presence: true
@@ -35,4 +34,8 @@ class Poll < ActiveRecord::Base
return none if user.nil? || user.unverified?
current
end
def document_has_voted?(document_number, document_type)
voters.where(document_number: document_number, document_type: document_type).exists?
end
end

View File

@@ -2,7 +2,6 @@ class Poll
class Booth < ActiveRecord::Base
has_many :booth_assignments, class_name: "Poll::BoothAssignment"
has_many :polls, through: :booth_assignments
has_many :voters
validates :name, presence: true, uniqueness: true
end

View File

@@ -5,5 +5,6 @@ class Poll
has_many :officer_assignments, class_name: "Poll::OfficerAssignment", dependent: :destroy
has_many :officers, through: :officer_assignments
has_many :voters
end
end

View File

@@ -1,11 +1,14 @@
class Poll
class Voter < ActiveRecord::Base
belongs_to :booth
delegate :poll, to: :booth
belongs_to :booth_assignment
delegate :poll, to: :booth_assignment
validates :booth_assignment, presence: true
validate :in_census
validate :has_not_voted
before_create :assign_poll
def in_census
errors.add(:document_number, :not_in_census) unless census_api_response.valid?
end
@@ -19,12 +22,16 @@ class Poll
end
def has_voted?
poll.voters.where(document_number: document_number, document_type: document_type).exists?
poll.document_has_voted?(document_number, document_type)
end
def name
@census.name
end
def assign_poll
poll_id = booth_assignment.poll_id
end
end
end