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)

Required attributes for create:

date - String, "YYYY-MM-DD" format (e.g., "2024-01-15") project_id - Integer, ID of the project task_id - Integer, ID of the task within the project

Optional attributes:

seconds - Integer, duration in seconds (3600 = 1 hour) hours - Float, duration in hours (alternative to seconds) description - String, description of the work done billable - Boolean, whether the activity is billable (default: true or project setting) tag - String, any tag (e.g., "RMT-123") remote_service - String, external service name. Allowed: "trello", "jira", "asana", "basecamp", "wunderlist", "basecamp2", "basecamp3", "toggl", "mite", "github", "youtrack" remote_id - String, ID in the external service (e.g., "PRJ-2342") remote_url - String, URL to the external ticket/issue

Read-only attributes (returned by API):

id, billed, invoice_id, project (Hash), task (Hash), customer (Hash), user (Hash), hourly_rate, timer_started_at, created_at, updated_at

Example:

moco.activities.create( date: "2024-01-15", project_id: 123456, task_id: 234567, seconds: 3600, description: "Implemented feature X" )

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.



110
111
112
# File 'lib/moco/entities/activity.rb', line 110

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.



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

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.



86
87
88
# File 'lib/moco/entities/activity.rb', line 86

def task
  @task
end

#userObject

Fetches the associated User object.



98
99
100
# File 'lib/moco/entities/activity.rb', line 98

def user
  @user
end

Class Method Details

.bulk_create(client, activities) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/moco/entities/activity.rb', line 59

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



49
50
51
52
53
54
55
56
57
# File 'lib/moco/entities/activity.rb', line 49

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



123
124
125
# File 'lib/moco/entities/activity.rb', line 123

def remote_id
  attributes[:remote_id]
end

#start_timerObject

Instance methods for activity-specific operations



38
39
40
41
# File 'lib/moco/entities/activity.rb', line 38

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

#stop_timerObject



43
44
45
46
# File 'lib/moco/entities/activity.rb', line 43

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