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

Inherits:
Command
  • Object
show all
Includes:
TranslatableAttributes
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

Methods included from TranslatableAttributes

#default_locale?

Methods inherited from Command

call, #evaluate, #method_missing, #respond_to_missing?, #transaction, #with_events

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.



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

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Decidim::Command

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



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

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] << translated_attribute(proposal.title)
    else
      transaction do
        update_proposal_category proposal
        notify_author proposal if proposal.coauthorships.any?
      end
      @response[:successful] << translated_attribute(proposal.title)
    end
  end

  broadcast(:update_proposals_category, @response)
end