Module: Contentful::DatabaseImporter::ResourceFieldClassMethods

Defined in:
lib/contentful/database_importer/resource_field_class_methods.rb

Overview

Field related Class Methods for Resource

Instance Method Summary collapse

Instance Method Details

#fetch_many_relationship_options(field, options) ⇒ Object Also known as: fetch_one_relationship_options



37
38
39
40
# File 'lib/contentful/database_importer/resource_field_class_methods.rb', line 37

def fetch_many_relationship_options(field, options)
  field[:id_field] = options.fetch(:id_field)
  field[:key] = options.fetch(:key)
end

#fetch_relationship_options(field, options) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/contentful/database_importer/resource_field_class_methods.rb', line 51

def fetch_relationship_options(field, options)
  field[:relationship] = options.fetch(:relationship)

  send(
    "fetch_#{options.fetch(:relationship)}_relationship_options",
    field,
    options
  )
end

#fetch_through_relationship_options(field, options) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/contentful/database_importer/resource_field_class_methods.rb', line 43

def fetch_through_relationship_options(field, options)
  field[:through] = options.fetch(:through)
  field[:primary_id_field] = options.fetch(:primary_id_field)
  field[:foreign_id_field] = options.fetch(:foreign_id_field)
  field[:primary_key] = options.fetch(:primary_key)
  field[:foreign_key] = options.fetch(:foreign_key)
end

#field(database_name, options = {}) ⇒ Object



5
6
7
8
# File 'lib/contentful/database_importer/resource_field_class_methods.rb', line 5

def field(database_name, options = {})
  @fields ||= []
  @fields << prepare_field(database_name, options)
end

#fieldsObject



10
11
12
# File 'lib/contentful/database_importer/resource_field_class_methods.rb', line 10

def fields
  @fields || []
end

#prepare_field(database_name, options) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/contentful/database_importer/resource_field_class_methods.rb', line 29

def prepare_field(database_name, options)
  field = prepare_standard_field_options(database_name, options)
  field[:item_type] = options.fetch(:item_type) if field[:type] == :array
  fetch_relationship_options(field, options) if options.fetch(:relationship, false)

  field
end

#prepare_standard_field_options(database_name, options) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/contentful/database_importer/resource_field_class_methods.rb', line 18

def prepare_standard_field_options(database_name, options)
  {
    db_name: database_name,
    maps_to: options.fetch(:maps_to, database_name),
    name: options.fetch(:name, database_name),
    type: options.fetch(:type),
    pre_process: options.fetch(:pre_process, nil),
    exclude_from_output: options.fetch(:exclude_from_output, false)
  }
end

#relationship_fieldsObject



14
15
16
# File 'lib/contentful/database_importer/resource_field_class_methods.rb', line 14

def relationship_fields
  @fields.select { |f| resource?(f[:type]) }
end