From 2be107f4da8b7114539e9e17551e53940ab75e71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Salvador=20P=C3=A9rez=20Garc=C3=ADa?= Date: Tue, 26 Jun 2018 12:09:36 +0200 Subject: [PATCH] Specs Added specs and fixed some issues found after executing them --- app/controllers/dashboard/polls_controller.rb | 2 - app/models/poll.rb | 2 +- app/models/poll/question.rb | 4 +- config/locales/en/general.yml | 3 + config/locales/es/general.yml | 3 + spec/features/dashboard/polls_spec.rb | 71 +++++++++++++++++++ spec/models/abilities/common_spec.rb | 5 ++ 7 files changed, 85 insertions(+), 5 deletions(-) create mode 100644 spec/features/dashboard/polls_spec.rb diff --git a/app/controllers/dashboard/polls_controller.rb b/app/controllers/dashboard/polls_controller.rb index 7f5e8380c..76fa56053 100644 --- a/app/controllers/dashboard/polls_controller.rb +++ b/app/controllers/dashboard/polls_controller.rb @@ -36,8 +36,6 @@ class Dashboard::PollsController < Dashboard::BaseController def update authorize! :manage_polls, proposal - byebug - if poll.update(poll_params) redirect_to proposal_dashboard_poll_path(proposal, poll), notice: t("flash.actions.update.poll") else diff --git a/app/models/poll.rb b/app/models/poll.rb index bed81bfa0..c15c67cc2 100644 --- a/app/models/poll.rb +++ b/app/models/poll.rb @@ -13,7 +13,7 @@ class Poll < ActiveRecord::Base has_many :voters has_many :officer_assignments, through: :booth_assignments has_many :officers, through: :officer_assignments - has_many :questions + has_many :questions, inverse_of: :poll has_many :comments, as: :commentable has_and_belongs_to_many :geozones diff --git a/app/models/poll/question.rb b/app/models/poll/question.rb index e589401ac..c87b89c83 100644 --- a/app/models/poll/question.rb +++ b/app/models/poll/question.rb @@ -5,7 +5,7 @@ class Poll::Question < ActiveRecord::Base acts_as_paranoid column: :hidden_at include ActsAsParanoidAliases - belongs_to :poll + belongs_to :poll, inverse_of: :questions belongs_to :author, -> { with_hidden }, class_name: 'User', foreign_key: 'author_id' has_many :comments, as: :commentable @@ -16,7 +16,7 @@ class Poll::Question < ActiveRecord::Base validates :title, presence: true validates :author, presence: true - validates :poll_id, presence: true + validates :poll_id, presence: true, if: Proc.new { |question| question.poll.nil? } validates :title, length: { minimum: 4 } diff --git a/config/locales/en/general.yml b/config/locales/en/general.yml index d57a787d3..8e365bbd8 100644 --- a/config/locales/en/general.yml +++ b/config/locales/en/general.yml @@ -521,6 +521,9 @@ en: add_question: Add question question_fields: remove_question: Remove question + add_answer: Add answer + question_answer_fields: + remove_answer: Remove answer polls: all: "All" no_dates: "no date assigned" diff --git a/config/locales/es/general.yml b/config/locales/es/general.yml index 7684cb0e3..a630a2f6d 100644 --- a/config/locales/es/general.yml +++ b/config/locales/es/general.yml @@ -521,6 +521,9 @@ es: add_question: Añadir pregunta question_fields: remove_question: Borrar pregunta + add_answer: Añadir respuesta + question_answer_fields: + remove_answer: Borrar respuesta polls: all: "Todas" no_dates: "sin fecha asignada" diff --git a/spec/features/dashboard/polls_spec.rb b/spec/features/dashboard/polls_spec.rb new file mode 100644 index 000000000..df27c9e76 --- /dev/null +++ b/spec/features/dashboard/polls_spec.rb @@ -0,0 +1,71 @@ +require 'rails_helper' + +feature 'Polls' do + let!(:proposal) { create(:proposal, :draft) } + + before do + login_as(proposal.author) + visit proposal_dashboard_index_path(proposal) + end + + scenario 'Has a link to polls feature' do + expect(page).to have_link('Polls') + end + + scenario 'Initially there are no polls' do + click_link 'Polls' + expect(page).to have_content('There are no polls coming up.') + end + + scenario 'Create a poll', :js do + click_link 'Polls' + click_link 'Create poll' + + start_date = 1.week.from_now + end_date = 2.weeks.from_now + + fill_in "poll_name", with: "Upcoming poll" + fill_in 'poll_starts_at', with: start_date.strftime("%d/%m/%Y") + fill_in 'poll_ends_at', with: end_date.strftime("%d/%m/%Y") + fill_in 'poll_summary', with: "Upcoming poll's summary. This poll..." + fill_in 'poll_description', with: "Upcomming poll's description. This poll..." + + expect(page).not_to have_css("#poll_results_enabled") + expect(page).not_to have_css("#poll_stats_enabled") + + click_link 'Add question' + + fill_in 'Question', with: 'First question' + + click_link 'Add answer' + fill_in 'Title', with: 'First answer' + + click_button "Create poll" + + expect(page).to have_content "Poll created successfully" + expect(page).to have_content "Upcoming poll" + expect(page).to have_content "First question" + expect(page).to have_content I18n.l(start_date.to_date) + expect(page).to have_content I18n.l(end_date.to_date) + expect(page).to have_link('Results') + end + + scenario 'Update a poll', :js do + poll = create(:poll, related: proposal) + + click_link 'Polls' + + expect(page).to have_content(poll.name) + + within("#poll_#{poll.id}") do + click_link 'Edit' + end + + fill_in "poll_name", with: "Updated upcoming poll" + + click_button "Update poll" + + expect(page).to have_content "Poll updated successfully" + expect(page).to have_content "Updated upcoming poll" + end +end diff --git a/spec/models/abilities/common_spec.rb b/spec/models/abilities/common_spec.rb index 8dd9b3c9f..2260193cc 100644 --- a/spec/models/abilities/common_spec.rb +++ b/spec/models/abilities/common_spec.rb @@ -166,6 +166,11 @@ describe Abilities::Common do it { should_not be_able_to(:dashboard, proposal) } end + describe 'proposal polls' do + it { should be_able_to(:manage_polls, own_proposal) } + it { should_not be_able_to(:manage_polls, proposal) } + end + describe 'publishing proposals' do let(:draft_own_proposal) { create(:proposal, :draft, author: user) }