Skip to content

Commit

Permalink
removes answer <-> voter association
Browse files Browse the repository at this point in the history
  • Loading branch information
xuanxu committed Jan 27, 2017
1 parent ce7bac4 commit 51be80e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 15 deletions.
4 changes: 1 addition & 3 deletions app/models/poll/answer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ class Poll::Answer < ActiveRecord::Base
belongs_to :question, -> { with_hidden }
belongs_to :author, -> { with_hidden }, class_name: 'User', foreign_key: 'author_id'

has_one :voter

delegate :poll, :poll_id, to: :question

validates :question, presence: true
Expand All @@ -16,6 +14,6 @@ class Poll::Answer < ActiveRecord::Base
scope :by_question, -> (question_id) { where(question_id: question_id) }

def record_voter_participation
Poll::Voter.create_from_user(author, {poll_id: poll_id, answer_id: id})
Poll::Voter.create_from_user(author, {poll_id: poll_id})
end
end
5 changes: 1 addition & 4 deletions app/models/poll/voter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ class Poll
class Voter < ActiveRecord::Base
belongs_to :poll
belongs_to :booth_assignment
belongs_to :answer

validates :poll, presence: true
validates :document_number, presence: true, uniqueness: { scope: [:poll_id, :document_type], message: :has_voted }
Expand All @@ -26,7 +25,6 @@ def fill_stats_fields
def self.create_from_user(user, options = {})
poll_id = options[:poll_id]
booth_assignment_id = options[:booth_assignment_id]
answer_id = options[:answer_id]

Voter.create(
document_type: user.document_type,
Expand All @@ -35,8 +33,7 @@ def self.create_from_user(user, options = {})
booth_assignment_id: booth_assignment_id,
gender: user.gender,
geozone_id: user.geozone_id,
age: user.age,
answer_id: answer_id
age: user.age
)
end

Expand Down
13 changes: 8 additions & 5 deletions spec/features/polls/questions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@
expect(page).to have_link('Answer this question')
end

scenario 'Records participarion', :js do
scenario 'Records participation', :js do
question = create(:poll_question, poll: poll, valid_answers: 'Han Solo, Chewbacca')
user = create(:user, :level_two, geozone: geozone)
user = create(:user, :level_two, geozone: geozone, gender: 'female', date_of_birth: 33.years.ago)

login_as user
visit question_path(question)
Expand All @@ -99,9 +99,12 @@

expect(page).to_not have_link('Han Solo')

answer = Poll::Answer.by_question(question.id).by_author(user.id).first
expect(answer.voter.document_number).to eq(user.document_number)
expect(answer.voter.poll_id).to eq(poll.id)
voter = poll.voters.first
expect(voter.document_number).to eq(user.document_number)
expect(voter.geozone_id).to eq(user.geozone_id)
expect(voter.gender).to eq(user.gender)
expect(voter.age).to eq(33)
expect(voter.poll_id).to eq(poll.id)
end

end
Expand Down
6 changes: 3 additions & 3 deletions spec/models/poll/answer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
describe "#record_voter_participation" do
it "creates a poll_voter with user and poll data" do
answer = create(:poll_answer)
expect(answer.voter).to be_nil
expect(answer.poll.voters).to be_blank

answer.record_voter_participation
voter = answer.reload.voter
expect(answer.poll.reload.voters.size).to eq(1)
voter = answer.poll.voters.first

expect(voter.answer).to eq(answer)
expect(voter.document_number).to eq(answer.author.document_number)
expect(voter.poll_id).to eq(answer.poll.id)
end
Expand Down

0 comments on commit 51be80e

Please sign in to comment.