Class: FunWithJsonApi::Attributes::RelationshipCollection
- Inherits:
-
FunWithJsonApi::Attribute
- Object
- FunWithJsonApi::Attribute
- FunWithJsonApi::Attributes::RelationshipCollection
- Defined in:
- lib/fun_with_json_api/attributes/relationship_collection.rb
Instance Attribute Summary collapse
-
#deserializer_class ⇒ Object
readonly
Returns the value of attribute deserializer_class.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Attributes inherited from FunWithJsonApi::Attribute
Class Method Summary collapse
Instance Method Summary collapse
-
#decode(values) ⇒ Object
Expects an array of id values for a nested collection.
- #deserializer ⇒ Object
-
#has_many? ⇒ Boolean
rubocop:disable Style/PredicateName.
-
#initialize(name, deserializer_class, options = {}) ⇒ RelationshipCollection
constructor
A new instance of RelationshipCollection.
-
#param_value ⇒ Object
User the singular of ‘as` that is how AMS converts the value.
Methods inherited from FunWithJsonApi::Attribute
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, = {}) = .reverse_merge( attributes: [], relationships: [] ) super(name, ) @deserializer_class = deserializer_class end |
Instance Attribute Details
#deserializer_class ⇒ Object (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 |
#options ⇒ Object (readonly)
Returns the value of attribute options.
11 12 13 |
# File 'lib/fun_with_json_api/attributes/relationship_collection.rb', line 11 def @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, = {}) new(name, deserializer_class_or_callable, ) 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 (collection, values) # Call ActiceRecord#pluck if it is available convert_collection_to_ids(collection) end |
#deserializer ⇒ Object
54 55 56 |
# File 'lib/fun_with_json_api/attributes/relationship_collection.rb', line 54 def deserializer @deserializer ||= end |
#has_many? ⇒ Boolean
rubocop:disable Style/PredicateName
43 44 45 |
# File 'lib/fun_with_json_api/attributes/relationship_collection.rb', line 43 def has_many? true end |
#param_value ⇒ Object
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 |