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
# 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)
  @deserializer_class = deserializer_class
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

#decode(values) ⇒ Object

Expects an array of id values for a nested collection



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

def decode(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



54
55
56
# File 'lib/fun_with_json_api/attributes/relationship_collection.rb', line 54

def deserializer
  @deserializer ||= build_deserializer_from_options
end

#has_many?Boolean

rubocop:disable Style/PredicateName

Returns:

  • (Boolean)


43
44
45
# File 'lib/fun_with_json_api/attributes/relationship_collection.rb', line 43

def has_many?
  true
end

#param_valueObject

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



50
51
52
# File 'lib/fun_with_json_api/attributes/relationship_collection.rb', line 50

def param_value
  :"#{as.to_s.singularize}_ids"
end