Class: FunWithJsonApi::Attributes::RelationshipCollection

Inherits:
FunWithJsonApi::Attribute show all
Defined in:
lib/fun_with_json_api/attributes/relationship_collection.rb

Instance Attribute Summary collapse

Attributes inherited from FunWithJsonApi::Attribute

#as, #name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from FunWithJsonApi::Attribute

#sanitize_attribute_method

Constructor Details

#initialize(name, deserializer_class, options = {}) ⇒ RelationshipCollection

Returns a new instance of RelationshipCollection.



14
15
16
17
18
19
20
21
22
23
# File 'lib/fun_with_json_api/attributes/relationship_collection.rb', line 14

def initialize(name, deserializer_class, options = {})
  options = options.reverse_merge(
    attributes: [],
    relationships: []
  )
  super(name, options.reverse_merge(as: name.to_s.singularize.to_sym))
  @deserializer_class = deserializer_class

  check_as_attribute_is_singular!
end

Instance Attribute Details

#deserializer_classObject (readonly)

Returns the value of attribute deserializer_class.



10
11
12
# File 'lib/fun_with_json_api/attributes/relationship_collection.rb', line 10

def deserializer_class
  @deserializer_class
end

#optionsObject (readonly)

Returns the value of attribute options.



11
12
13
# File 'lib/fun_with_json_api/attributes/relationship_collection.rb', line 11

def options
  @options
end

Class Method Details

.create(name, deserializer_class_or_callable, options = {}) ⇒ Object



6
7
8
# File 'lib/fun_with_json_api/attributes/relationship_collection.rb', line 6

def self.create(name, deserializer_class_or_callable, options = {})
  new(name, deserializer_class_or_callable, options)
end

Instance Method Details

#call(values) ⇒ Object

Expects an array of id values for a nested collection



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/fun_with_json_api/attributes/relationship_collection.rb', line 26

def call(values)
  unless values.nil? || values.is_a?(Array)
    raise build_invalid_relationship_collection_error(values)
  end

  collection = deserializer.load_collection_from_id_values(values)

  # Ensure the collection size matches
  check_collection_matches_values!(collection, values)

  # Ensure the user is authorized to access the collection
  check_collection_is_authorized!(collection, values)

  # Call ActiceRecord#pluck if it is available
  convert_collection_to_ids(collection)
end

#deserializerObject



56
57
58
# File 'lib/fun_with_json_api/attributes/relationship_collection.rb', line 56

def deserializer
  @deserializer ||= build_deserializer_from_options
end

#has_many?Boolean

rubocop:disable Style/PredicateName

Returns:

  • (Boolean)


45
46
47
# File 'lib/fun_with_json_api/attributes/relationship_collection.rb', line 45

def has_many?
  true
end

#param_valueObject

User the singular of ‘as` that is how AMS converts the value



52
53
54
# File 'lib/fun_with_json_api/attributes/relationship_collection.rb', line 52

def param_value
  :"#{as}_ids"
end