moco-ruby 1.0.0.alpha → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,7 +5,7 @@ module MOCO
5
5
  # Provides methods for holiday-specific associations
6
6
  class Holiday < BaseEntity
7
7
  # Override entity_path to match API path
8
- def entity_path
8
+ def self.entity_path
9
9
  "users/holidays"
10
10
  end
11
11
 
@@ -7,29 +7,46 @@ module MOCO
7
7
  association(:customer, "Company")
8
8
  end
9
9
 
10
+ def leader
11
+ # Use the association method to fetch the leader
12
+ association(:leader, "User")
13
+ end
14
+
15
+ def co_leader
16
+ # Use the association method to fetch the co_leader
17
+ association(:co_leader, "User")
18
+ end
19
+
10
20
  # Fetches activities associated with this project.
11
21
  def activities
12
22
  # Use the has_many method to fetch activities
13
23
  has_many(:activities)
14
24
  end
15
25
 
26
+ # Fetches expenses associated with this project.
27
+ def expenses
28
+ # Don't cache the proxy - create a fresh one each time
29
+ # This ensures we get fresh data when expenses are created/updated/deleted
30
+ MOCO::NestedCollectionProxy.new(client, self, :expenses, "Expense")
31
+ end
32
+
16
33
  # Fetches tasks associated with this project.
17
34
  def tasks
18
- # Check if tasks are already loaded in attributes
19
- if attributes[:tasks].is_a?(Array) && attributes[:tasks].all? { |t| t.is_a?(MOCO::Task) }
20
- # If tasks are already loaded, create a NestedCollectionProxy with the loaded tasks
21
- @_tasks_proxy ||= begin
22
- require_relative "../nested_collection_proxy"
23
- proxy = MOCO::NestedCollectionProxy.new(client, self, :tasks, "Task")
24
- # We need to manually set the loaded records since we already have them
25
- proxy.instance_variable_set(:@records, attributes[:tasks])
26
- proxy.instance_variable_set(:@loaded, true)
27
- proxy
28
- end
29
- else
30
- # Otherwise, use has_many with nested=true
31
- has_many(:tasks, nil, nil, true)
35
+ # If tasks are already embedded in the attributes (e.g., from projects.assigned),
36
+ # return them directly instead of making a new API call
37
+ embedded_tasks = attributes[:tasks]
38
+ if embedded_tasks.is_a?(Array) && embedded_tasks.all? { |t| t.is_a?(MOCO::Task) }
39
+ return embedded_tasks
32
40
  end
41
+
42
+ # Otherwise, create a proxy for fetching tasks via API
43
+ # Don't cache the proxy - create a fresh one each time
44
+ # This ensures we get fresh data when tasks are created/updated/deleted
45
+ MOCO::NestedCollectionProxy.new(client, self, :tasks, "Task")
46
+ end
47
+
48
+ def to_s
49
+ "Project #{identifier} \"#{name}\" (#{id})"
33
50
  end
34
51
 
35
52
  def active?
@@ -5,7 +5,7 @@ module MOCO
5
5
  # Provides methods for webhook-specific operations
6
6
  class WebHook < BaseEntity
7
7
  # Override entity_path to match API path
8
- def entity_path
8
+ def self.entity_path
9
9
  "account/web_hooks"
10
10
  end
11
11
 
@@ -14,8 +14,8 @@ module MOCO
14
14
  @entity_class_name = entity_class_name
15
15
  end
16
16
 
17
- def all(params = {})
18
- collection.all(params)
17
+ def all
18
+ collection.all
19
19
  end
20
20
 
21
21
  def find(id)
@@ -31,7 +31,7 @@ module MOCO
31
31
  end
32
32
 
33
33
  def each(&)
34
- all.each(&)
34
+ collection.each(&)
35
35
  end
36
36
 
37
37
  def first
@@ -12,9 +12,12 @@ module MOCO
12
12
  end
13
13
 
14
14
  # Override determine_base_path to include the parent's path
15
+ # For nested resources, we ignore any custom entity_path and just use simple pluralization
15
16
  def determine_base_path(path_or_entity_name)
16
17
  parent_type = ActiveSupport::Inflector.underscore(parent.class.name.split("::").last)
17
- "#{parent_type.pluralize}/#{parent.id}/#{super}"
18
+ # Use simple tableized name, not entity_path (which might include 'projects/' prefix)
19
+ nested_path = ActiveSupport::Inflector.tableize(path_or_entity_name.to_s)
20
+ "#{parent_type.pluralize}/#{parent.id}/#{nested_path}"
18
21
  end
19
22
 
20
23
  # Create a new entity in this nested collection