Sha256: c957c2cd8878d93716c94a2fa87ef55e1d04d16d9d084d26ad73a2cd97fb02fe

Contents?: true

Size: 1.47 KB

Versions: 19

Compression:

Stored size: 1.47 KB

Contents

# frozen_string_literal: true

module Decidim
  module Meetings
    module Admin
      # This command is executed when the user changes a Questionnaire from the admin
      # panel.
      class UpdateQuestionStatus < Rectify::Command
        class InvalidStatus < StandardError; end

        # Initializes a UpdateQuestionnaire Command.
        #
        # form - The form from which to get the data.
        # questionnaire - The current instance of the questionnaire to be updated.
        def initialize(question, current_user)
          @question = question
          @current_user = current_user
        end

        # Updates the questionnaire if valid.
        #
        # Broadcasts :ok if successful, :invalid otherwise.
        def call
          Decidim::Meetings::Question.transaction do
            update_question
          end

          broadcast(:ok)
        rescue InvalidStatus
          broadcast(:invalid)
        end

        private

        attr_reader :question, :current_user

        def update_question
          Decidim.traceability.update!(
            question,
            current_user,
            status: new_status(question)
          )
        end

        def new_status(question)
          if question.unpublished?
            Decidim::Meetings::Question.statuses[:published]
          elsif question.published?
            Decidim::Meetings::Question.statuses[:closed]
          else
            raise InvalidStatus
          end
        end
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
decidim-meetings-0.26.10 app/commands/decidim/meetings/admin/update_question_status.rb
decidim-meetings-0.26.9 app/commands/decidim/meetings/admin/update_question_status.rb
decidim-meetings-0.26.8 app/commands/decidim/meetings/admin/update_question_status.rb
decidim-meetings-0.26.7 app/commands/decidim/meetings/admin/update_question_status.rb
decidim-meetings-0.26.5 app/commands/decidim/meetings/admin/update_question_status.rb
decidim-meetings-0.26.4 app/commands/decidim/meetings/admin/update_question_status.rb
decidim-meetings-0.26.3 app/commands/decidim/meetings/admin/update_question_status.rb
decidim-meetings-0.26.2 app/commands/decidim/meetings/admin/update_question_status.rb
decidim-meetings-0.26.1 app/commands/decidim/meetings/admin/update_question_status.rb
decidim-meetings-0.26.0 app/commands/decidim/meetings/admin/update_question_status.rb
decidim-meetings-0.26.0.rc2 app/commands/decidim/meetings/admin/update_question_status.rb
decidim-meetings-0.26.0.rc1 app/commands/decidim/meetings/admin/update_question_status.rb
decidim-meetings-0.25.2 app/commands/decidim/meetings/admin/update_question_status.rb
decidim-meetings-0.25.1 app/commands/decidim/meetings/admin/update_question_status.rb
decidim-meetings-0.25.0 app/commands/decidim/meetings/admin/update_question_status.rb
decidim-meetings-0.25.0.rc4 app/commands/decidim/meetings/admin/update_question_status.rb
decidim-meetings-0.25.0.rc3 app/commands/decidim/meetings/admin/update_question_status.rb
decidim-meetings-0.25.0.rc2 app/commands/decidim/meetings/admin/update_question_status.rb
decidim-meetings-0.25.0.rc1 app/commands/decidim/meetings/admin/update_question_status.rb