Class: FastJsonapi::Relationship

Inherits:
Object
  • Object
show all
Defined in:
lib/fast_jsonapi/relationship.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key:, name:, id_method_name:, record_type:, object_method_name:, object_block:, serializer:, relationship_type:, cached: false, polymorphic:, conditional_proc:, transform_method:, links:, lazy_load_data: false) ⇒ Relationship

Returns a new instance of Relationship.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fast_jsonapi/relationship.rb', line 5

def initialize(
  key:,
  name:,
  id_method_name:,
  record_type:,
  object_method_name:,
  object_block:,
  serializer:,
  relationship_type:,
  cached: false,
  polymorphic:,
  conditional_proc:,
  transform_method:,
  links:,
  lazy_load_data: false
)
  @key = key
  @name = name
  @id_method_name = id_method_name
  @record_type = record_type
  @object_method_name = object_method_name
  @object_block = object_block
  @serializer = serializer
  @relationship_type = relationship_type
  @cached = cached
  @polymorphic = polymorphic
  @conditional_proc = conditional_proc
  @transform_method = transform_method
  @links = links || {}
  @lazy_load_data = lazy_load_data
end

Instance Attribute Details

#cachedObject (readonly)

Returns the value of attribute cached.



3
4
5
# File 'lib/fast_jsonapi/relationship.rb', line 3

def cached
  @cached
end

#conditional_procObject (readonly)

Returns the value of attribute conditional_proc.



3
4
5
# File 'lib/fast_jsonapi/relationship.rb', line 3

def conditional_proc
  @conditional_proc
end

#id_method_nameObject (readonly)

Returns the value of attribute id_method_name.



3
4
5
# File 'lib/fast_jsonapi/relationship.rb', line 3

def id_method_name
  @id_method_name
end

#keyObject (readonly)

Returns the value of attribute key.



3
4
5
# File 'lib/fast_jsonapi/relationship.rb', line 3

def key
  @key
end

#lazy_load_dataObject (readonly)

Returns the value of attribute lazy_load_data.



3
4
5
# File 'lib/fast_jsonapi/relationship.rb', line 3

def lazy_load_data
  @lazy_load_data
end

Returns the value of attribute links.



3
4
5
# File 'lib/fast_jsonapi/relationship.rb', line 3

def links
  @links
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/fast_jsonapi/relationship.rb', line 3

def name
  @name
end

#object_blockObject (readonly)

Returns the value of attribute object_block.



3
4
5
# File 'lib/fast_jsonapi/relationship.rb', line 3

def object_block
  @object_block
end

#object_method_nameObject (readonly)

Returns the value of attribute object_method_name.



3
4
5
# File 'lib/fast_jsonapi/relationship.rb', line 3

def object_method_name
  @object_method_name
end

#polymorphicObject (readonly)

Returns the value of attribute polymorphic.



3
4
5
# File 'lib/fast_jsonapi/relationship.rb', line 3

def polymorphic
  @polymorphic
end

#record_typeObject (readonly)

Returns the value of attribute record_type.



3
4
5
# File 'lib/fast_jsonapi/relationship.rb', line 3

def record_type
  @record_type
end

#relationship_typeObject (readonly)

Returns the value of attribute relationship_type.



3
4
5
# File 'lib/fast_jsonapi/relationship.rb', line 3

def relationship_type
  @relationship_type
end

#serializerObject (readonly)

Returns the value of attribute serializer.



3
4
5
# File 'lib/fast_jsonapi/relationship.rb', line 3

def serializer
  @serializer
end

#transform_methodObject (readonly)

Returns the value of attribute transform_method.



3
4
5
# File 'lib/fast_jsonapi/relationship.rb', line 3

def transform_method
  @transform_method
end

Instance Method Details

#fetch_associated_object(record, params) ⇒ Object



49
50
51
52
# File 'lib/fast_jsonapi/relationship.rb', line 49

def fetch_associated_object(record, params)
  return object_block.call(record, params) unless object_block.nil?
  record.send(object_method_name)
end

#include_relationship?(record, serialization_params) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
58
59
60
# File 'lib/fast_jsonapi/relationship.rb', line 54

def include_relationship?(record, serialization_params)
  if conditional_proc.present?
    conditional_proc.call(record, serialization_params)
  else
    true
  end
end

#serialize(record, serialization_params, output_hash) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fast_jsonapi/relationship.rb', line 37

def serialize(record, serialization_params, output_hash)
  if include_relationship?(record, serialization_params)
    empty_case = relationship_type == :has_many ? [] : nil

    output_hash[key] = {}
    unless lazy_load_data
      output_hash[key][:data] = ids_hash_from_record_and_relationship(record, serialization_params) || empty_case
    end
    add_links_hash(record, serialization_params, output_hash) if links.present?
  end
end