Sha256: 979e3bd48000ae46f3a73a53cd4bbfb044b65aeefce669f795f518753c43b350

Contents?: true

Size: 1.65 KB

Versions: 33

Compression:

Stored size: 1.65 KB

Contents

# frozen_string_literal: true

module Decidim
  module Budgets
    module Admin
      # This command is executed when the user creates a Project from the admin
      # panel.
      class CreateProject < Rectify::Command
        include ::Decidim::AttachmentMethods
        include ::Decidim::GalleryMethods

        def initialize(form)
          @form = form
        end

        # Creates 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
            create_project!
            link_proposals
            create_gallery if process_gallery?
          end

          broadcast(:ok)
        end

        private

        attr_reader :form, :project, :gallery

        def create_project!
          attributes = {
            budget: form.budget,
            scope: form.scope,
            category: form.category,
            title: form.title,
            description: form.description,
            budget_amount: form.budget_amount
          }

          @project = Decidim.traceability.create!(
            Project,
            form.current_user,
            attributes,
            visibility: "all"
          )
          @attached_to = @project
        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

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
decidim-budgets-0.26.10 app/commands/decidim/budgets/admin/create_project.rb
decidim-budgets-0.26.9 app/commands/decidim/budgets/admin/create_project.rb
decidim-budgets-0.26.8 app/commands/decidim/budgets/admin/create_project.rb
decidim-budgets-0.26.7 app/commands/decidim/budgets/admin/create_project.rb
decidim-budgets-0.26.5 app/commands/decidim/budgets/admin/create_project.rb
decidim-budgets-0.26.4 app/commands/decidim/budgets/admin/create_project.rb
decidim-budgets-0.26.3 app/commands/decidim/budgets/admin/create_project.rb
decidim-budgets-0.26.2 app/commands/decidim/budgets/admin/create_project.rb
decidim-budgets-0.26.1 app/commands/decidim/budgets/admin/create_project.rb
decidim-budgets-0.26.0 app/commands/decidim/budgets/admin/create_project.rb
decidim-budgets-0.26.0.rc2 app/commands/decidim/budgets/admin/create_project.rb
decidim-budgets-0.26.0.rc1 app/commands/decidim/budgets/admin/create_project.rb
decidim-budgets-0.25.2 app/commands/decidim/budgets/admin/create_project.rb
decidim-budgets-0.25.1 app/commands/decidim/budgets/admin/create_project.rb
decidim-budgets-0.25.0 app/commands/decidim/budgets/admin/create_project.rb
decidim-budgets-0.25.0.rc4 app/commands/decidim/budgets/admin/create_project.rb
decidim-budgets-0.25.0.rc3 app/commands/decidim/budgets/admin/create_project.rb
decidim-budgets-0.25.0.rc2 app/commands/decidim/budgets/admin/create_project.rb
decidim-budgets-0.25.0.rc1 app/commands/decidim/budgets/admin/create_project.rb
decidim-budgets-0.24.3 app/commands/decidim/budgets/admin/create_project.rb