Class: Decidim::ParticipatoryDocuments::Admin::PublishAnswers

Inherits:
Command
  • Object
show all
Defined in:
app/commands/decidim/participatory_documents/admin/publish_answers.rb

Instance Method Summary collapse

Constructor Details

#initialize(component, user, suggestion_ids) ⇒ PublishAnswers

Public: Initializes the command.

component - The component that contains the answers. user - the Decidim::User that is publishing the answers. proposal_ids - the identifiers of the suggestions with the answers to be published.



12
13
14
15
16
# File 'app/commands/decidim/participatory_documents/admin/publish_answers.rb', line 12

def initialize(component, user, suggestion_ids)
  @component = component
  @user = user
  @suggestion_ids = suggestion_ids
end

Instance Method Details

#callObject

Executes the command. Broadcasts these events:

  • :ok when everything is valid.

  • :invalid if there are not proposals to publish.

Returns nothing.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/commands/decidim/participatory_documents/admin/publish_answers.rb', line 24

def call
  return broadcast(:invalid) unless suggestions.any?

  suggestions.each do |suggestion|
    suggestion.assign_attributes(answered_at: Time.current, answer_is_published: true)

    if suggestion.answer_is_published_changed?
      Decidim.traceability.perform_action!("publish_answer", suggestion, user) { suggestion.save! }
      notify_user(suggestion)
    end
  end

  broadcast(:ok)
end