Module: Arkenstone::Associations::InstanceMethods

Defined in:
lib/arkenstone/associations.rb

Instance Method Summary collapse

Instance Method Details

#add_child(child_model_name, child_id) ⇒ Object

Calls the POST url for creating a nested_resource



340
341
342
343
344
# File 'lib/arkenstone/associations.rb', line 340

def add_child(child_model_name, child_id)
  url = build_nested_url child_model_name
  body = { id: child_id }.to_json
  self.class.send_request url, :post, body
end

#fetch_child(child_model_name, child_url_fragment) ⇒ Object

Fetches a single ‘has_one` based resource



322
323
324
325
326
327
328
# File 'lib/arkenstone/associations.rb', line 322

def fetch_child(child_model_name, child_url_fragment)
  fetch_nested_resource child_model_name, child_url_fragment do |klass, response_body|
    return nil if response_body.nil? || response_body.empty?

    klass.build JSON.parse(response_body)
  end
end

#fetch_children(child_model_name, child_url_fragment) ⇒ Object

Fetches a ‘has_many` based resource



315
316
317
318
319
# File 'lib/arkenstone/associations.rb', line 315

def fetch_children(child_model_name, child_url_fragment)
  fetch_nested_resource child_model_name, child_url_fragment do |klass, response_body|
    klass.parse_all response_body
  end
end

#fetch_parent(parent_model_name) ⇒ Object

Fetches a single ‘belongs_to` parent resource.



331
332
333
334
335
336
337
# File 'lib/arkenstone/associations.rb', line 331

def fetch_parent(parent_model_name)
  klass_name = parent_model_name.to_s.classify
  klass_name = prefix_with_class_module klass_name
  klass      = Kernel.const_get klass_name
  parent_model_field = "#{parent_model_name}_id"
  klass.send(:find, send(parent_model_field))
end

#remove_child(child_model_name, child_id) ⇒ Object

Calls the DELETE route for a nested resource



347
348
349
350
# File 'lib/arkenstone/associations.rb', line 347

def remove_child(child_model_name, child_id)
  url = build_nested_url child_model_name, child_id
  self.class.send_request url, :delete
end