Class: Decidim::Forms::ResponseForm

Inherits:
Decidim::Form
  • Object
show all
Includes:
AttachmentAttributes, TranslationsHelper
Defined in:
app/forms/decidim/forms/response_form.rb

Overview

This class holds a Form to save the questionnaire responses from Decidim’s public page

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#questionObject



30
31
32
# File 'app/forms/decidim/forms/response_form.rb', line 30

def question
  @question ||= Decidim::Forms::Question.find(question_id)
end

Instance Method Details

#custom_choicesObject



58
59
60
# File 'app/forms/decidim/forms/response_form.rb', line 58

def custom_choices
  choices.select(&:custom_body)
end

#display_conditions_fulfilled?Boolean

Returns:

  • (Boolean)


62
63
64
65
66
# File 'app/forms/decidim/forms/response_form.rb', line 62

def display_conditions_fulfilled?
  return optional_conditions_fulfilled? unless question.display_conditions.where(mandatory: true).any?

  mandatory_conditions_fulfilled?
end

#has_attachments?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'app/forms/decidim/forms/response_form.rb', line 84

def has_attachments?
  question.has_attachments? && errors[:add_documents].empty? && add_documents.present?
end

#has_error_in_attachments?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'app/forms/decidim/forms/response_form.rb', line 88

def has_error_in_attachments?
  errors[:add_documents].present?
end

#labelObject



34
35
36
37
38
39
# File 'app/forms/decidim/forms/response_form.rb', line 34

def label
  base = translated_attribute(question.body)
  base += " #{mandatory_label}" if question.mandatory?
  base += " (#{max_choices_label})" if question.max_choices
  base
end

#mandatory_conditions_fulfilled?Boolean

Returns:

  • (Boolean)


68
69
70
71
72
73
# File 'app/forms/decidim/forms/response_form.rb', line 68

def mandatory_conditions_fulfilled?
  question.display_conditions.where(mandatory: true).all? do |condition|
    response = context.responses&.find { |r| r.question_id&.to_i == condition.condition_question.id }
    condition.fulfilled?(response)
  end
end

#map_model(model) ⇒ Object

Public: Map the correct fields.

Returns nothing.



44
45
46
47
48
49
50
51
52
# File 'app/forms/decidim/forms/response_form.rb', line 44

def map_model(model)
  self.question_id = model.decidim_question_id
  self.question = model.question
  self.documents = model.attachments

  self.choices = model.choices.map do |choice|
    ResponseChoiceForm.from_model(choice)
  end
end

#optional_conditions_fulfilled?Boolean

Returns:

  • (Boolean)


75
76
77
78
79
80
81
82
# File 'app/forms/decidim/forms/response_form.rb', line 75

def optional_conditions_fulfilled?
  return true unless question.display_conditions.where(mandatory: false).any?

  question.display_conditions.where(mandatory: false).any? do |condition|
    response = context.responses&.find { |r| r.question_id&.to_i == condition.condition_question.id }
    condition.fulfilled?(response)
  end
end

#selected_choicesObject



54
55
56
# File 'app/forms/decidim/forms/response_form.rb', line 54

def selected_choices
  choices.select(&:body)
end

#sorting?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'app/forms/decidim/forms/response_form.rb', line 92

def sorting?
  question.question_type == "sorting"
end