Show link to the first question
This commit is contained in:
@@ -14,8 +14,14 @@ class Legislation::Question < ActiveRecord::Base
|
||||
validates :process, presence: true
|
||||
validates :title, presence: true
|
||||
|
||||
scope :sorted, -> { order('id ASC') }
|
||||
|
||||
def next_question_id
|
||||
@next_question_id ||= process.questions.where("id > ?", id).order('id ASC').limit(1).pluck(:id).first
|
||||
@next_question_id ||= process.questions.where("id > ?", id).sorted.limit(1).pluck(:id).first
|
||||
end
|
||||
|
||||
def first_question_id
|
||||
@first_question_id ||= process.questions.sorted.limit(1).pluck(:id).first
|
||||
end
|
||||
|
||||
def answer_for_user(user)
|
||||
|
||||
@@ -16,6 +16,13 @@
|
||||
<span class="icon-angle-right" aria-hidden="true">
|
||||
<% end %>
|
||||
<% end %>
|
||||
<% elsif @question.first_question_id %>
|
||||
<%= link_to legislation_process_question_path(@process, @question.first_question_id), class: "quiz-next-link" do %>
|
||||
<%= content_tag :div, class: "quiz-next" do %>
|
||||
<%= t('.first_question') %>
|
||||
<span class="icon-angle-right" aria-hidden="true">
|
||||
<% end %>
|
||||
<% end %>
|
||||
<% end %>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -281,6 +281,7 @@ en:
|
||||
answer_question: Submit answer
|
||||
comments: Comments
|
||||
next_question: Next question
|
||||
first_question: First question
|
||||
share: Share
|
||||
share_twitter: Share on Twitter
|
||||
share_facebook: Share on Facebook
|
||||
|
||||
@@ -281,6 +281,7 @@ es:
|
||||
answer_question: Enviar respuesta
|
||||
comments: Comentarios
|
||||
next_question: Siguiente pregunta
|
||||
first_question: Primera pregunta
|
||||
share: Compartir
|
||||
share_twitter: Compartir en Twitter
|
||||
share_facebook: Compartir en Facebook
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Legislation::Question, type: :model do
|
||||
let(:legislation_question) { build(:legislation_question) }
|
||||
let(:question) { create(:legislation_question) }
|
||||
|
||||
it "should be valid" do
|
||||
expect(legislation_question).to be_valid
|
||||
expect(question).to be_valid
|
||||
end
|
||||
|
||||
context "can be deleted" do
|
||||
@@ -17,7 +17,6 @@ RSpec.describe Legislation::Question, type: :model do
|
||||
end
|
||||
|
||||
example "when it has options but no answers" do
|
||||
question = create(:legislation_question)
|
||||
create(:legislation_question_option, question: question, value: "Yes")
|
||||
create(:legislation_question_option, question: question, value: "No")
|
||||
|
||||
@@ -27,7 +26,6 @@ RSpec.describe Legislation::Question, type: :model do
|
||||
end
|
||||
|
||||
example "when it has options and answers" do
|
||||
question = create(:legislation_question)
|
||||
option_1 = create(:legislation_question_option, question: question, value: "Yes")
|
||||
option_2 = create(:legislation_question_option, question: question, value: "No")
|
||||
create(:legislation_answer, question: question, question_option: option_1)
|
||||
@@ -38,4 +36,27 @@ RSpec.describe Legislation::Question, type: :model do
|
||||
end.to change { Legislation::Question.count }.by(-1)
|
||||
end
|
||||
end
|
||||
|
||||
describe "#next_question_id" do
|
||||
let!(:question1) { create(:legislation_question) }
|
||||
let!(:question2) { create(:legislation_question, legislation_process_id: question1.legislation_process_id) }
|
||||
|
||||
it "should return the next question" do
|
||||
expect(question1.next_question_id).to eq(question2.id)
|
||||
end
|
||||
|
||||
it "should return nil" do
|
||||
expect(question2.next_question_id).to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
describe "#first_question_id" do
|
||||
let!(:question1) { create(:legislation_question) }
|
||||
let!(:question2) { create(:legislation_question, legislation_process_id: question1.legislation_process_id) }
|
||||
|
||||
it "should return the first question" do
|
||||
expect(question1.first_question_id).to eq(question1.id)
|
||||
expect(question2.first_question_id).to eq(question1.id)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user