Module: Contentful::DatabaseImporter::ResourceRelationships

Included in:
Resource
Defined in:
lib/contentful/database_importer/resource_relationships.rb

Overview

Relationship methods for Resource

Instance Method Summary collapse

Instance Method Details

#fetch_many(relationship_field_definition) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/contentful/database_importer/resource_relationships.rb', line 17

def fetch_many(relationship_field_definition)
  table_name = relationship_field_definition[:type].table_name
  Contentful::DatabaseImporter.database[table_name].where(
    relationship_field_definition[:key] =>
      @raw[relationship_field_definition[:id_field]]
  ).map do |row|
    relationship_field_definition[:type].new(row).to_link
  end
end

#fetch_one(relationship_field_definition) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/contentful/database_importer/resource_relationships.rb', line 27

def fetch_one(relationship_field_definition)
  table_name = relationship_field_definition[:type].table_name
  row = Contentful::DatabaseImporter.database[table_name].where(
    relationship_field_definition[:id_field] =>
      @raw[relationship_field_definition[:key]]
  ).first

  return if row.nil?

  relationship_field_definition[:type].new(row).to_link
end

#fetch_relations(relationship_field_definition) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/contentful/database_importer/resource_relationships.rb', line 5

def fetch_relations(relationship_field_definition)
  relations = %i[many one through]
  if relations.include?(relationship_field_definition[:relationship])
    return send(
      "fetch_#{relationship_field_definition[:relationship]}".to_sym,
      relationship_field_definition
    )
  end

  raise 'Invalid Relationship type'
end

#fetch_through(relationship_field_definition) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/contentful/database_importer/resource_relationships.rb', line 59

def fetch_through(relationship_field_definition)
  through = fetch_through_table_rows(relationship_field_definition)

  through.map do |through_row|
    resolve_through_relationship(
      through_row,
      relationship_field_definition
    )
  end
end

#fetch_through_table_rows(relationship_field_definition) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/contentful/database_importer/resource_relationships.rb', line 39

def fetch_through_table_rows(relationship_field_definition)
  through_table_name = relationship_field_definition[:through]

  Contentful::DatabaseImporter.database[through_table_name].where(
    relationship_field_definition[:primary_key] =>
      @raw[relationship_field_definition[:primary_id_field]]
  ).to_a
end

#resolve_through_relationship(through_row, field_definition) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/contentful/database_importer/resource_relationships.rb', line 48

def resolve_through_relationship(through_row, field_definition)
  table_name = field_definition[:type].table_name

  related = Contentful::DatabaseImporter.database[table_name].where(
    field_definition[:foreign_id_field] =>
      through_row[field_definition[:foreign_key]]
  ).first

  field_definition[:type].new(related).to_link
end