Sha256: 5a15646c31f668e5f45107c81a174e6f5f613435262938938b943bda056267f4

Contents?: true

Size: 1.32 KB

Versions: 21

Compression:

Stored size: 1.32 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 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

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
decidim-budgets-0.25.2 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.25.1 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.25.0 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.25.0.rc4 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.25.0.rc3 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.25.0.rc2 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.25.0.rc1 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.24.3 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.23.6 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.24.2 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.23.5 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.24.1 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.24.0 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.24.0.rc2 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.23.4 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.24.0.rc1 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.23.3 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.23.2 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.23.1 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.23.1.rc1 app/commands/decidim/budgets/add_line_item.rb