Class: Decidim::Meetings::Admin::MeetingsPollController

Inherits:
ApplicationController
  • Object
show all
Includes:
TranslatableAttributes
Defined in:
app/controllers/decidim/meetings/admin/meetings_poll_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#maps_enabled?, #meetings

Instance Method Details

#answer_optionsObject



55
56
57
58
59
60
61
62
63
# File 'app/controllers/decidim/meetings/admin/meetings_poll_controller.rb', line 55

def answer_options
  respond_to do |format|
    format.json do
      question_id = params["id"]
      question = Decidim::Meetings::Question.find_by(id: question_id)
      render json: question.answer_options.map { |answer_option| AnswerOptionPresenter.new(answer_option).as_json } if question.present?
    end
  end
end

#answer_options_url(params) ⇒ Object

Returns the url to get the answer options json (for the display conditions form) for the question with id = params



71
72
73
# File 'app/controllers/decidim/meetings/admin/meetings_poll_controller.rb', line 71

def answer_options_url(params)
  url_for([questionnaire.questionnaire_for, { action: :answer_options, format: :json, **params }])
end

#editObject



15
16
17
18
19
20
21
# File 'app/controllers/decidim/meetings/admin/meetings_poll_controller.rb', line 15

def edit
  enforce_permission_to(:update, :poll, meeting:, poll:)

  @form = form(Admin::QuestionnaireForm).from_model(questionnaire)

  render template: "decidim/meetings/admin/poll/edit"
end

#edit_questionnaire_titleObject

Implement this method in your controller to set the title of the edit form.



77
78
79
# File 'app/controllers/decidim/meetings/admin/meetings_poll_controller.rb', line 77

def edit_questionnaire_title
  t(:title, scope: "decidim.meetings.admin.meetings_poll.form", questionnaire_for: translated_attribute(meeting.try(:title)))
end

#questionnaire_forObject



65
66
67
# File 'app/controllers/decidim/meetings/admin/meetings_poll_controller.rb', line 65

def questionnaire_for
  poll
end

#updateObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/controllers/decidim/meetings/admin/meetings_poll_controller.rb', line 23

def update
  enforce_permission_to(:update, :poll, meeting:, poll:)

  current_questions_forms = form(Admin::QuestionnaireForm).from_model(questionnaire).questions
  @form = form(Admin::QuestionnaireForm).from_params(params)

  # Although the question values (except the position) will be ignored if they are not editable,
  # its information is completed so that if any update failure occurs, the form is rendered again
  # with the full data for the disabled questions.
  @form.questions = @form.questions.map do |question_form|
    next question_form if question_form.editable?

    full_question_form = current_questions_forms.find { |form| form.id.to_s == question_form.id.to_s }
    full_question_form.position = question_form.position
    full_question_form
  end

  Admin::UpdateQuestionnaire.call(@form, questionnaire) do
    on(:ok) do
      # i18n-tasks-use t("decidim.forms.admin.questionnaires.update.success")
      flash[:notice] = I18n.t("update.success", scope: "decidim.meetings.admin.meetings_poll")
      redirect_to after_update_url
    end

    on(:invalid) do
      # i18n-tasks-use t("decidim.forms.admin.questionnaires.update.invalid")
      flash.now[:alert] = I18n.t("update.invalid", scope: "decidim.meetings.admin.meetings_poll")
      render template: "decidim/meetings/admin/poll/edit"
    end
  end
end