Sha256: b241925701d94b346b55c141539621381d47ed370626de3dcda804de144a12a4

Contents?: true

Size: 1.34 KB

Versions: 8

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, feature: @project.feature)
      end

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

      def feature
        @project.feature
      end

      def votes_disabled?
        !feature.active_step_settings.votes_enabled?
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
decidim-budgets-0.1.0 app/commands/decidim/budgets/add_line_item.rb
decidim-0.1.0 decidim-budgets/app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.0.8.1 app/commands/decidim/budgets/add_line_item.rb
decidim-0.0.8.1 decidim-budgets/app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.0.7 app/commands/decidim/budgets/add_line_item.rb
decidim-0.0.7 decidim-budgets/app/commands/decidim/budgets/add_line_item.rb
decidim-budgets-0.0.6 app/commands/decidim/budgets/add_line_item.rb
decidim-0.0.6 decidim-budgets/app/commands/decidim/budgets/add_line_item.rb