Sha256: 45d8e9b5c47b52bef57f9fb6224087c85c4f5f4d1d25a2875ccef3c3ccb98f6e

Contents?: true

Size: 1.4 KB

Versions: 25

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true

module Decidim
  module Budgets
    module Admin
      class UpdateProjectCategory < Decidim::Command
        include TranslatableAttributes

        # Public: Initializes the command.
        #
        # category_id - the category id to update
        # project_ids - the project ids to update.
        def initialize(category_id, project_ids)
          @category = Decidim::Category.find_by id: category_id
          @project_ids = project_ids
          @response = { category_name: "", successful: [], errored: [] }
        end

        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

        private

        def update_project_category(project)
          project.update!(
            category: @category
          )
        end
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
decidim-budgets-0.29.1 app/commands/decidim/budgets/admin/update_project_category.rb
decidim-budgets-0.28.4 app/commands/decidim/budgets/admin/update_project_category.rb
decidim-budgets-0.27.9 app/commands/decidim/budgets/admin/update_project_category.rb
decidim-budgets-0.29.0 app/commands/decidim/budgets/admin/update_project_category.rb
decidim-budgets-0.28.3 app/commands/decidim/budgets/admin/update_project_category.rb
decidim-budgets-0.27.8 app/commands/decidim/budgets/admin/update_project_category.rb
decidim-budgets-0.29.0.rc4 app/commands/decidim/budgets/admin/update_project_category.rb
decidim-budgets-0.29.0.rc3 app/commands/decidim/budgets/admin/update_project_category.rb
decidim-budgets-0.29.0.rc2 app/commands/decidim/budgets/admin/update_project_category.rb
decidim-budgets-0.29.0.rc1 app/commands/decidim/budgets/admin/update_project_category.rb
decidim-budgets-0.28.2 app/commands/decidim/budgets/admin/update_project_category.rb
decidim-budgets-0.27.7 app/commands/decidim/budgets/admin/update_project_category.rb
decidim-budgets-0.28.1 app/commands/decidim/budgets/admin/update_project_category.rb
decidim-budgets-0.27.6 app/commands/decidim/budgets/admin/update_project_category.rb
decidim-budgets-0.28.0 app/commands/decidim/budgets/admin/update_project_category.rb
decidim-budgets-0.27.5 app/commands/decidim/budgets/admin/update_project_category.rb
decidim-budgets-0.28.0.rc5 app/commands/decidim/budgets/admin/update_project_category.rb
decidim-budgets-0.28.0.rc4 app/commands/decidim/budgets/admin/update_project_category.rb
decidim-budgets-0.27.4 app/commands/decidim/budgets/admin/update_project_category.rb
decidim-budgets-0.27.3 app/commands/decidim/budgets/admin/update_project_category.rb