From d9d03079e173aa438909ac0795d21adcac05e638 Mon Sep 17 00:00:00 2001 From: Angel Perez Date: Tue, 3 Oct 2017 13:29:07 -0400 Subject: [PATCH 1/9] Adapt Poll::Question 'show' action to mockup --- app/views/admin/poll/questions/_form.html.erb | 4 ---- app/views/admin/poll/questions/show.html.erb | 22 ++++++++++++------- config/locales/en/admin.yml | 3 ++- config/locales/es/admin.yml | 3 ++- config/locales/fr/admin.yml | 3 ++- 5 files changed, 20 insertions(+), 15 deletions(-) diff --git a/app/views/admin/poll/questions/_form.html.erb b/app/views/admin/poll/questions/_form.html.erb index 7881936e4..5b4de0bcb 100644 --- a/app/views/admin/poll/questions/_form.html.erb +++ b/app/views/admin/poll/questions/_form.html.erb @@ -16,10 +16,6 @@ <%= f.text_field :title, maxlength: Poll::Question.title_max_length %> - <%= f.label :valid_answers %> -

<%= t("admin.questions.new.valid_answers_note") %>

- <%= f.text_field :valid_answers, label: false, aria: {describedby: "valid-answers-help-text"} %> -
<%= f.cktext_area :description, maxlength: Poll::Question.description_max_length, diff --git a/app/views/admin/poll/questions/show.html.erb b/app/views/admin/poll/questions/show.html.erb index 3f776c5c0..9d38735ca 100644 --- a/app/views/admin/poll/questions/show.html.erb +++ b/app/views/admin/poll/questions/show.html.erb @@ -24,15 +24,21 @@ <%= link_to @question.author.name, user_path(@question.author) %>

-

- <%= t("admin.questions.show.valid_answers") %> -

+ + + + + - <% @question.valid_answers.each do |answer| %> - - <%= answer %> - - <% end %> + + + + + + + + +
<%= t('admin.questions.show.valid_answers') %><%= link_to t("admin.questions.show.add_answer"), "#", class: "button hollow float-right" %>
<%= t("admin.questions.show.answer") %><%= t("admin.questions.show.description") %>

