Class: Decidim::TimeTracker::StartTimeEvent

Inherits:
Rectify::Command
  • Object
show all
Defined in:
app/commands/decidim/time_tracker/start_time_event.rb

Overview

A command with all the business logic when creating a time event

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(form) ⇒ StartTimeEvent

Public: Initializes the command.

form - A form object with the params.



10
11
12
# File 'app/commands/decidim/time_tracker/start_time_event.rb', line 10

def initialize(form)
  @form = form
end

Instance Attribute Details

#formObject (readonly)

Returns the value of attribute form.



38
39
40
# File 'app/commands/decidim/time_tracker/start_time_event.rb', line 38

def form
  @form
end

Instance Method Details

#callObject

Executes the command. Broadcasts these events:

  • :ok when everything is valid.

  • :invalid if the form wasn’t valid and we couldn’t proceed.

Returns nothing.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/commands/decidim/time_tracker/start_time_event.rb', line 20

def call
  return broadcast(:invalid, form.errors&.first&.second) if form.invalid?

  return broadcast(:already_active, @time_entry) if already_active?

  return broadcast(:invalid, form.errors&.first&.second) if activity_invalid?

  begin
    stop_previous_activity
    create_time_event!
    StopCounterJob.set(wait: form.activity.remaining_seconds_for_today.seconds).perform_later(@time_entry)
  rescue StandardError => e
    return broadcast(:invalid, e.message)
  end

  broadcast(:ok, @time_entry)
end