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 responses of which are checked against the conditions. Conditions can be whether the question is responded (“responded”) or it is not (“not_responded”), if the selected response option is (“equal”) or is not (“not_equal”) a given one, or whether the text value of the response matches a string (“match”).

Instance Method Summary collapse

Instance Method Details

#fulfilled?(response_form) ⇒ Boolean

Returns:

  • (Boolean)


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

def fulfilled?(response_form)
  return response_form.present? if condition_type == "responded"
  return response_form.blank? if condition_type == "not_responded"
  # rest of options require presence
  return if response_form.blank?

  case condition_type
  when "equal"
    response_form.choices.pluck(:response_option_id).include?(response_option.id)
  when "not_equal"
    response_form.choices.pluck(:response_option_id).exclude?(response_option.id)
  when "match"
    condition_value.values.compact_blank.any? { |value| response_form_matches?(response_form, value) }
  end
end

#to_html_dataObject



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

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