Sha256: 87fb749decf2d73d680e01ea32841bdc4db4319c9ae034e51ef9674814cfee5e

Contents?: true

Size: 1.13 KB

Versions: 3

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true
module Decidim
  module Budgets
    # A command with all the business to add new line items to orders
    class AddLineItem < Rectify::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 order.checked_out?
          add_line_item
          broadcast(:ok, order)
        end
      end

      private

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

      def add_line_item
        order.projects << @project
        order.save!
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
decidim-0.0.4 decidim-budgets/app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.0.3 app/commands/decidim/budgets/add_line_item.rb
decidim-0.0.3 decidim-budgets/app/commands/decidim/budgets/add_line_item.rb