<%= t("admin.questions.show.description") %> diff --git a/config/locales/en/admin.yml b/config/locales/en/admin.yml index 0545e6af5..2b8a18ce2 100644 --- a/config/locales/en/admin.yml +++ b/config/locales/en/admin.yml @@ -597,12 +597,13 @@ en: new: title: "Create Question" poll_label: "Poll" - valid_answers_note: "Enter the answers separated by commas (,)" show: proposal: Original proposal author: Author title: Title valid_answers: Valid answers + add_answer: "Add answer" + answer: "Answer" description: Description video_url: External video documents: Documents (1) diff --git a/config/locales/es/admin.yml b/config/locales/es/admin.yml index 816cadd57..333cf9cf9 100644 --- a/config/locales/es/admin.yml +++ b/config/locales/es/admin.yml @@ -597,12 +597,13 @@ es: new: title: "Crear pregunta ciudadana" poll_label: "Votación" - valid_answers_note: "Escribe las respuestas separadas por comas (,)" show: proposal: Propuesta ciudadana original author: Autor title: Título valid_answers: Respuestas válidas + add_answer: "Añadir respuesta" + answer: "Respuesta" description: Descripción video_url: Video externo documents: Documentos (1) diff --git a/config/locales/fr/admin.yml b/config/locales/fr/admin.yml index ae9a44277..f509b2a94 100644 --- a/config/locales/fr/admin.yml +++ b/config/locales/fr/admin.yml @@ -377,12 +377,13 @@ fr: new: title: "Créer une question" poll_label: "Vote" - valid_answers_note: "Saisir les réponses séparées par des virgules (,)" show: proposal: Proposition originale author: Auteur title: Titre valid_answers: Réponses valides + add_answer: "Ajouter une réponse" + answer: "Réponse" description: Description preview: Voir l'aperçu recounts: From 1d1b861ddf34e7caaf68077c9a6e4dc571acd3aa Mon Sep 17 00:00:00 2001 From: Angel Perez Date: Tue, 3 Oct 2017 23:04:50 -0400 Subject: [PATCH 2/9] Create 'poll_question_answers' table --- app/models/poll/question.rb | 1 + app/models/poll/question_answer.rb | 5 +++++ .../20171004025903_create_poll_question_answers.rb | 9 +++++++++ db/schema.rb | 11 ++++++++++- 4 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 app/models/poll/question_answer.rb create mode 100644 db/migrate/20171004025903_create_poll_question_answers.rb diff --git a/app/models/poll/question.rb b/app/models/poll/question.rb index a46f28a8c..d576ac95a 100644 --- a/app/models/poll/question.rb +++ b/app/models/poll/question.rb @@ -15,6 +15,7 @@ class Poll::Question < ActiveRecord::Base has_many :comments, as: :commentable has_many :answers + has_many :question_answers, class_name: 'Poll::QuestionAnswer', foreign_key: 'poll_question_id' has_many :partial_results belongs_to :proposal diff --git a/app/models/poll/question_answer.rb b/app/models/poll/question_answer.rb new file mode 100644 index 000000000..508a92ee2 --- /dev/null +++ b/app/models/poll/question_answer.rb @@ -0,0 +1,5 @@ +class Poll::QuestionAnswer < ActiveRecord::Base + belongs_to :question, class_name: 'Poll::Question', foreign_key: 'poll_question_id' + + validates :title, presence: true +end diff --git a/db/migrate/20171004025903_create_poll_question_answers.rb b/db/migrate/20171004025903_create_poll_question_answers.rb new file mode 100644 index 000000000..834421f5f --- /dev/null +++ b/db/migrate/20171004025903_create_poll_question_answers.rb @@ -0,0 +1,9 @@ +class CreatePollQuestionAnswers < ActiveRecord::Migration + def change + create_table :poll_question_answers do |t| + t.string :title + t.text :description + t.references :poll_question, index: true, foreign_key: true + end + end +end diff --git a/db/schema.rb b/db/schema.rb index af121fa0e..4e62c099c 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: 20171002191347) do +ActiveRecord::Schema.define(version: 20171004025903) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -669,6 +669,14 @@ ActiveRecord::Schema.define(version: 20171002191347) do add_index "poll_partial_results", ["origin"], name: "index_poll_partial_results_on_origin", using: :btree add_index "poll_partial_results", ["question_id"], name: "index_poll_partial_results_on_question_id", using: :btree + create_table "poll_question_answers", force: :cascade do |t| + t.string "title" + t.text "description" + t.integer "poll_question_id" + end + + add_index "poll_question_answers", ["poll_question_id"], name: "index_poll_question_answers_on_poll_question_id", using: :btree + create_table "poll_questions", force: :cascade do |t| t.integer "proposal_id" t.integer "poll_id" @@ -1143,6 +1151,7 @@ ActiveRecord::Schema.define(version: 20171002191347) do add_foreign_key "poll_partial_results", "poll_officer_assignments", column: "officer_assignment_id" add_foreign_key "poll_partial_results", "poll_questions", column: "question_id" add_foreign_key "poll_partial_results", "users", column: "author_id" + add_foreign_key "poll_question_answers", "poll_questions" add_foreign_key "poll_questions", "polls" add_foreign_key "poll_questions", "proposals" add_foreign_key "poll_questions", "users", column: "author_id" From 1cc0816dcdd47409a897dafc83bde14ac4d2830f Mon Sep 17 00:00:00 2001 From: Angel Perez Date: Tue, 3 Oct 2017 23:23:50 -0400 Subject: [PATCH 3/9] Create 'poll_question_answers' controller and views --- .../admin/poll/answers_controller.rb | 29 +++++++++++++++++++ app/models/poll/question_answer.rb | 4 +++ app/views/admin/poll/answers/_form.html.erb | 24 +++++++++++++++ app/views/admin/poll/answers/new.html.erb | 7 +++++ app/views/admin/poll/questions/show.html.erb | 7 +++-- config/locales/en/activerecord.yml | 3 ++ config/locales/en/admin.yml | 3 ++ config/locales/es/activerecord.yml | 3 ++ config/locales/es/admin.yml | 3 ++ config/locales/fr/activerecord.yml | 3 ++ config/locales/fr/admin.yml | 3 ++ config/routes.rb | 4 ++- 12 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 app/controllers/admin/poll/answers_controller.rb create mode 100644 app/views/admin/poll/answers/_form.html.erb create mode 100644 app/views/admin/poll/answers/new.html.erb diff --git a/app/controllers/admin/poll/answers_controller.rb b/app/controllers/admin/poll/answers_controller.rb new file mode 100644 index 000000000..f949ad115 --- /dev/null +++ b/app/controllers/admin/poll/answers_controller.rb @@ -0,0 +1,29 @@ +class Admin::Poll::AnswersController < Admin::Poll::BaseController + before_action :load_question + + load_and_authorize_resource :question, class: "::Poll::Question" + + def new + @answer = Poll::QuestionAnswer.new + end + + def create + @answer = Poll::QuestionAnswer.new(answer_params) + + if @answer.save + redirect_to admin_question_path(@question) + else + render :new + end + end + + private + + def answer_params + params.require(:poll_question_answer).permit(:title, :description, :poll_question_id) + end + + def load_question + @question = ::Poll::Question.find(params[:question_id]) + end +end diff --git a/app/models/poll/question_answer.rb b/app/models/poll/question_answer.rb index 508a92ee2..871336cfd 100644 --- a/app/models/poll/question_answer.rb +++ b/app/models/poll/question_answer.rb @@ -2,4 +2,8 @@ class Poll::QuestionAnswer < ActiveRecord::Base belongs_to :question, class_name: 'Poll::Question', foreign_key: 'poll_question_id' validates :title, presence: true + + def description + super.try :html_safe + end end diff --git a/app/views/admin/poll/answers/_form.html.erb b/app/views/admin/poll/answers/_form.html.erb new file mode 100644 index 000000000..7da1afd26 --- /dev/null +++ b/app/views/admin/poll/answers/_form.html.erb @@ -0,0 +1,24 @@ +<%= form_for(@answer, url: form_url) do |f| %> + + <%= render 'shared/errors', resource: @answer %> + + <%= f.hidden_field :poll_question_id, value: @question.id %> + +

+
+ <%= f.text_field :title %> + +
+ <%= f.cktext_area :description, + maxlength: Poll::Question.description_max_length, + ckeditor: { language: I18n.locale } %> +
+ +
+
+ <%= f.submit(class: "button expanded", value: t("shared.save")) %> +
+
+
+
+<% end %> diff --git a/app/views/admin/poll/answers/new.html.erb b/app/views/admin/poll/answers/new.html.erb new file mode 100644 index 000000000..b4d02cfeb --- /dev/null +++ b/app/views/admin/poll/answers/new.html.erb @@ -0,0 +1,7 @@ +<%= back_link_to %> + +

