OpenSourcePolitics/decidim-module-budgets_enhanced

View on GitHub
app/commands/decidim/budgets/admin/update_project.rb

Summary

Maintainability
A
0 mins
Test Coverage
# frozen_string_literal: true

module Decidim
  module Budgets
    module Admin
      # This command is executed when the user changes a Project from the admin
      # panel.
      class UpdateProject < Rectify::Command
        # Initializes an UpdateProject Command.
        #
        # form - The form from which to get the data.
        # project - The current instance of the project to be updated.
        def initialize(form, project)
          @form = form
          @project = project
        end

        # Updates the project if valid.
        #
        # Broadcasts :ok if successful, :invalid otherwise.
        def call
          return broadcast(:invalid) if form.invalid?

          transaction do
            update_project
            link_proposals
          end

          broadcast(:ok)
        end

        private

        attr_reader :project, :form

        def update_project
          Decidim.traceability.update!(
            project,
            form.current_user,
            scope: form.scope,
            category: form.category,
            title: form.title,
            description: form.description,
            budget: form.budget,
            address: form.address,
            latitude: form.latitude,
            longitude: form.longitude
          )
        end

        def proposals
          @proposals ||= project.sibling_scope(:proposals).where(id: form.proposal_ids)
        end

        def link_proposals
          project.link_resources(proposals, "included_proposals")
        end
      end
    end
  end
end