app/controllers/concerns/decidim/consultations/needs_question.rb in decidim-consultations-0.17.2 vs app/controllers/concerns/decidim/consultations/needs_question.rb in decidim-consultations-0.18.0
- old
+ new
@@ -5,11 +5,11 @@
# This module, when injected into a controller, ensures there's a
# question available and deducts it from the context.
module NeedsQuestion
def self.enhance_controller(instance_or_module)
instance_or_module.class_eval do
- helper_method :current_question, :current_consultation, :current_participatory_space, :stats,
+ helper_method :current_question, :previous_question, :next_question, :current_consultation, :current_participatory_space, :stats,
:sorted_results
helper Decidim::WidgetUrlsHelper
end
end
@@ -33,10 +33,30 @@
# Returns the current Question.
def current_question
@current_question ||= detect_question
end
+ # Public: Finds the previous Question in the Array of questions
+ # associated to the current_question's consultation.
+ #
+ # Returns the previous Question in the Array or nil if out of bounds.
+ def previous_question
+ return nil if (current_question_index - 1).negative?
+
+ current_consultation_questions.at(current_question_index - 1)
+ end
+
+ # Public: Finds the next Question in the Array of questions
+ # associated to the current_question's consultation.
+ #
+ # Returns the next Question in the Array or nil if out of bounds.
+ def next_question
+ return nil if current_question_index + 1 >= current_consultation_questions.size
+
+ current_consultation_questions.at(current_question_index + 1)
+ end
+
# Public: Finds the current Consultation given this controller's
# context.
#
# Returns the current Consultation.
def current_consultation
@@ -61,9 +81,17 @@
@organization_consultations ||= OrganizationConsultations.new(current_organization).query
end
def stats
@stats ||= QuestionStatsPresenter.new(question: current_question)
+ end
+
+ def current_consultation_questions
+ @current_consultation_questions ||= current_question.consultation.questions.to_a
+ end
+
+ def current_question_index
+ current_consultation_questions.find_index(current_question)
end
end
end
end
end