Class: Decidim::Forms::DisplayCondition

Inherits:
ApplicationRecord show all
Defined in:
decidim-forms/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 whethe the text value of the answer matches a string (“match”).

Instance Method Summary collapse

Instance Method Details

#fulfilled?(answer_form) ⇒ Boolean

Returns:

  • (Boolean)

24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'decidim-forms/app/models/decidim/forms/display_condition.rb', line 24

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.reject(&:blank?).any? { |value| answer_form_matches?(answer_form, value) }
  end
end

#to_html_dataObject


40
41
42
43
44
45
46
47
48
49
# File 'decidim-forms/app/models/decidim/forms/display_condition.rb', line 40

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