Class: MOCO::Activity

Inherits:
BaseEntity show all
Defined in:
lib/moco/entities.rb,
lib/moco/entities/activity.rb

Overview

Represents a MOCO activity (time entry) Provides methods for activity-specific operations and associations

Instance Attribute Summary collapse

Attributes inherited from BaseEntity

#attributes, #client

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseEntity

#==, #association, #destroy, #eql?, #has_many, #hash, #initialize, #inspect, #reload, #save, #to_h, #to_json, #update

Constructor Details

This class inherits a constructor from MOCO::BaseEntity

Instance Attribute Details

#activeObject

Returns the value of attribute active.



70
71
72
# File 'lib/moco/entities.rb', line 70

def active
  @active
end

#billableObject

Returns the value of attribute billable.



70
71
72
# File 'lib/moco/entities.rb', line 70

def billable
  @billable
end

#billedObject

Returns the value of attribute billed.



70
71
72
# File 'lib/moco/entities.rb', line 70

def billed
  @billed
end

#customerObject

Fetches the associated Customer (Company) object.



80
81
82
# File 'lib/moco/entities/activity.rb', line 80

def customer
  @customer
end

#dateObject

Returns the value of attribute date.



70
71
72
# File 'lib/moco/entities.rb', line 70

def date
  @date
end

#descriptionObject

Returns the value of attribute description.



70
71
72
# File 'lib/moco/entities.rb', line 70

def description
  @description
end

#hoursObject

Returns the value of attribute hours.



70
71
72
# File 'lib/moco/entities.rb', line 70

def hours
  @hours
end

#idObject

Returns the value of attribute id.



70
71
72
# File 'lib/moco/entities.rb', line 70

def id
  @id
end

#projectObject

Associations Fetches the associated Project object.



41
42
43
# File 'lib/moco/entities/activity.rb', line 41

def project
  @project
end

#secondsObject

Returns the value of attribute seconds.



70
71
72
# File 'lib/moco/entities.rb', line 70

def seconds
  @seconds
end

#tagObject

Returns the value of attribute tag.



70
71
72
# File 'lib/moco/entities.rb', line 70

def tag
  @tag
end

#taskObject

Fetches the associated Task object.



56
57
58
# File 'lib/moco/entities/activity.rb', line 56

def task
  @task
end

#userObject

Fetches the associated User object.



68
69
70
# File 'lib/moco/entities/activity.rb', line 68

def user
  @user
end

Class Method Details

.bulk_create(client, activities) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/moco/entities/activity.rb', line 29

def self.bulk_create(client, activities)
  api_entities = activities.map do |activity|
    activity.to_h.except(:id, :project, :user, :customer).tap do |h|
      h[:project_id] = activity.project.id if activity.project
      h[:task_id] = activity.task.id if activity.task
    end
  end
  client.post("activities/bulk", { activities: api_entities })
end

.disregard(client, reason:, activity_ids:, company_id:, project_id: nil) ⇒ Object

Class methods for bulk operations



19
20
21
22
23
24
25
26
27
# File 'lib/moco/entities/activity.rb', line 19

def self.disregard(client, reason:, activity_ids:, company_id:, project_id: nil)
  payload = {
    reason:,
    activity_ids:,
    company_id:
  }
  payload[:project_id] = project_id if project_id
  client.post("activities/disregard", payload)
end

Instance Method Details

#remote_idObject

Access the remote_id attribute



93
94
95
# File 'lib/moco/entities/activity.rb', line 93

def remote_id
  attributes[:remote_id]
end

#start_timerObject

Instance methods for activity-specific operations



8
9
10
11
# File 'lib/moco/entities/activity.rb', line 8

def start_timer
  client.patch("activities/#{id}/start_timer")
  self
end

#stop_timerObject



13
14
15
16
# File 'lib/moco/entities/activity.rb', line 13

def stop_timer
  client.patch("activities/#{id}/stop_timer")
  self
end

#to_sObject



73
74
75
76
77
78
79
# File 'lib/moco/entities.rb', line 73

def to_s
  description_part = description.empty? ? "" : " (#{description})"
  status_part = "(#{%i[billable billed].map { |x| (send(x) ? "" : "not ") + x.to_s }.join(", ")})"

  "#{date} - #{Helpers.decimal_hours_to_civil(hours)}h (#{seconds}s) - " \
    "#{project&.name} - #{task&.name}#{description_part} #{status_part}"
end