diff --git a/app/controllers/polls/questions_controller.rb b/app/controllers/polls/questions_controller.rb index 03e71c15b..e9c7e2bbd 100644 --- a/app/controllers/polls/questions_controller.rb +++ b/app/controllers/polls/questions_controller.rb @@ -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 diff --git a/app/controllers/polls_controller.rb b/app/controllers/polls_controller.rb index a93c84e9d..ce80f8dcc 100644 --- a/app/controllers/polls_controller.rb +++ b/app/controllers/polls_controller.rb @@ -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 diff --git a/app/models/poll/answer.rb b/app/models/poll/answer.rb new file mode 100644 index 000000000..b0f2d4410 --- /dev/null +++ b/app/models/poll/answer.rb @@ -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 \ No newline at end of file diff --git a/db/migrate/20170125112017_create_poll_answers.rb b/db/migrate/20170125112017_create_poll_answers.rb new file mode 100644 index 000000000..7ea930d0c --- /dev/null +++ b/db/migrate/20170125112017_create_poll_answers.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 0cdbcad2b..932d4e28a 100644 --- a/db/schema.rb +++ b/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" diff --git a/spec/factories.rb b/spec/factories.rb index f46362e16..c3e694025 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -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 diff --git a/spec/features/polls/polls_spec.rb b/spec/features/polls/polls_spec.rb index c4a8f55a2..fe5957181 100644 --- a/spec/features/polls/polls_spec.rb +++ b/spec/features/polls/polls_spec.rb @@ -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) diff --git a/spec/features/polls/questions_spec.rb b/spec/features/polls/questions_spec.rb index af4c1058c..e925c8fd6 100644 --- a/spec/features/polls/questions_spec.rb +++ b/spec/features/polls/questions_spec.rb @@ -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)