Class: FunWithJsonApi::CollectionManager

Inherits:
Object
  • Object
show all
Defined in:
lib/fun_with_json_api/collection_manager.rb

Overview

Abstract Handles updating a collection relationship. Override the ‘insert`, `remove` and `replace` methods, or provide a block that perfoms the desired action on the collection.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent_resource, deserializer_class, deserializer_options) ⇒ CollectionManager

Returns a new instance of CollectionManager.



11
12
13
14
# File 'lib/fun_with_json_api/collection_manager.rb', line 11

def initialize(parent_resource, deserializer_class, deserializer_options)
  @parent_resource = parent_resource
  @deserializer = deserializer_class.create(deserializer_options)
end

Instance Attribute Details

#deserializerObject (readonly)

Returns the value of attribute deserializer.



9
10
11
# File 'lib/fun_with_json_api/collection_manager.rb', line 9

def deserializer
  @deserializer
end

#parent_resourceObject (readonly)

Returns the value of attribute parent_resource.



8
9
10
# File 'lib/fun_with_json_api/collection_manager.rb', line 8

def parent_resource
  @parent_resource
end

Instance Method Details

#failure_message_for_resource(resource, failure_message_or_callable) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/fun_with_json_api/collection_manager.rb', line 68

def failure_message_for_resource(resource, failure_message_or_callable)
  resource_id = deserializer.format_resource_id(resource)
  failure_message = failure_message_or_callable
  if failure_message.respond_to?(:call)
    failure_message = failure_message.call(resource_id)
  end
  failure_message || default_invalid_resource_message(resource_id)
end

#insert_records(document, failure_message_or_callable = nil, &block) ⇒ Object

Inserts all records from a document into the parent resource Either provide a block method that adds an individual item or override this method

The block will be provided with a resource and must return true, or an exception with a payload will be raised after all items have been iterated through

You need to reverse all changes made in the event of an exception, wrapping an an ActiveRecord::Base.transaction block will usually work



30
31
32
33
34
35
36
37
38
39
# File 'lib/fun_with_json_api/collection_manager.rb', line 30

def insert_records(document, failure_message_or_callable = nil, &block)
  # Action is not supported unless overridden, or a block is defined
  unless block_given?
    raise build_relationship_not_supported_exception(
      "Override #{self.class.name}#insert_records or supply a block",
      insert_not_supported_message
    )
  end
  update_collection_items(load_collection(document), failure_message_or_callable, &block)
end

#load_collection(document) ⇒ Object

Loads a collection from the document Use this method when implementinog a CollectionManager



18
19
20
# File 'lib/fun_with_json_api/collection_manager.rb', line 18

def load_collection(document)
  FunWithJsonApi::FindCollectionFromDocument.find(document, deserializer)
end

#remove_records(document, failure_message_or_callable = nil, &block) ⇒ Object

Removes all records from a document from the parent resource Either provide a block method that removes an individual item or override this method

The block will be provided with a resource and must return true, or an exception with a payload will be raised after all items have been iterated through

You need to reverse all changes made in the event of an exception, wrapping an an ActiveRecord::Base.transaction block will usually work



49
50
51
52
53
54
55
56
57
58
# File 'lib/fun_with_json_api/collection_manager.rb', line 49

def remove_records(document, failure_message_or_callable = nil, &block)
  # Action is not supported unless overridden, or a block is defined
  unless block_given?
    raise build_relationship_not_supported_exception(
      "Override #{self.class.name}#remove_records or supply a block",
      remove_not_supported_message
    )
  end
  update_collection_items(load_collection(document), failure_message_or_callable, &block)
end

#replace_all_records(_document) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/fun_with_json_api/collection_manager.rb', line 60

def replace_all_records(_document)
  # Action is not supported unless overridden
  raise build_relationship_not_supported_exception(
    "Override #{self.class.name}#replace_all_records to implement replace all",
    replace_all_not_supported_message
  )
end