Sha256: b2eba139ceb78b1ac7c1177693f95070263229351ec517961e2a0f8e9540bf33

Contents?: true

Size: 1.34 KB

Versions: 24

Compression:

Stored size: 1.34 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 votes_disabled? || order.checked_out?
          add_line_item
          broadcast(:ok, order)
        end
      end

      private

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

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

      def component
        @project.component
      end

      def votes_disabled?
        !component.current_settings.votes_enabled?
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
decidim-budgets-0.18.1 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.17.2 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.18.0 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.17.1 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.16.1 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.17.0 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.16.0 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.15.2 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.15.1 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.15.0 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.14.4 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.14.3 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.14.2 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.14.1 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.13.1 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.12.2 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.13.0 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.12.1 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.13.0.pre1 app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.12.0 app/commands/decidim/budgets/add_line_item.rb