Class: Decidim::Budgets::AddLineItem

Inherits:
Command
  • Object
show all
Defined in:
app/commands/decidim/budgets/add_line_item.rb

Overview

A command with all the business to add new line items to orders

Instance Method Summary collapse

Constructor Details

#initialize(current_order, project, current_user) ⇒ AddLineItem

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



12
13
14
15
16
# File 'app/commands/decidim/budgets/add_line_item.rb', line 12

def initialize(current_order, project, current_user)
  @order = current_order
  @project = project
  @current_user = current_user
end

Instance Method Details

#callObject

Executes the command. Broadcasts these events:

  • :ok when everything is valid.

  • :invalid if the there is an error.

Returns nothing.



24
25
26
27
28
29
30
31
32
33
# File 'app/commands/decidim/budgets/add_line_item.rb', line 24

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