adds Poll::Answer model for web users
PartialResults is kept for booth results
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,15 @@ 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!
|
||||
|
||||
@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
|
||||
|
||||
|
||||
13
app/models/poll/answer.rb
Normal file
13
app/models/poll/answer.rb
Normal file
@@ -0,0 +1,13 @@
|
||||
class Poll::Answer < ActiveRecord::Base
|
||||
|
||||
belongs_to :question, -> { with_hidden }
|
||||
belongs_to :author, -> { with_hidden }, class_name: 'User', foreign_key: 'author_id'
|
||||
|
||||
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) }
|
||||
end
|
||||
15
db/migrate/20170125112017_create_poll_answers.rb
Normal file
15
db/migrate/20170125112017_create_poll_answers.rb
Normal file
@@ -0,0 +1,15 @@
|
||||
class CreatePollAnswers < ActiveRecord::Migration
|
||||
def change
|
||||
create_table :poll_answers do |t|
|
||||
t.integer :question_id
|
||||
t.integer :author_id
|
||||
t.string :answer
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
|
||||
add_index :poll_answers, :question_id
|
||||
add_index :poll_answers, :author_id
|
||||
add_index :poll_answers, [:question_id, :answer]
|
||||
end
|
||||
end
|
||||
14
db/schema.rb
14
db/schema.rb
@@ -11,7 +11,7 @@
|
||||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 20170125104542) do
|
||||
ActiveRecord::Schema.define(version: 20170125112017) do
|
||||
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "plpgsql"
|
||||
@@ -374,6 +374,18 @@ ActiveRecord::Schema.define(version: 20170125104542) do
|
||||
|
||||
add_index "organizations", ["user_id"], name: "index_organizations_on_user_id", using: :btree
|
||||
|
||||
create_table "poll_answers", force: :cascade do |t|
|
||||
t.integer "question_id"
|
||||
t.integer "author_id"
|
||||
t.string "answer"
|
||||
t.datetime "created_at"
|
||||
t.datetime "updated_at"
|
||||
end
|
||||
|
||||
add_index "poll_answers", ["author_id"], name: "index_poll_answers_on_author_id", using: :btree
|
||||
add_index "poll_answers", ["question_id", "answer"], name: "index_poll_answers_on_question_id_and_answer", using: :btree
|
||||
add_index "poll_answers", ["question_id"], name: "index_poll_answers_on_question_id", using: :btree
|
||||
|
||||
create_table "poll_booth_assignments", force: :cascade do |t|
|
||||
t.integer "booth_id"
|
||||
t.integer "poll_id"
|
||||
|
||||
@@ -434,6 +434,12 @@ FactoryGirl.define do
|
||||
end
|
||||
end
|
||||
|
||||
factory :poll_answer, class: 'Poll::Answer' do
|
||||
association :question, factory: :poll_question
|
||||
association :author, factory: :user
|
||||
answer { question.verified_answers.sample }
|
||||
end
|
||||
|
||||
factory :poll_partial_result, class: 'Poll::PartialResult' do
|
||||
association :question, factory: :poll_question
|
||||
association :author, factory: :user
|
||||
|
||||
@@ -166,7 +166,7 @@ feature 'Polls' do
|
||||
scenario 'Level 2 users who have already answered' do
|
||||
question = create(:poll_question, poll: poll, valid_answers: 'Han Solo, Chewbacca')
|
||||
user = create(:user, :level_two)
|
||||
create(:poll_partial_result, question: question, author: user, answer: 'Chewbacca')
|
||||
create(:poll_answer, question: question, author: user, answer: 'Chewbacca')
|
||||
|
||||
login_as user
|
||||
visit poll_path(poll)
|
||||
|
||||
@@ -70,7 +70,7 @@ feature 'Poll Questions' do
|
||||
question = create(:poll_question, poll: poll, valid_answers: 'Han Solo, Chewbacca')
|
||||
|
||||
user = create(:user, :level_two, geozone: geozone)
|
||||
create(:poll_partial_result, question: question, author: user, answer: 'Chewbacca')
|
||||
create(:poll_answer, question: question, author: user, answer: 'Chewbacca')
|
||||
|
||||
login_as user
|
||||
visit question_path(question)
|
||||
|
||||
Reference in New Issue
Block a user