Sha256: 8291d268c9c7d492c48a9b742ebe745278b2e7d65bff100c9c09a07de09ecc76

Contents?: true

Size: 1.7 KB

Versions: 13

Compression:

Stored size: 1.7 KB

Contents

# frozen_string_literal: true

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

        # Public: Initializes the command.
        #
        # selection - Defines if projects are selected (for implementation)
        # project_ids - the project ids to update.
        def initialize(selection, project_ids)
          @selection = selection
          @project_ids = project_ids
          @response = { selection_name: "", successful: [], errored: [] }
        end

        def call
          return broadcast(:invalid_selection) if @selection.blank? || [true, false, "true", "false"].exclude?(@selection)
          return broadcast(:invalid_project_ids) if @project_ids.blank?

          @selection = ActiveModel::Type::Boolean.new.cast(@selection)

          update_projects_selection

          broadcast(:update_projects_selection, @response)
        end

        private

        attr_reader :selection, :project_ids

        def update_projects_selection
          ::Decidim::Budgets::Project.where(id: project_ids).find_each do |project|
            if (selection == false && !project.selected?) || (selection && project.selected?)
              @response[:errored] << translated_attribute(project.title)
            else
              transaction do
                update_project_selection project
              end
              @response[:successful] << translated_attribute(project.title)
            end
          end
        end

        def update_project_selection(project)
          selected_at = selection ? Time.current : nil
          project.update!(
            selected_at:
          )
        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_selection.rb
decidim-budgets-0.28.4 app/commands/decidim/budgets/admin/update_project_selection.rb
decidim-budgets-0.29.0 app/commands/decidim/budgets/admin/update_project_selection.rb
decidim-budgets-0.28.3 app/commands/decidim/budgets/admin/update_project_selection.rb
decidim-budgets-0.29.0.rc4 app/commands/decidim/budgets/admin/update_project_selection.rb
decidim-budgets-0.29.0.rc3 app/commands/decidim/budgets/admin/update_project_selection.rb
decidim-budgets-0.29.0.rc2 app/commands/decidim/budgets/admin/update_project_selection.rb
decidim-budgets-0.29.0.rc1 app/commands/decidim/budgets/admin/update_project_selection.rb
decidim-budgets-0.28.2 app/commands/decidim/budgets/admin/update_project_selection.rb
decidim-budgets-0.28.1 app/commands/decidim/budgets/admin/update_project_selection.rb
decidim-budgets-0.28.0 app/commands/decidim/budgets/admin/update_project_selection.rb
decidim-budgets-0.28.0.rc5 app/commands/decidim/budgets/admin/update_project_selection.rb
decidim-budgets-0.28.0.rc4 app/commands/decidim/budgets/admin/update_project_selection.rb