Class: Decidim::EnhancedTextwork::Admin::UpdateParagraphCategory
- Inherits:
-
Rectify::Command
- Object
- Rectify::Command
- Decidim::EnhancedTextwork::Admin::UpdateParagraphCategory
- Defined in:
- app/commands/decidim/enhanced_textwork/admin/update_paragraph_category.rb
Overview
A command with all the business logic when an admin batch updates paragraphs category.
Instance Method Summary collapse
-
#call ⇒ Object
Executes the command.
-
#initialize(category_id, paragraph_ids) ⇒ UpdateParagraphCategory
constructor
Public: Initializes the command.
Constructor Details
#initialize(category_id, paragraph_ids) ⇒ UpdateParagraphCategory
Public: Initializes the command.
category_id - the category id to update paragraph_ids - the paragraphs ids to update.
12 13 14 15 16 |
# File 'app/commands/decidim/enhanced_textwork/admin/update_paragraph_category.rb', line 12 def initialize(category_id, paragraph_ids) @category = Decidim::Category.find_by id: category_id @paragraph_ids = paragraph_ids @response = { category_name: "", successful: [], errored: [] } end |
Instance Method Details
#call ⇒ Object
Executes the command. Broadcasts these events:
-
:update_paragraphs_category - when everything is ok, returns @response.
-
:invalid_category - if the category is blank.
-
:invalid_paragraph_ids - if the paragraph_ids is blank.
Returns @response hash:
-
:category_name - the translated_name of the category assigned
-
:successful - Array of names of the updated paragraphs
-
:errored - Array of names of the paragraphs 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 'app/commands/decidim/enhanced_textwork/admin/update_paragraph_category.rb', line 29 def call return broadcast(:invalid_category) if @category.blank? return broadcast(:invalid_paragraph_ids) if @paragraph_ids.blank? @response[:category_name] = @category.translated_name Paragraph.where(id: @paragraph_ids).find_each do |paragraph| if @category == paragraph.category @response[:errored] << paragraph.title else transaction do update_paragraph_category paragraph paragraph if paragraph..any? end @response[:successful] << paragraph.title end end broadcast(:update_paragraphs_category, @response) end |