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



75
76
77
78
79
80
81
82
# File 'lib/fun_with_json_api/collection_manager.rb', line 75

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_record(_record) ⇒ Object

Inserts a single record into a collection Must return true for a successful update, or return false for any failures



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

def insert_record(_record)
  raise build_relationship_not_supported_exception(
    "Override #{self.class.name}#insert_record",
    insert_not_supported_message
  )
end

#insert_records(collection, failure_message_or_callable = nil) ⇒ Object

Inserts all records from a collection into the parent resource

Will attempt to call ‘insert_record` for each item in the `collection`. If false is received for any `insert_record` call, 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

Action is not supported unless ‘insert_records` or the `insert_record` method is overridden



44
45
46
47
48
# File 'lib/fun_with_json_api/collection_manager.rb', line 44

def insert_records(collection, failure_message_or_callable = nil)
  update_collection_items(collection, failure_message_or_callable) do |record|
    insert_record(record)
  end
end

#remove_record(_record) ⇒ Object

Removes a single record into a collection Must return true for a successful update, or return false for any failures



27
28
29
30
31
32
# File 'lib/fun_with_json_api/collection_manager.rb', line 27

def remove_record(_record)
  raise build_relationship_not_supported_exception(
    "Override #{self.class.name}#remove_record",
    remove_not_supported_message
  )
end

#remove_records(collection, failure_message_or_callable = nil) ⇒ Object

Removes all records from a collection into the parent resource

Will attempt to call ‘remove_record` for each item in the `collection`. If false is received for any `remove_record` call, 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

Action is not supported unless ‘remove_records` or the `remove_record` method is overridden



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

def remove_records(collection, failure_message_or_callable = nil)
  update_collection_items(collection, failure_message_or_callable) do |record|
    remove_record(record)
  end
end

#replace_all_records(_collection) ⇒ Object

Replaces all records



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

def replace_all_records(_collection)
  # 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