Class: Decidim::Proposals::Admin::UpdateProposalCategory

Inherits:
Rectify::Command
  • Object
show all
Defined in:
decidim-proposals/app/commands/decidim/proposals/admin/update_proposal_category.rb

Overview

A command with all the business logic when an admin batch updates proposals category.

Instance Method Summary collapse

Constructor Details

#initialize(category_id, proposal_ids) ⇒ UpdateProposalCategory

Public: Initializes the command.

category_id - the category id to update proposal_ids - the proposals ids to update.


12
13
14
15
16
# File 'decidim-proposals/app/commands/decidim/proposals/admin/update_proposal_category.rb', line 12

def initialize(category_id, proposal_ids)
  @category = Decidim::Category.find_by id: category_id
  @proposal_ids = proposal_ids
  @response = { category_name: "", successful: [], errored: [] }
end

Instance Method Details

#callObject

Executes the command. Broadcasts these events:

  • :update_proposals_category - when everything is ok, returns @response.

  • :invalid_category - if the category is blank.

  • :invalid_proposal_ids - if the proposal_ids is blank.

Returns @response hash:

  • :category_name - the translated_name of the category assigned

  • :successful - Array of names of the updated proposals

  • :errored - Array of names of the proposals not updated because they already had the category assigned


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'decidim-proposals/app/commands/decidim/proposals/admin/update_proposal_category.rb', line 29

def call
  return broadcast(:invalid_category) if @category.blank?
  return broadcast(:invalid_proposal_ids) if @proposal_ids.blank?

  @response[:category_name] = @category.translated_name
  Proposal.where(id: @proposal_ids).find_each do |proposal|
    if @category == proposal.category
      @response[:errored] << proposal.title
    else
      transaction do
        update_proposal_category proposal
        notify_author proposal if proposal.coauthorships.any?
      end
      @response[:successful] << proposal.title
    end
  end

  broadcast(:update_proposals_category, @response)
end