class Contentful::Management::Entry

Resource class for Entry. @see _ www.contentful.com/developers/documentation/content-management-api/#resources-entries

Attributes

content_type[RW]

Public Class Methods

client_association_class() click to toggle source

@private

# File lib/contentful/management/entry.rb, line 44
def self.client_association_class
  ClientEntryMethodsFactory
end
create_attributes(client, attributes) click to toggle source

@private

# File lib/contentful/management/entry.rb, line 49
def self.create_attributes(client, attributes)
  content_type = attributes.delete(:content_type)
  fields_for_create = if attributes[:fields] # create from initialized dynamic entry via save
                        tmp_entry = new
                        tmp_entry.instance_variable_set(:@fields, attributes.delete(:fields) || {})
                        Contentful::Management::Support.deep_hash_merge(
                          tmp_entry.fields_for_query,
                          tmp_entry.fields_from_attributes(attributes)
                        )
                      else
                        fields_with_locale content_type, attributes.clone
                      end

  client.register_dynamic_entry(content_type.id, DynamicEntry.create(content_type, client))
  { fields: fields_for_create, metadata: attributes[:_metadata] }
end
create_headers(_client, attributes, instance = nil) click to toggle source

@private

# File lib/contentful/management/entry.rb, line 67
def self.create_headers(_client, attributes, instance = nil)
  content_type = instance.nil? ? attributes[:content_type] : (instance.content_type || instance.sys[:contentType])
  content_type_id = content_type.respond_to?(:id) ? content_type.id : content_type[:id]

  { content_type_id: content_type_id }
end
endpoint() click to toggle source

@private

# File lib/contentful/management/entry.rb, line 39
def self.endpoint
  'entries'
end
fields_with_locale(content_type, attributes) click to toggle source

Gets Hash of fields for all locales, with locales at a field level

@return [Hash] fields by locale

# File lib/contentful/management/entry.rb, line 121
def self.fields_with_locale(content_type, attributes)
  locale = attributes[:locale] || content_type.sys[:space].default_locale
  fields = content_type.properties[:fields]
  field_names = fields.map { |field| field.id.to_sym }
  attributes = attributes.keep_if { |key| field_names.include?(key) }

  attributes.each_with_object({}) do |(id, value), result|
    field = fields.detect { |f| f.id.to_sym == id.to_sym }
    result[id] = { locale => parse_attribute_with_field(value, field) }
  end
end
parse_attribute_with_field(attribute, field) click to toggle source

@private

# File lib/contentful/management/entry.rb, line 75
def self.parse_attribute_with_field(attribute, field)
  case field.type
  when ContentType::LINK
    { sys: { type: field.type, linkType: field.link_type, id: attribute.id } } if attribute
  when ContentType::ARRAY
    parse_fields_array(attribute)
  when ContentType::LOCATION
    { lat: attribute.properties[:lat], lon: attribute.properties[:lon] } if attribute
  else
    attribute
  end
end
parse_fields_array(attributes) click to toggle source

@private

# File lib/contentful/management/entry.rb, line 94
def self.parse_fields_array(attributes)
  type = attributes.first.class
  type == String ? attributes : parse_objects_array(attributes)
end
parse_objects_array(attributes) click to toggle source

@private

# File lib/contentful/management/entry.rb, line 100
def self.parse_objects_array(attributes)
  attributes.each_with_object([]) do |attribute, result|
    result << if attribute.is_a? Entry
                hash_with_link_object('Entry', attribute)
              elsif attribute.is_a? Asset
                hash_with_link_object('Asset', attribute)
              elsif attribute.is_a? Hash
                attribute
              elsif attribute.class.ancestors.map(&:to_s).include?('Contentful::Entry')
                hash_with_link_object('Entry', attribute)
              elsif attribute.class.ancestors.map(&:to_s).include?('Contentful::Asset')
                hash_with_link_object('Asset', attribute)
              else
                attribute
              end
  end
end
pre_process_params(parameters) click to toggle source

@private

# File lib/contentful/management/entry.rb, line 34
def self.pre_process_params(parameters)
  Support.normalize_select!(parameters)
end

Public Instance Methods

after_create(attributes) click to toggle source

@private

