Class: MOCO::NestedCollectionProxy

Inherits:
CollectionProxy show all
Defined in:
lib/moco/nested_collection_proxy.rb

Overview

Provides ActiveRecord-style query interface for nested MOCO entities For example, project.tasks is a nested collection of tasks under a project

Instance Attribute Summary collapse

Attributes inherited from CollectionProxy

#client, #entity_class_name, #filters, #limit_value

Instance Method Summary collapse

Methods inherited from CollectionProxy

#all, #assigned, #delete, #each, #find, #find_by, #first, #limit, #load_entity_class, #load_records, #update, #where

Constructor Details

#initialize(client, parent, path_or_entity_name, entity_class_name) ⇒ NestedCollectionProxy

Returns a new instance of NestedCollectionProxy.



9
10
11
12
# File 'lib/moco/nested_collection_proxy.rb', line 9

def initialize(client, parent, path_or_entity_name, entity_class_name)
  @parent = parent
  super(client, path_or_entity_name, entity_class_name)
end

Instance Attribute Details

#parentObject (readonly)

Returns the value of attribute parent.



7
8
9
# File 'lib/moco/nested_collection_proxy.rb', line 7

def parent
  @parent
end

#recordsObject (readonly)

Returns the value of attribute records.



7
8
9
# File 'lib/moco/nested_collection_proxy.rb', line 7

def records
  @records
end

Instance Method Details

#create(attributes) ⇒ Object

Create a new entity in this nested collection



24
25
26
27
28
29
# File 'lib/moco/nested_collection_proxy.rb', line 24

def create(attributes)
  klass = entity_class
  return nil unless klass && klass <= MOCO::BaseEntity

  klass.new(client, client.post(@base_path, attributes))
end

#destroy_allObject

Delete all entities in this nested collection



32
33
34
35
36
37
38
# File 'lib/moco/nested_collection_proxy.rb', line 32

def destroy_all
  client.delete("#{@base_path}/destroy_all")
  true
rescue StandardError => e
  warn "Warning: Failed to destroy all entities in #{@base_path}: #{e.message}"
  false
end

#determine_base_path(path_or_entity_name) ⇒ Object

Override determine_base_path to include the parent’s path For nested resources, we ignore any custom entity_path and just use simple pluralization



16
17
18
19
20
21
# File 'lib/moco/nested_collection_proxy.rb', line 16

def determine_base_path(path_or_entity_name)
  parent_type = ActiveSupport::Inflector.underscore(parent.class.name.split("::").last)
  # Use simple tableized name, not entity_path (which might include 'projects/' prefix)
  nested_path = ActiveSupport::Inflector.tableize(path_or_entity_name.to_s)
  "#{parent_type.pluralize}/#{parent.id}/#{nested_path}"
end