Sha256: c441fb2197898ec383d99017841609ab2ad8d3368acb591477f2b041b5a6954f

Contents?: true

Size: 1.62 KB

Versions: 3

Compression:

Stored size: 1.62 KB

Contents

# frozen_string_literal: true

module Decidim
  module ParticipatoryDocuments
    module Admin
      class AnswerSuggestion < Decidim::Command
        # Public: Initializes the command.
        #
        # form - A form object with the params.
        def initialize(form, suggestion)
          @form = form
          @suggestion = suggestion
        end

        def call
          return broadcast(:invalid) if form.invalid?

          store_initial_suggestion_state

          suggestion.assign_attributes(attributes)
          if (%w(state answer answer_is_published) & suggestion.changed).any?
            transaction { answer_suggestion }
            notify_suggestion_answer
          end

          broadcast(:ok)
        end

        private

        attr_reader :form, :suggestion, :initial_state

        def attributes
          {
            state: form.state,
            answer: form.answer,
            answer_is_published: form.answer_is_published,
            answered_at: %w(not_answered withdrawn).include?(form.state) ? nil : Time.current
          }
        end

        def answer_suggestion
          Decidim.traceability.perform_action!("answer", suggestion, form.current_user) do
            suggestion.save!
            suggestion
          end
        end

        def notify_suggestion_answer
          suggestion.reload
          return unless suggestion.has_answer? && suggestion.answered? && suggestion.answer_is_published?

          NotifySuggestionAnswer.call(suggestion, initial_state)
        end

        def store_initial_suggestion_state
          @initial_state = suggestion.state
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
decidim-participatory_documents-0.2.2 app/commands/decidim/participatory_documents/admin/answer_suggestion.rb
decidim-participatory_documents-0.2.1 app/commands/decidim/participatory_documents/admin/answer_suggestion.rb
decidim-participatory_documents-0.2.0 app/commands/decidim/participatory_documents/admin/answer_suggestion.rb