Sha256: 24bc4415f4d5174926315bc3f678b40a9b95e54de012179b844bb44305df9ef8

Contents?: true

Size: 1.71 KB

Versions: 12

Compression:

Stored size: 1.71 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: selected_at
          )
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
decidim-budgets-0.27.9 app/commands/decidim/budgets/admin/update_project_selection.rb
decidim-budgets-0.27.8 app/commands/decidim/budgets/admin/update_project_selection.rb
decidim-budgets-0.27.7 app/commands/decidim/budgets/admin/update_project_selection.rb
decidim-budgets-0.27.6 app/commands/decidim/budgets/admin/update_project_selection.rb
decidim-budgets-0.27.5 app/commands/decidim/budgets/admin/update_project_selection.rb
decidim-budgets-0.27.4 app/commands/decidim/budgets/admin/update_project_selection.rb
decidim-budgets-0.27.3 app/commands/decidim/budgets/admin/update_project_selection.rb
decidim-budgets-0.27.2 app/commands/decidim/budgets/admin/update_project_selection.rb
decidim-budgets-0.27.1 app/commands/decidim/budgets/admin/update_project_selection.rb
decidim-budgets-0.27.0 app/commands/decidim/budgets/admin/update_project_selection.rb
decidim-budgets-0.27.0.rc2 app/commands/decidim/budgets/admin/update_project_selection.rb
decidim-budgets-0.27.0.rc1 app/commands/decidim/budgets/admin/update_project_selection.rb