Sha256: 20f07fa849dbe87a9950e97feeaf6909a28f9db39a82045c3d774ea2d955c6be

Contents?: true

Size: 1.53 KB

Versions: 25

Compression:

Stored size: 1.53 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 < Decidim::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
          raise ActiveRecord::RecordInvalid if voting_not_enabled? || order.checked_out? || exceeds_budget?

          add_line_item
          broadcast(:ok, order)
        end
      rescue ActiveRecord::RecordInvalid
        broadcast(:invalid)
      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 exceeds_budget?
        order.allocation_for(project) + order.total > order.available_allocation
      end

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

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
decidim-budgets-0.29.1 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.28.4 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.27.9 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.29.0 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.28.3 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.27.8 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.29.0.rc4 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.29.0.rc3 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.29.0.rc2 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.29.0.rc1 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.28.2 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.27.7 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.28.1 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.27.6 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.28.0 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.27.5 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.28.0.rc5 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.28.0.rc4 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.27.4 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.27.3 app/commands/decidim/budgets/add_line_item.rb