Class: Decidim::Budgets::Admin::UpdateProjectCategory

Inherits:
Command
  • Object
show all
Includes:
TranslatableAttributes
Defined in:
app/commands/decidim/budgets/admin/update_project_category.rb

Instance Method Summary collapse

Constructor Details

#initialize(category_id, project_ids) ⇒ UpdateProjectCategory

Public: Initializes the command.

category_id - the category id to update project_ids - the project ids to update.



13
14
15
16
17
# File 'app/commands/decidim/budgets/admin/update_project_category.rb', line 13

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

Instance Method Details

#callObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/commands/decidim/budgets/admin/update_project_category.rb', line 19

def call
  return broadcast(:invalid_category) if @category.blank?
  return broadcast(:invalid_project_ids) if @project_ids.blank?

  @response[:category_name] = @category.translated_name
  Project.where(id: @project_ids).find_each do |project|
    if @category == project.category
      @response[:errored] << translated_attribute(project.title)
    else
      transaction do
        update_project_category project
      end
      @response[:successful] << translated_attribute(project.title)
    end
  end

  broadcast(:update_projects_category, @response)
end