# File lib/contentful/management/entry.rb, line 134
def after_create(attributes)
  self.locale = attributes[:locale] || client.default_locale
end
fields_for_query(remove_undefined = true) click to toggle source

Parser for entry attributes from query. Returns a hash of existing fields.

@param [Boolean] remove_undefined Returns only defined (non-nil) fields if true. This replicates the WebApp logic for empty fields, so that we stay consistent across all our software @private

# File lib/contentful/management/entry.rb, line 151
def fields_for_query(remove_undefined = true)
  raw_fields = instance_variable_get(:@fields)
  fields_names = flatten_field_names(raw_fields)
  fields_names.each_with_object({}) do |field_name, results|
    results[field_name] = raw_fields.each_with_object({}) do |(locale, fields), field_results|
      attribute_value = parse_update_attribute(fields[field_name])
      field_results[locale] = attribute_value unless attribute_value.nil? && remove_undefined
    end
  end
end
fields_from_attributes(attributes) click to toggle source

@private

# File lib/contentful/management/entry.rb, line 169
def fields_from_attributes(attributes)
  attributes.each do |id, value|
    attributes[id] = { locale => parse_update_attribute(value) }
  end
end
flatten_field_names(fields) click to toggle source

@private

# File lib/contentful/management/entry.rb, line 163
def flatten_field_names(fields)
  without_locales = fields.map { |_, v| v }
  without_locales.map(&:keys).flatten.uniq
end
locale() click to toggle source

Returns the currently supported local.

@return [String] current_locale

# File lib/contentful/management/entry.rb, line 141
def locale
  sys[:locale] || default_locale
end
references(query = {}, headers = {}) click to toggle source
# File lib/contentful/management/entry.rb, line 184
def references(query = {}, headers = {})
  ResourceRequester.new(client, self.class).all(
    {
      space_id: space.id,
      environment_id: environment_id,
      resource_id: id,
      suffix: '/references'
    },
    query,
    headers
  )
end
snapshots() click to toggle source

Allows manipulation of snapshots in context of the current entry Allows listing all snapshots belonging to this entry and finding one by id. @see _ README for details.

@return [Contentful::Management::EntrySnapshotMethodsFactory]

# File lib/contentful/management/entry.rb, line 180
def snapshots
  EntrySnapshotMethodsFactory.new(self)
end

Protected Instance Methods

query_attributes(attributes) click to toggle source
# File lib/contentful/management/entry.rb, line 199
def query_attributes(attributes)
  { metadata: attributes.delete(:_metadata),
    fields: Contentful::Management::Support.deep_hash_merge(fields_for_query, fields_from_attributes(attributes)) }
end

Private Instance Methods

fetch_content_type() click to toggle source

rubocop:enable Style/MethodMissing

# File lib/contentful/management/entry.rb, line 235
def fetch_content_type
  content_type_id = if sys[:contentType].is_a?(::Contentful::Management::Resource)
                      sys[:contentType].id
                    else
                      sys[:contentType]['sys']['id']
                    end
  space_id = space.is_a?(::Contentful::Management::Resource) ? space.id : space['sys']['id']
  @content_type ||= ::Contentful::Management::ContentType.find(client, space_id, environment_id, content_type_id)
end
method_missing(name, *args, &block) click to toggle source

rubocop:disable Style/MethodMissing

# File lib/contentful/management/entry.rb, line 222
def method_missing(name, *args, &block)
  if content_type.nil?
    fetch_content_type

    Contentful::Management::Resource::FieldAware.create_fields_for_content_type(self)

    return send(name, *args, &block) if respond_to? name
  end

  fail NameError.new("undefined local variable or method `#{name}' for #{self.class}:#{sys[:id]}", name)
end
parse_update_attribute(attribute) click to toggle source
# File lib/contentful/management/entry.rb, line 206
def parse_update_attribute(attribute)
  case attribute
  when Asset
    self.class.hash_with_link_object('Asset', attribute)
  when Entry
    self.class.hash_with_link_object('Entry', attribute)
  when Location
    { lat: attribute.properties[:lat], lon: attribute.properties[:lon] }
  when ::Array
    self.class.parse_fields_array(attribute)
  else
    attribute
  end
end