Class: Decidim::UpdateResourcesTaxonomies

Inherits:
Command
  • Object
show all
Defined in:
decidim-core/app/commands/decidim/update_resources_taxonomies.rb

Overview

A command with all the business logic when an admin batch updates taxonomies on several resources.

Instance Method Summary collapse

Methods inherited from Command

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

Constructor Details

#initialize(taxonomy_ids, resources, organization) ⇒ UpdateResourcesTaxonomies

Public: Initializes the command.

taxonomy_ids - the taxonomy ids to update resources - an ApplicationRecord collection of resources to update.



10
11
12
13
14
15
# File 'decidim-core/app/commands/decidim/update_resources_taxonomies.rb', line 10

def initialize(taxonomy_ids, resources, organization)
  @organization = organization
  @taxonomies = Decidim::Taxonomy.non_roots.where(organization:, id: taxonomy_ids)
  @resources = resources
  @response = { taxonomies: [], 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_resources_taxonomies - when everything is ok, returns @response.

  • :invalid_taxonomies - if the taxonomy is blank.

  • :invalid_resources - if the resource_ids is blank.

Returns @response hash:

  • :taxonomies - Array of the updated taxonomies

  • :successful - Array of the updated resources

  • :errored - Array of the resources not updated because they already had the taxonomies assigned



28
29
30
31
32
33
34
35
# File 'decidim-core/app/commands/decidim/update_resources_taxonomies.rb', line 28

def call
  return broadcast(:invalid_taxonomies) if @taxonomies.blank?
  return broadcast(:invalid_resources) if @resources.blank? || !@resources.respond_to?(:find_each)

  update_resources_taxonomies

  broadcast(:update_resources_taxonomies, @response)
end

#run_after_hooks(resource) ⇒ Object

Useful for running any code that you may want to execute after updating taxonomies on each resource.



41
# File 'decidim-core/app/commands/decidim/update_resources_taxonomies.rb', line 41

def run_after_hooks(resource); end

#run_before_hooks(resource) ⇒ Object

Useful for running any code that you may want to execute before updating taxonomies on each resource.



38
# File 'decidim-core/app/commands/decidim/update_resources_taxonomies.rb', line 38

def run_before_hooks(resource); end