<%= t('admin.answers.new.title') %>

+ +
+ <%= render "form", form_url: admin_question_answers_path %> +
diff --git a/app/views/admin/poll/questions/show.html.erb b/app/views/admin/poll/questions/show.html.erb index 9d38735ca..6f8dfd583 100644 --- a/app/views/admin/poll/questions/show.html.erb +++ b/app/views/admin/poll/questions/show.html.erb @@ -27,7 +27,7 @@ - + @@ -36,7 +36,10 @@ - + <% @question.question_answers.each do |answer| %> + + + <% end %>
<%= t('admin.questions.show.valid_answers') %><%= link_to t("admin.questions.show.add_answer"), "#", class: "button hollow float-right" %><%= link_to t("admin.questions.show.add_answer"), new_admin_question_answer_path(@question), class: "button hollow float-right" %>
<%= answer.title %><%= answer.description %>
diff --git a/config/locales/en/activerecord.yml b/config/locales/en/activerecord.yml index 1e3f62b1a..dd279f6ae 100644 --- a/config/locales/en/activerecord.yml +++ b/config/locales/en/activerecord.yml @@ -211,6 +211,9 @@ en: image: title: Title attachment: Attachment + poll/question_answer: + title: "Answer" + description: "Description" errors: models: user: diff --git a/config/locales/en/admin.yml b/config/locales/en/admin.yml index 2b8a18ce2..2c6b8bc97 100644 --- a/config/locales/en/admin.yml +++ b/config/locales/en/admin.yml @@ -608,6 +608,9 @@ en: video_url: External video documents: Documents (1) preview: View on website + answers: + new: + title: "New answer" recounts: index: title: "Recounts" diff --git a/config/locales/es/activerecord.yml b/config/locales/es/activerecord.yml index 108f19fcf..c12b8e9a9 100644 --- a/config/locales/es/activerecord.yml +++ b/config/locales/es/activerecord.yml @@ -205,6 +205,9 @@ es: image: title: Título attachment: Archivo adjunto + poll/question_answer: + title: "Respuesta" + description: "Descripción" errors: models: user: diff --git a/config/locales/es/admin.yml b/config/locales/es/admin.yml index 333cf9cf9..37e34fa52 100644 --- a/config/locales/es/admin.yml +++ b/config/locales/es/admin.yml @@ -608,6 +608,9 @@ es: video_url: Video externo documents: Documentos (1) preview: Ver en la web + answers: + new: + title: "Nueva respuesta" recounts: index: title: "Recuentos" diff --git a/config/locales/fr/activerecord.yml b/config/locales/fr/activerecord.yml index b588d37dc..d6ab5db80 100644 --- a/config/locales/fr/activerecord.yml +++ b/config/locales/fr/activerecord.yml @@ -147,6 +147,9 @@ fr: name: Nom locale: Langue body: Contenu + poll/question_answer: + title: "Réponse" + description: "Description" errors: models: user: diff --git a/config/locales/fr/admin.yml b/config/locales/fr/admin.yml index f509b2a94..e000c8a28 100644 --- a/config/locales/fr/admin.yml +++ b/config/locales/fr/admin.yml @@ -386,6 +386,9 @@ fr: answer: "Réponse" description: Description preview: Voir l'aperçu + answers: + new: + title: "Nouvelle réponse" recounts: index: title: "Dépouillements" diff --git a/config/routes.rb b/config/routes.rb index 410f2dced..d72058737 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -302,7 +302,9 @@ Rails.application.routes.draw do end end - resources :questions + resources :questions do + resources :answers, only: [:new, :create] + end end resources :verifications, controller: :verifications, only: :index do From bf7cab29cf3751d7bf47a1e6b031575552fc08e0 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Wed, 4 Oct 2017 15:04:34 +0200 Subject: [PATCH 4/9] fixes specs --- app/views/admin/poll/questions/_form.html.erb | 6 ------ config/locales/en/admin.yml | 3 --- spec/features/admin/poll/questions_spec.rb | 2 -- 3 files changed, 11 deletions(-) diff --git a/app/views/admin/poll/questions/_form.html.erb b/app/views/admin/poll/questions/_form.html.erb index 5b4de0bcb..56b7ec93f 100644 --- a/app/views/admin/poll/questions/_form.html.erb +++ b/app/views/admin/poll/questions/_form.html.erb @@ -16,12 +16,6 @@ <%= f.text_field :title, maxlength: Poll::Question.title_max_length %> -
- <%= f.cktext_area :description, - maxlength: Poll::Question.description_max_length, - ckeditor: { language: I18n.locale } %> -
-
<%= render 'documents/nested_documents', documentable: @question, f: f %>
diff --git a/config/locales/en/admin.yml b/config/locales/en/admin.yml index 181c3dbc6..fc7082edf 100644 --- a/config/locales/en/admin.yml +++ b/config/locales/en/admin.yml @@ -597,12 +597,9 @@ en: description: Description video_url: External video documents: Documents (1) - preview: View on website answers: new: title: "New answer" - video_url: External video - documents: Documents (1) recounts: index: title: "Recounts" diff --git a/spec/features/admin/poll/questions_spec.rb b/spec/features/admin/poll/questions_spec.rb index 52e3783f0..7e103c647 100644 --- a/spec/features/admin/poll/questions_spec.rb +++ b/spec/features/admin/poll/questions_spec.rb @@ -25,7 +25,6 @@ feature 'Admin poll questions' do expect(page).to have_content(question.title) expect(page).to have_content(question.author.name) - expect(page).to have_content(question.valid_answers.join(" ")) end scenario 'Create' do @@ -61,7 +60,6 @@ feature 'Admin poll questions' do expect(current_path).to eq(new_admin_question_path) expect(page).to have_field('poll_question_title', with: proposal.title) - expect(page).to have_field('poll_question_valid_answers', with: "Yes, No") select 'Proposals', from: 'poll_question_poll_id' From 1f2aa301d4016ff3966305ca68f4e3e8a590441b Mon Sep 17 00:00:00 2001 From: rgarcia Date: Wed, 4 Oct 2017 16:33:52 +0200 Subject: [PATCH 5/9] adds specs for answer's index and create --- .../admin/poll/answers_controller.rb | 3 +- app/views/admin/poll/questions/show.html.erb | 22 ++++++---- config/locales/en/admin.yml | 4 +- config/locales/en/responders.yml | 1 + config/locales/es/admin.yml | 4 +- config/locales/es/responders.yml | 1 + spec/factories.rb | 6 +++ spec/features/polls/answers_spec.rb | 41 +++++++++++++++++++ 8 files changed, 71 insertions(+), 11 deletions(-) create mode 100644 spec/features/polls/answers_spec.rb diff --git a/app/controllers/admin/poll/answers_controller.rb b/app/controllers/admin/poll/answers_controller.rb index f949ad115..5650ab773 100644 --- a/app/controllers/admin/poll/answers_controller.rb +++ b/app/controllers/admin/poll/answers_controller.rb @@ -11,7 +11,8 @@ class Admin::Poll::AnswersController < Admin::Poll::BaseController @answer = Poll::QuestionAnswer.new(answer_params) if @answer.save - redirect_to admin_question_path(@question) + redirect_to admin_question_path(@question), + notice: t("flash.actions.create.poll_question_answer") else render :new end diff --git a/app/views/admin/poll/questions/show.html.erb b/app/views/admin/poll/questions/show.html.erb index 1502c8357..7037afd5f 100644 --- a/app/views/admin/poll/questions/show.html.erb +++ b/app/views/admin/poll/questions/show.html.erb @@ -26,21 +26,27 @@ - - + + - - + + - - <% @question.question_answers.each do |answer| %> + <% @question.question_answers.each do |answer| %> + - <% end %> - + + <% end %>
<%= t('admin.questions.show.valid_answers') %><%= link_to t("admin.questions.show.add_answer"), new_admin_question_answer_path(@question), class: "button hollow float-right" %> + <%= t('admin.questions.show.valid_answers') %> + + <%= link_to t("admin.questions.show.add_answer"), + new_admin_question_answer_path(@question), + class: "button hollow float-right" %> +
<%= t("admin.questions.show.answer") %><%= t("admin.questions.show.description") %><%= t("admin.questions.show.answers.title") %><%= t("admin.questions.show.answers.description") %>
<%= answer.title %> <%= answer.description %>
<% if @question.video_url.present? %> diff --git a/config/locales/en/admin.yml b/config/locales/en/admin.yml index fc7082edf..cf92d0cba 100644 --- a/config/locales/en/admin.yml +++ b/config/locales/en/admin.yml @@ -593,10 +593,12 @@ en: title: Title valid_answers: Valid answers add_answer: "Add answer" - answer: "Answer" description: Description video_url: External video documents: Documents (1) + answers: + title: Answer + description: Description answers: new: title: "New answer" diff --git a/config/locales/en/responders.yml b/config/locales/en/responders.yml index 203d5700e..fd187c495 100644 --- a/config/locales/en/responders.yml +++ b/config/locales/en/responders.yml @@ -8,6 +8,7 @@ en: direct_message: "You message has been sent successfully." poll: "Poll created successfully." poll_booth: "Booth created successfully." + poll_question_answer: "Answer created successfully" proposal: "Proposal created successfully." proposal_notification: "Your message has been sent correctly." spending_proposal: "Spending proposal created successfully. You can access it from %{activity}" diff --git a/config/locales/es/admin.yml b/config/locales/es/admin.yml index 818e3c569..ed4e2f87b 100644 --- a/config/locales/es/admin.yml +++ b/config/locales/es/admin.yml @@ -593,11 +593,13 @@ es: title: Título valid_answers: Respuestas válidas add_answer: "Añadir respuesta" - answer: "Respuesta" description: Descripción video_url: Video externo documents: Documentos (1) preview: Ver en la web + answers: + title: Respuesta + description: Descripción answers: new: title: "Nueva respuesta" diff --git a/config/locales/es/responders.yml b/config/locales/es/responders.yml index 74e3ea20a..a35387ff3 100644 --- a/config/locales/es/responders.yml +++ b/config/locales/es/responders.yml @@ -8,6 +8,7 @@ es: direct_message: "Tu mensaje ha sido enviado correctamente." poll: "Votación creada correctamente." poll_booth: "Urna creada correctamente." + poll_question_answer: "Respuesta creada correctamente" proposal: "Propuesta creada correctamente." proposal_notification: "Tu message ha sido enviado correctamente." spending_proposal: "Propuesta de inversión creada correctamente. Puedes acceder a ella desde %{activity}" diff --git a/spec/factories.rb b/spec/factories.rb index 9f1fca8ed..640b50bf2 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -501,6 +501,12 @@ FactoryGirl.define do valid_answers { Faker::Lorem.words(3).join(', ') } end + factory :poll_question_answer, class: 'Poll::QuestionAnswer' do + association :question, factory: :poll_question + sequence(:title) { |n| "Question title #{n}" } + sequence(:description) { |n| "Question description #{n}" } + end + factory :poll_booth, class: 'Poll::Booth' do sequence(:name) { |n| "Booth #{n}" } sequence(:location) { |n| "Street #{n}" } diff --git a/spec/features/polls/answers_spec.rb b/spec/features/polls/answers_spec.rb new file mode 100644 index 000000000..5b1091134 --- /dev/null +++ b/spec/features/polls/answers_spec.rb @@ -0,0 +1,41 @@ +require 'rails_helper' + +feature 'Answers' do + + background do + admin = create(:administrator) + login_as(admin.user) + end + + scenario "Index" do + question = create(:poll_question) + answer1 = create(:poll_question_answer, question: question) + answer2 = create(:poll_question_answer, question: question) + + visit admin_question_path(question) + + expect(page).to have_css(".poll_question_answer", count: 2) + + within("#poll_question_answer_#{answer1.id}") do + expect(page).to have_content answer1.title + expect(page).to have_content answer1.description + end + end + + scenario "Create" do + question = create(:poll_question) + + visit admin_question_path(question) + + click_link "Add answer" + fill_in "poll_question_answer_title", with: "¿Would you like to reform Central Park?" + fill_in "poll_question_answer_description", with: "Adding more trees, creating a play area..." + click_button "Save" + + expect(page).to have_content "Answer created successfully" + end + + pending "Update" + pending "Destroy" + +end \ No newline at end of file From 461410a79dcd6e8f597babd62013144edb71bc7f Mon Sep 17 00:00:00 2001 From: rgarcia Date: Wed, 4 Oct 2017 17:06:29 +0200 Subject: [PATCH 6/9] adds consistency to answer's namespace --- .../admin/poll/{ => questions}/answers_controller.rb | 6 +++--- app/models/poll/question.rb | 2 +- app/models/poll/{question_answer.rb => question/answer.rb} | 2 +- app/views/admin/poll/{ => questions}/answers/_form.html.erb | 0 app/views/admin/poll/{ => questions}/answers/new.html.erb | 0 config/routes.rb | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) rename app/controllers/admin/poll/{ => questions}/answers_controller.rb (75%) rename app/models/poll/{question_answer.rb => question/answer.rb} (77%) rename app/views/admin/poll/{ => questions}/answers/_form.html.erb (100%) rename app/views/admin/poll/{ => questions}/answers/new.html.erb (100%) diff --git a/app/controllers/admin/poll/answers_controller.rb b/app/controllers/admin/poll/questions/answers_controller.rb similarity index 75% rename from app/controllers/admin/poll/answers_controller.rb rename to app/controllers/admin/poll/questions/answers_controller.rb index 5650ab773..d0a7c4cc7 100644 --- a/app/controllers/admin/poll/answers_controller.rb +++ b/app/controllers/admin/poll/questions/answers_controller.rb @@ -1,14 +1,14 @@ -class Admin::Poll::AnswersController < Admin::Poll::BaseController +class Admin::Poll::Questions::AnswersController < Admin::Poll::BaseController before_action :load_question load_and_authorize_resource :question, class: "::Poll::Question" def new - @answer = Poll::QuestionAnswer.new + @answer = ::Poll::Question::Answer.new end def create - @answer = Poll::QuestionAnswer.new(answer_params) + @answer = ::Poll::Question::Answer.new(answer_params) if @answer.save redirect_to admin_question_path(@question), diff --git a/app/models/poll/question.rb b/app/models/poll/question.rb index 5d370509b..af29143ae 100644 --- a/app/models/poll/question.rb +++ b/app/models/poll/question.rb @@ -15,7 +15,7 @@ class Poll::Question < ActiveRecord::Base has_many :comments, as: :commentable has_many :answers - has_many :question_answers, class_name: 'Poll::QuestionAnswer', foreign_key: 'poll_question_id' + has_many :question_answers, class_name: 'Poll::Question::Answer', foreign_key: 'poll_question_id' has_many :partial_results belongs_to :proposal diff --git a/app/models/poll/question_answer.rb b/app/models/poll/question/answer.rb similarity index 77% rename from app/models/poll/question_answer.rb rename to app/models/poll/question/answer.rb index 871336cfd..641512263 100644 --- a/app/models/poll/question_answer.rb +++ b/app/models/poll/question/answer.rb @@ -1,4 +1,4 @@ -class Poll::QuestionAnswer < ActiveRecord::Base +class Poll::Question::Answer < ActiveRecord::Base belongs_to :question, class_name: 'Poll::Question', foreign_key: 'poll_question_id' validates :title, presence: true diff --git a/app/views/admin/poll/answers/_form.html.erb b/app/views/admin/poll/questions/answers/_form.html.erb similarity index 100% rename from app/views/admin/poll/answers/_form.html.erb rename to app/views/admin/poll/questions/answers/_form.html.erb diff --git a/app/views/admin/poll/answers/new.html.erb b/app/views/admin/poll/questions/answers/new.html.erb similarity index 100% rename from app/views/admin/poll/answers/new.html.erb rename to app/views/admin/poll/questions/answers/new.html.erb diff --git a/config/routes.rb b/config/routes.rb index 4bead575d..89691feac 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -301,7 +301,7 @@ Rails.application.routes.draw do end resources :questions do - resources :answers, only: [:new, :create] + resources :answers, only: [:new, :create], controller: 'questions/answers' end end From 4aaf681d2d32271cc9259fd8f4a1456168a1b4aa Mon Sep 17 00:00:00 2001 From: rgarcia Date: Wed, 4 Oct 2017 17:45:51 +0200 Subject: [PATCH 7/9] uses new answer model in user facing interface --- app/controllers/polls/questions_controller.rb | 2 +- app/models/poll/answer.rb | 6 ++++-- app/models/poll/question.rb | 4 ++-- app/models/poll/question/answer.rb | 2 +- .../poll/questions/answers/_form.html.erb | 2 +- app/views/polls/questions/_answers.html.erb | 19 ++++++++++--------- ..._rename_poll_question_id_to_question_id.rb | 5 +++++ db/schema.rb | 8 ++++---- 8 files changed, 28 insertions(+), 20 deletions(-) create mode 100644 db/migrate/20171004151553_rename_poll_question_id_to_question_id.rb diff --git a/app/controllers/polls/questions_controller.rb b/app/controllers/polls/questions_controller.rb index 74266a6fc..490b32fbe 100644 --- a/app/controllers/polls/questions_controller.rb +++ b/app/controllers/polls/questions_controller.rb @@ -12,7 +12,7 @@ class Polls::QuestionsController < ApplicationController answer.save! answer.record_voter_participation - @answers_by_question_id = {@question.id => params[:answer]} + @answers_by_question_id = { @question.id => params[:answer] } end end diff --git a/app/models/poll/answer.rb b/app/models/poll/answer.rb index 01eebd553..2378de6ba 100644 --- a/app/models/poll/answer.rb +++ b/app/models/poll/answer.rb @@ -8,8 +8,10 @@ class Poll::Answer < ActiveRecord::Base validates :question, presence: true validates :author, presence: true validates :answer, presence: true - validates :answer, inclusion: { in: ->(a) { a.question.valid_answers }}, - unless: ->(a) { a.question.blank? } + + # temporary skipping validation, review when removing valid_answers + # validates :answer, inclusion: { in: ->(a) { a.question.valid_answers }}, + # unless: ->(a) { a.question.blank? } scope :by_author, ->(author_id) { where(author_id: author_id) } scope :by_question, ->(question_id) { where(question_id: question_id) } diff --git a/app/models/poll/question.rb b/app/models/poll/question.rb index af29143ae..1e94e0180 100644 --- a/app/models/poll/question.rb +++ b/app/models/poll/question.rb @@ -14,8 +14,8 @@ class Poll::Question < ActiveRecord::Base belongs_to :author, -> { with_hidden }, class_name: 'User', foreign_key: 'author_id' has_many :comments, as: :commentable - has_many :answers - has_many :question_answers, class_name: 'Poll::Question::Answer', foreign_key: 'poll_question_id' + has_many :answers, class_name: 'Poll::Answer' + has_many :question_answers, class_name: 'Poll::Question::Answer' has_many :partial_results belongs_to :proposal diff --git a/app/models/poll/question/answer.rb b/app/models/poll/question/answer.rb index 641512263..3f8f4a172 100644 --- a/app/models/poll/question/answer.rb +++ b/app/models/poll/question/answer.rb @@ -1,5 +1,5 @@ class Poll::Question::Answer < ActiveRecord::Base - belongs_to :question, class_name: 'Poll::Question', foreign_key: 'poll_question_id' + belongs_to :question, class_name: 'Poll::Question', foreign_key: 'question_id' validates :title, presence: true diff --git a/app/views/admin/poll/questions/answers/_form.html.erb b/app/views/admin/poll/questions/answers/_form.html.erb index 7da1afd26..7c6ce8673 100644 --- a/app/views/admin/poll/questions/answers/_form.html.erb +++ b/app/views/admin/poll/questions/answers/_form.html.erb @@ -2,7 +2,7 @@ <%= render 'shared/errors', resource: @answer %> - <%= f.hidden_field :poll_question_id, value: @question.id %> + <%= f.hidden_field :question_id, value: @question.id %>
diff --git a/app/views/polls/questions/_answers.html.erb b/app/views/polls/questions/_answers.html.erb index c88156b68..2bef6de72 100644 --- a/app/views/polls/questions/_answers.html.erb +++ b/app/views/polls/questions/_answers.html.erb @@ -1,22 +1,23 @@
<% if can? :answer, question %> - <% question.valid_answers.each do |answer| %> - <% if @answers_by_question_id[question.id] == answer %> - "> - <%= answer %> + <% question.question_answers.each do |answer| %> + <% if @answers_by_question_id[question.id] == answer.title %> + "> + <%= answer.title %> <% else %> - <%= link_to answer, - answer_question_path(question, answer: answer), + <%= link_to answer.title, + answer_question_path(question, answer: answer.title), method: :post, remote: true, - title: t("poll_questions.show.vote_answer", answer: answer), + title: t("poll_questions.show.vote_answer", answer: answer.title), class: "button secondary hollow" %> <% end %> <% end %> <% else %> - <% question.valid_answers.each do |answer| %> - <%= answer %> + <% question.question_answers.each do |answer| %> + <%= answer.title %> <% end %> <% end %>
diff --git a/db/migrate/20171004151553_rename_poll_question_id_to_question_id.rb b/db/migrate/20171004151553_rename_poll_question_id_to_question_id.rb new file mode 100644 index 000000000..c53298bfb --- /dev/null +++ b/db/migrate/20171004151553_rename_poll_question_id_to_question_id.rb @@ -0,0 +1,5 @@ +class RenamePollQuestionIdToQuestionId < ActiveRecord::Migration + def change + rename_column :poll_question_answers, :poll_question_id, :question_id + end +end diff --git a/db/schema.rb b/db/schema.rb index 959066d2f..dbbd3ec46 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: 20171004025903) do +ActiveRecord::Schema.define(version: 20171004151553) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -671,10 +671,10 @@ ActiveRecord::Schema.define(version: 20171004025903) do create_table "poll_question_answers", force: :cascade do |t| t.string "title" t.text "description" - t.integer "poll_question_id" + t.integer "question_id" end - add_index "poll_question_answers", ["poll_question_id"], name: "index_poll_question_answers_on_poll_question_id", using: :btree + add_index "poll_question_answers", ["question_id"], name: "index_poll_question_answers_on_question_id", using: :btree create_table "poll_questions", force: :cascade do |t| t.integer "proposal_id" @@ -1152,7 +1152,7 @@ ActiveRecord::Schema.define(version: 20171004025903) do add_foreign_key "poll_partial_results", "poll_officer_assignments", column: "officer_assignment_id" add_foreign_key "poll_partial_results", "poll_questions", column: "question_id" add_foreign_key "poll_partial_results", "users", column: "author_id" - add_foreign_key "poll_question_answers", "poll_questions" + add_foreign_key "poll_question_answers", "poll_questions", column: "question_id" add_foreign_key "poll_questions", "polls" add_foreign_key "poll_questions", "proposals" add_foreign_key "poll_questions", "users", column: "author_id" From 007bcb7ec764f4c95c4c26f1f9fcc7f6a591bf48 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Wed, 4 Oct 2017 17:46:25 +0200 Subject: [PATCH 8/9] fixes specs --- spec/factories.rb | 2 +- spec/features/polls/polls_spec.rb | 56 +++++++++++++++++++++++++------ spec/features/polls/voter_spec.rb | 8 +++-- spec/models/poll/answer_spec.rb | 1 + 4 files changed, 54 insertions(+), 13 deletions(-) diff --git a/spec/factories.rb b/spec/factories.rb index 640b50bf2..a28471efc 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -501,7 +501,7 @@ FactoryGirl.define do valid_answers { Faker::Lorem.words(3).join(', ') } end - factory :poll_question_answer, class: 'Poll::QuestionAnswer' do + factory :poll_question_answer, class: 'Poll::Question::Answer' do association :question, factory: :poll_question sequence(:title) { |n| "Question title #{n}" } sequence(:description) { |n| "Question description #{n}" } diff --git a/spec/features/polls/polls_spec.rb b/spec/features/polls/polls_spec.rb index 8a90fb4a8..683c0402a 100644 --- a/spec/features/polls/polls_spec.rb +++ b/spec/features/polls/polls_spec.rb @@ -73,7 +73,10 @@ feature 'Polls' do end scenario 'Non-logged in users' do - create(:poll_question, poll: poll, valid_answers: 'Han Solo, Chewbacca') + question = create(:poll_question, poll: poll) + answer1 = create(:poll_question_answer, question: question, title: 'Han Solo') + answer2 = create(:poll_question_answer, question: question, title: 'Chewbacca') + visit poll_path(poll) expect(page).to have_content('Han Solo') @@ -87,7 +90,11 @@ feature 'Polls' do scenario 'Level 1 users' do poll.update(geozone_restricted: true) poll.geozones << geozone - create(:poll_question, poll: poll, valid_answers: 'Han Solo, Chewbacca') + + question = create(:poll_question, poll: poll) + answer1 = create(:poll_question_answer, question: question, title: 'Han Solo') + answer2 = create(:poll_question_answer, question: question, title: 'Chewbacca') + login_as(create(:user, geozone: geozone)) visit poll_path(poll) @@ -103,7 +110,11 @@ feature 'Polls' do scenario 'Level 2 users in an incoming poll' do incoming_poll = create(:poll, :incoming, geozone_restricted: true) incoming_poll.geozones << geozone - create(:poll_question, poll: incoming_poll, valid_answers: 'Rey, Finn') + + question = create(:poll_question, poll: incoming_poll) + answer1 = create(:poll_question_answer, question: question, title: 'Rey') + answer2 = create(:poll_question_answer, question: question, title: 'Finn') + login_as(create(:user, :level_two, geozone: geozone)) visit poll_path(incoming_poll) @@ -119,7 +130,11 @@ feature 'Polls' do scenario 'Level 2 users in an expired poll' do expired_poll = create(:poll, :expired, geozone_restricted: true) expired_poll.geozones << geozone - create(:poll_question, poll: expired_poll, valid_answers: 'Luke, Leia') + + question = create(:poll_question, poll: expired_poll) + answer1 = create(:poll_question_answer, question: question, title: 'Luke') + answer2 = create(:poll_question_answer, question: question, title: 'Leia') + login_as(create(:user, :level_two, geozone: geozone)) visit poll_path(expired_poll) @@ -135,7 +150,11 @@ feature 'Polls' do scenario 'Level 2 users in a poll with questions for a geozone which is not theirs' do poll.update(geozone_restricted: true) poll.geozones << create(:geozone) - create(:poll_question, poll: poll, valid_answers: 'Vader, Palpatine') + + question = create(:poll_question, poll: poll) + answer1 = create(:poll_question_answer, question: question, title: 'Vader') + answer2 = create(:poll_question_answer, question: question, title: 'Palpatine') + login_as(create(:user, :level_two)) visit poll_path(poll) @@ -149,7 +168,11 @@ feature 'Polls' do scenario 'Level 2 users reading a same-geozone poll' do poll.update(geozone_restricted: true) poll.geozones << geozone - create(:poll_question, poll: poll, valid_answers: 'Han Solo, Chewbacca') + + question = create(:poll_question, poll: poll) + answer1 = create(:poll_question_answer, question: question, title: 'Han Solo') + answer2 = create(:poll_question_answer, question: question, title: 'Chewbacca') + login_as(create(:user, :level_two, geozone: geozone)) visit poll_path(poll) @@ -158,7 +181,10 @@ feature 'Polls' do end scenario 'Level 2 users reading a all-geozones poll' do - create(:poll_question, poll: poll, valid_answers: 'Han Solo, Chewbacca') + question = create(:poll_question, poll: poll) + answer1 = create(:poll_question_answer, question: question, title: 'Han Solo') + answer2 = create(:poll_question_answer, question: question, title: 'Chewbacca') + login_as(create(:user, :level_two)) visit poll_path(poll) @@ -167,7 +193,9 @@ feature 'Polls' do end scenario 'Level 2 users who have already answered' do - question = create(:poll_question, poll: poll, valid_answers: 'Han Solo, Chewbacca') + question = create(:poll_question, poll: poll) + answer1 = create(:poll_question_answer, question: question, title: 'Han Solo') + answer2 = create(:poll_question_answer, question: question, title: 'Chewbacca') user = create(:user, :level_two) create(:poll_answer, question: question, author: user, answer: 'Chewbacca') @@ -182,7 +210,11 @@ feature 'Polls' do scenario 'Level 2 users answering', :js do poll.update(geozone_restricted: true) poll.geozones << geozone - create(:poll_question, poll: poll, valid_answers: 'Han Solo, Chewbacca') + + question = create(:poll_question, poll: poll) + answer1 = create(:poll_question_answer, question: question, title: 'Han Solo') + answer2 = create(:poll_question_answer, question: question, title: 'Chewbacca') + user = create(:user, :level_two, geozone: geozone) login_as user @@ -197,7 +229,11 @@ feature 'Polls' do scenario 'Level 2 users changing answer', :js do poll.update(geozone_restricted: true) poll.geozones << geozone - create(:poll_question, poll: poll, valid_answers: 'Han Solo, Chewbacca') + + question = create(:poll_question, poll: poll) + answer1 = create(:poll_question_answer, question: question, title: 'Han Solo') + answer2 = create(:poll_question_answer, question: question, title: 'Chewbacca') + user = create(:user, :level_two, geozone: geozone) login_as user diff --git a/spec/features/polls/voter_spec.rb b/spec/features/polls/voter_spec.rb index d925d3394..745489c32 100644 --- a/spec/features/polls/voter_spec.rb +++ b/spec/features/polls/voter_spec.rb @@ -6,7 +6,9 @@ feature "Voter" do scenario "Voting via web", :js do poll = create(:poll) - question = create(:poll_question, poll: poll, valid_answers: 'Yes, No') + question = create(:poll_question, poll: poll) + answer1 = create(:poll_question_answer, question: question, title: 'Yes') + answer2 = create(:poll_question_answer, question: question, title: 'No') user = create(:user, :level_two) login_as user @@ -48,7 +50,9 @@ feature "Voter" do context "Trying to vote the same poll in booth and web" do let(:poll) { create(:poll) } - let(:question) { create(:poll_question, poll: poll, valid_answers: 'Yes, No') } + let(:question) { create(:poll_question, poll: poll) } + let!(:answer1) { create(:poll_question_answer, question: question, title: 'Yes') } + let!(:answer2) { create(:poll_question_answer, question: question, title: 'No') } let!(:user) { create(:user, :in_census) } let(:officer) { create(:poll_officer) } diff --git a/spec/models/poll/answer_spec.rb b/spec/models/poll/answer_spec.rb index d245af8d4..2c27bc060 100644 --- a/spec/models/poll/answer_spec.rb +++ b/spec/models/poll/answer_spec.rb @@ -26,6 +26,7 @@ describe Poll::Answer do end it "should be valid for answers included in the Poll::Question's list" do + skip "review when removing valid_answers" question = create(:poll_question, valid_answers: 'One, Two, Three') expect(build(:poll_answer, question: question, answer: 'One')).to be_valid expect(build(:poll_answer, question: question, answer: 'Two')).to be_valid From adda4f9d33cbb30a473fbde1bb593dd2fe08d932 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Wed, 4 Oct 2017 18:08:55 +0200 Subject: [PATCH 9/9] removes unused translation --- config/locales/en/admin.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/config/locales/en/admin.yml b/config/locales/en/admin.yml index cf92d0cba..a31afe4ec 100644 --- a/config/locales/en/admin.yml +++ b/config/locales/en/admin.yml @@ -593,7 +593,6 @@ en: title: Title valid_answers: Valid answers add_answer: "Add answer" - description: Description video_url: External video documents: Documents (1) answers: