Class: Decidim::Forms::DisplayCondition

Inherits:
ApplicationRecord show all
Defined in:
app/models/decidim/forms/display_condition.rb

Overview

The data store for a DisplayCondition in the Decidim::Forms component. A display condition is associated to two questions. :question is the question that we want to display or hide based on some conditions, and :condition_question is the question the answers of which are checked against the conditions. Conditions can be whether the question is answered (“answered”) or it is not (“not_answered”), if the selected answer option is (“equal”) or is not (“not_equal”) a given one, or whether the text value of the answer matches a string (“match”).

Instance Method Summary collapse

Instance Method Details

#fulfilled?(answer_form) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/models/decidim/forms/display_condition.rb', line 32

def fulfilled?(answer_form)
  return answer_form.present? if condition_type == "answered"
  return answer_form.blank? if condition_type == "not_answered"
  # rest of options require presence
  return if answer_form.blank?

  case condition_type
  when "equal"
    answer_form.choices.pluck(:answer_option_id).include?(answer_option.id)
  when "not_equal"
    answer_form.choices.pluck(:answer_option_id).exclude?(answer_option.id)
  when "match"
    condition_value.values.compact_blank.any? { |value| answer_form_matches?(answer_form, value) }
  end
end

#to_html_dataObject



48
49
50
51
52
53
54
55
56
57
# File 'app/models/decidim/forms/display_condition.rb', line 48

def to_html_data
  {
    id:,
    type: condition_type,
    condition: decidim_condition_question_id,
    option: decidim_answer_option_id,
    mandatory:,
    value: condition_value&.dig(I18n.locale.to_s)
  }.compact
end