Sha256: 865d06865f8b241763a57959b32ddbc8848c8d6e251e1621cdfce17ddf0eb5cd

Contents?: true

Size: 2 KB

Versions: 12

Compression:

Stored size: 2 KB

Contents

# 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 < Decidim::Command
        include ::Decidim::AttachmentMethods
        include ::Decidim::GalleryMethods

        # 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
          @attached_to = project
        end

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

          if process_gallery?
            build_gallery
            return broadcast(:invalid) if gallery_invalid?
          end

          transaction do
            update_project
            link_proposals
            create_gallery if process_gallery?
            photo_cleanup!
          end

          broadcast(:ok)
        end

        private

        attr_reader :project, :form, :gallery

        def update_project
          Decidim.traceability.update!(
            project,
            form.current_user,
            scope: form.scope,
            category: form.category,
            title: form.title,
            description: form.description,
            budget_amount: form.budget_amount,
            selected_at: selected_at,
            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

        def selected_at
          return unless form.selected

          Time.current
        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.rb
decidim-budgets-0.27.8 app/commands/decidim/budgets/admin/update_project.rb
decidim-budgets-0.27.7 app/commands/decidim/budgets/admin/update_project.rb
decidim-budgets-0.27.6 app/commands/decidim/budgets/admin/update_project.rb
decidim-budgets-0.27.5 app/commands/decidim/budgets/admin/update_project.rb
decidim-budgets-0.27.4 app/commands/decidim/budgets/admin/update_project.rb
decidim-budgets-0.27.3 app/commands/decidim/budgets/admin/update_project.rb
decidim-budgets-0.27.2 app/commands/decidim/budgets/admin/update_project.rb
decidim-budgets-0.27.1 app/commands/decidim/budgets/admin/update_project.rb
decidim-budgets-0.27.0 app/commands/decidim/budgets/admin/update_project.rb
decidim-budgets-0.27.0.rc2 app/commands/decidim/budgets/admin/update_project.rb
decidim-budgets-0.27.0.rc1 app/commands/decidim/budgets/admin/update_project.rb