Skip to content

Commit

Permalink
Add optionality to answer_option
Browse files Browse the repository at this point in the history
  • Loading branch information
JoonasAapro committed Mar 21, 2024
1 parent 4e46f11 commit ec6843d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,24 @@ module AnswerChoiceExtensions
extend ActiveSupport::Concern

included do
belongs_to :location_option,
class_name: "LocationOption",
foreign_key: "decidim_location_option_id",
optional: true
# Define a module variable to track if the association has been defined
class_attribute :answer_option_association_defined
self.answer_option_association_defined = false unless self.answer_option_association_defined

# Redefine the belongs_to :answer_option association if not already defined
unless self.answer_option_association_defined
belongs_to :answer_option,
class_name: "AnswerOption",
foreign_key: "decidim_answer_option_id",
optional: true

belongs_to :answer_option,
class_name: "AnswerOption",
foreign_key: "decidim_answer_option_id",
optional: true
self.answer_option_association_defined = true
end

belongs_to :location_option,
class_name: "LocationOption",
foreign_key: "decidim_location_option_id",
optional: true
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

module Decidim
module Forms
class AnswerChoice < Forms::ApplicationRecord
belongs_to :answer,
class_name: "Answer",
foreign_key: "decidim_answer_id"

belongs_to :answer_option,
class_name: "AnswerOption",
foreign_key: "decidim_answer_option_id"

belongs_to :matrix_row,
class_name: "QuestionMatrixRow",
foreign_key: "decidim_question_matrix_row_id",
optional: true

validates :matrix_row, presence: true, if: -> { answer.question.matrix? }
end
end
end

0 comments on commit ec6843d

Please sign in to comment.