class Contentful::Exporter::Drupal::ContentType

Attributes

config[R]
exporter[R]
schema[R]
type[R]

Public Class Methods

new(exporter, config, type, schema) click to toggle source
# File lib/drupal/content_type.rb, line 8
def initialize(exporter, config, type, schema)
  @exporter = exporter
  @config = config
  @type = type
  @schema = schema
end

Public Instance Methods

save_content_types_as_json() click to toggle source
# File lib/drupal/content_type.rb, line 15
def save_content_types_as_json
  exporter.create_directory("#{config.entries_dir}/#{type}")
  config.db[:node].where(type: type).each do |content_row|
    extract_data(content_row)
  end
end

Private Instance Methods

author(user_id) click to toggle source
# File lib/drupal/content_type.rb, line 40
def author(user_id)
  {type: 'Author', id: "user_#{user_id}"}
end
boolean_column?(column_name) click to toggle source
# File lib/drupal/content_type.rb, line 140
def boolean_column?(column_name)
  exporter.boolean_columns && exporter.boolean_columns.flatten.include?(column_name) ? true : false
end
convert_boolean_value(value) click to toggle source
# File lib/drupal/content_type.rb, line 144
def convert_boolean_value(value)
  value.nil? ? nil : (value == 1 ? true : false)
end
convert_type_value(value, column_name) click to toggle source
# File lib/drupal/content_type.rb, line 130
def convert_type_value(value, column_name)
  if value.is_a?(BigDecimal)
    value.to_f
  elsif boolean_column?(column_name)
    convert_boolean_value(value)
  else
    value
  end
end
created_at(timestamp) click to toggle source
# File lib/drupal/content_type.rb, line 126
def created_at(timestamp)
  Time.at(timestamp).to_datetime
end
entity_tags(entity_id) click to toggle source
# File lib/drupal/content_type.rb, line 51
def entity_tags(entity_id)
  config.db[:field_data_field_tags].where(entity_id: entity_id)
end
extract_data(content_row) click to toggle source
# File lib/drupal/content_type.rb, line 24
def extract_data(content_row)
  puts "Saving #{type} - id: #{content_row[:nid]}"
  db_object = map_fields(content_row)
  exporter.write_json_to_file("#{config.entries_dir}/#{type}/#{db_object[:id]}.json", db_object)
end
fetch_custom_tags(entity_id, table_name) click to toggle source
# File lib/drupal/content_type.rb, line 109
def fetch_custom_tags(entity_id, table_name)
  custom_tag_table = "field_data_#{table_name['table']}".to_sym
  tag_id = "#{table_name['table']}_tid".to_sym
  config.db[custom_tag_table].where(entity_id: entity_id).each_with_object([]) do |content_tag, tags|
    lined_tags = {type: 'EntryTag', id: "tag_#{content_tag[tag_id]}"}
    tags << lined_tags
  end
end
field_name(table_name) click to toggle source
# File lib/drupal/content_type.rb, line 122
def field_name(table_name)
  "#{table_name}_value".to_sym
end
file_id(file_id) click to toggle source
# File lib/drupal/content_type.rb, line 92
def file_id(file_id)
  config.db[:file_managed].where(fid: file_id).first[:fid]
end
get_file_id(related_row, table_name) click to toggle source
# File lib/drupal/content_type.rb, line 85
def get_file_id(related_row, table_name)
  file_key = "#{table_name}_fid".to_sym
  file_id = related_row.first[file_key]
  file_asset_id = file_id(file_id)
  link_asset_to_content_type(file_asset_id)
end
id(content_id) click to toggle source
# File lib/drupal/content_type.rb, line 36
def id(content_id)
  "#{type}_#{content_id}"
end
map_fields(row, result = {}) click to toggle source
# File lib/drupal/content_type.rb, line 30
def map_fields(row, result = {})
  result.merge!(set_default_data(row))
  result.merge!(find_related_data(row))
  result
end
respond_to_file?(related_row, table_name) click to toggle source
# File lib/drupal/content_type.rb, line 80
def respond_to_file?(related_row, table_name)
  file_id = "#{table_name}_fid".to_sym
  (related_row.present? && related_row.first[file_id]) ? true : false
end
set_default_data(row, result = {}) click to toggle source
# File lib/drupal/content_type.rb, line 55
def set_default_data(row, result = {})
  result[:id] = id(row[:nid])
  result[:title] = row[:title]
  result[:author] = author(row[:uid])
  result[:tags] = tags(row[:nid]) unless tags(row[:nid]).empty?
  result[:created_at] = created_at(row[:created])
  result
end
tags(entity_row_id) click to toggle source
# File lib/drupal/content_type.rb, line 44
def tags(entity_row_id)
  entity_tags(entity_row_id).each_with_object([]) do |tag, tags|
    linked_tag = {type: 'EntryTag', id: "tag_#{tag[:field_tags_tid]}"}
    tags << linked_tag
  end
end