Merge branch 'polls' into polls-question-show
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
class Polls::QuestionsController < ApplicationController
|
||||
|
||||
load_and_authorize_resource :poll
|
||||
load_and_authorize_resource :question, class: 'Poll::Question'#, through: :poll
|
||||
load_and_authorize_resource :question, class: 'Poll::Question'
|
||||
|
||||
has_orders %w{most_voted newest oldest}, only: :show
|
||||
|
||||
@@ -10,17 +10,16 @@ class Polls::QuestionsController < ApplicationController
|
||||
@comment_tree = CommentTree.new(@commentable, params[:page], @current_order)
|
||||
set_comment_flags(@comment_tree.comments)
|
||||
|
||||
question_answer = @question.partial_results.where(author_id: current_user.try(:id)).first
|
||||
question_answer = @question.answers.where(author_id: current_user.try(:id)).first
|
||||
@answers_by_question_id = {@question.id => question_answer.try(:answer)}
|
||||
end
|
||||
|
||||
def answer
|
||||
partial_result = @question.partial_results.find_or_initialize_by(author: current_user,
|
||||
amount: 1,
|
||||
origin: 'web')
|
||||
answer = @question.answers.find_or_initialize_by(author: current_user)
|
||||
|
||||
partial_result.answer = params[:answer]
|
||||
partial_result.save!
|
||||
answer.answer = params[:answer]
|
||||
answer.save!
|
||||
answer.record_voter_participation
|
||||
|
||||
@answers_by_question_id = {@question.id => params[:answer]}
|
||||
end
|
||||
|
||||
@@ -9,12 +9,12 @@ class PollsController < ApplicationController
|
||||
end
|
||||
|
||||
def show
|
||||
@questions = @poll.questions.for_render.sort_for_list
|
||||
@questions = @poll.questions.for_render.sort_for_list
|
||||
|
||||
@answers_by_question_id = {}
|
||||
poll_partial_results = Poll::PartialResult.by_question(@poll.question_ids).by_author(current_user.try(:id))
|
||||
poll_partial_results.each do |result|
|
||||
@answers_by_question_id[result.question_id] = result.answer
|
||||
poll_answers = Poll::Answer.by_question(@poll.question_ids).by_author(current_user.try(:id))
|
||||
poll_answers.each do |answer|
|
||||
@answers_by_question_id[answer.question_id] = answer.answer
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
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 :voters
|
||||
has_many :officer_assignments, through: :booth_assignments
|
||||
has_many :officers, through: :officer_assignments
|
||||
has_many :questions
|
||||
|
||||
21
app/models/poll/answer.rb
Normal file
21
app/models/poll/answer.rb
Normal file
@@ -0,0 +1,21 @@
|
||||
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
|
||||
validates :author, presence: true
|
||||
validates :answer, presence: true
|
||||
validates :answer, inclusion: {in: ->(a) { a.question.valid_answers }}
|
||||
|
||||
scope :by_author, -> (author_id) { where(author_id: author_id) }
|
||||
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})
|
||||
end
|
||||
end
|
||||
@@ -1,31 +1,55 @@
|
||||
class Poll
|
||||
class Voter < ActiveRecord::Base
|
||||
belongs_to :poll
|
||||
belongs_to :booth_assignment
|
||||
delegate :poll, to: :booth_assignment
|
||||
belongs_to :answer
|
||||
|
||||
validates :booth_assignment, presence: true
|
||||
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, name: name) if has_voted?
|
||||
end
|
||||
validates :poll, presence: true
|
||||
validates :document_number, presence: true, uniqueness: { scope: [:poll_id, :document_type], message: :has_voted }
|
||||
|
||||
def census_api_response
|
||||
@census ||= CensusApi.new.call(document_type, document_number)
|
||||
@census_api_response ||= CensusApi.new.call(document_type, document_number)
|
||||
end
|
||||
|
||||
def has_voted?
|
||||
poll.document_has_voted?(document_number, document_type)
|
||||
def in_census?
|
||||
census_api_response.valid?
|
||||
end
|
||||
|
||||
def name
|
||||
@census.name
|
||||
def fill_stats_fields
|
||||
if in_census?
|
||||
self.gender = census_api_response.gender
|
||||
self.geozone_id = Geozone.select(:id).where(census_code: census_api_response.district_code).first.try(:id)
|
||||
self.age = voter_age(census_api_response.date_of_birth)
|
||||
end
|
||||
end
|
||||
|
||||
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,
|
||||
document_number: user.document_number,
|
||||
poll_id: poll_id,
|
||||
booth_assignment_id: booth_assignment_id,
|
||||
gender: user.gender,
|
||||
geozone_id: user.geozone_id,
|
||||
age: user.age,
|
||||
answer_id: answer_id
|
||||
)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def voter_age(dob)
|
||||
if dob.blank?
|
||||
nil
|
||||
else
|
||||
now = Time.now.utc.to_date
|
||||
now.year - dob.year - ((now.month > dob.month || (now.month == dob.month && now.day >= dob.day)) ? 0 : 1)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
@@ -246,6 +246,17 @@ class User < ActiveRecord::Base
|
||||
"#{name} (#{email})"
|
||||
end
|
||||
|
||||
def age
|
||||
if date_of_birth.blank?
|
||||
nil
|
||||
else
|
||||
now = Time.now.utc.to_date
|
||||
now.year - date_of_birth.year - (
|
||||
(now.month > date_of_birth.month || (now.month == date_of_birth.month && now.day >= date_of_birth.day)
|
||||
) ? 0 : 1)
|
||||
end
|
||||
end
|
||||
|
||||
def save_requiring_finish_signup
|
||||
begin
|
||||
self.registering_with_oauth = true
|
||||
|
||||
Reference in New Issue
Block a user