Sha256: a224c4956ffe4e3cbc8505c45585313579d0023c476d34d4053587f41243b1b2

Contents?: true

Size: 1.5 KB

Versions: 13

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

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

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

        def call
          return broadcast(:invalid_scope) if @scope.blank?
          return broadcast(:invalid_project_ids) if @project_ids.blank?

          update_projects_scope

          broadcast(:update_projects_scope, @response)
        end

        private

        attr_reader :scope, :project_ids

        def update_projects_scope
          @response[:scope_name] = translated_attribute(scope.name, scope.organization)
          ::Decidim::Budgets::Project.where(id: project_ids).find_each do |project|
            if scope == project.scope
              @response[:errored] << translated_attribute(project.title)
            else
              transaction do
                update_project_scope project
              end
              @response[:successful] << translated_attribute(project.title)
            end
          end
        end

        def update_project_scope(project)
          project.update!(
            scope:
          )
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
decidim-budgets-0.29.1 app/commands/decidim/budgets/admin/update_project_scope.rb
decidim-budgets-0.28.4 app/commands/decidim/budgets/admin/update_project_scope.rb
decidim-budgets-0.29.0 app/commands/decidim/budgets/admin/update_project_scope.rb
decidim-budgets-0.28.3 app/commands/decidim/budgets/admin/update_project_scope.rb
decidim-budgets-0.29.0.rc4 app/commands/decidim/budgets/admin/update_project_scope.rb
decidim-budgets-0.29.0.rc3 app/commands/decidim/budgets/admin/update_project_scope.rb
decidim-budgets-0.29.0.rc2 app/commands/decidim/budgets/admin/update_project_scope.rb
decidim-budgets-0.29.0.rc1 app/commands/decidim/budgets/admin/update_project_scope.rb
decidim-budgets-0.28.2 app/commands/decidim/budgets/admin/update_project_scope.rb
decidim-budgets-0.28.1 app/commands/decidim/budgets/admin/update_project_scope.rb
decidim-budgets-0.28.0 app/commands/decidim/budgets/admin/update_project_scope.rb
decidim-budgets-0.28.0.rc5 app/commands/decidim/budgets/admin/update_project_scope.rb
decidim-budgets-0.28.0.rc4 app/commands/decidim/budgets/admin/update_project_scope.rb