Sha256: 7236b47904f118973e991712079873d4d44b0fdeb886112726185e212c0b05cc

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

module Decidim
  module Lausanne
    module Budgets
      # A command with all the business to add new line items to orders
      class AddLineItem < Decidim::Lausanne::Budgets::Command
        # Public: Initializes the command.
        #
        # order - The current order for the user or nil if it is not created yet.
        # project - The the project to include in the order
        # current_user - The current user logged in
        def initialize(current_order, project, current_user)
          @order = current_order
          @project = project
          @current_user = current_user
        end

        # Executes the command. Broadcasts these events:
        #
        # - :ok when everything is valid.
        # - :invalid if the there is an error.
        #
        # Returns nothing.
        def call
          transaction do
            return broadcast(:invalid) if voting_not_enabled? || order.checked_out?

            add_line_item
            broadcast(:ok, order)
          end
        end

        private

          attr_reader :current_user, :project

          def order
            @order ||= Order.create!(user: current_user, budget: project.budget)
          end

          def add_line_item
            order.with_lock do
              order.projects << project
            end
          end

          def voting_not_enabled?
            project.component.current_settings.votes != "enabled"
          end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
decidim-lausanne-budgets-0.1.0 app/commands/decidim/lausanne/budgets/add_line_item.rb