Class: ContentfulModel::Migrations::ContentType

Inherits:
Object
  • Object
show all
Defined in:
lib/contentful_model/migrations/content_type.rb

Overview

Class for defining Content Type transformations

Constant Summary collapse

MANAGEMENT_TYPE_MAPPING =
{
  'string' => 'Symbol',
  'rich_text' => 'RichText'
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, management_content_type = nil) ⇒ ContentType

Returns a new instance of ContentType.



14
15
16
17
# File 'lib/contentful_model/migrations/content_type.rb', line 14

def initialize(name = nil, management_content_type = nil)
  @name = name
  @management_content_type = management_content_type
end

Instance Attribute Details

#display_fieldObject

Returns the value of attribute display_field.



7
8
9
# File 'lib/contentful_model/migrations/content_type.rb', line 7

def display_field
  @display_field
end

#idObject

Returns the value of attribute id.



7
8
9
# File 'lib/contentful_model/migrations/content_type.rb', line 7

def id
  @id
end

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/contentful_model/migrations/content_type.rb', line 7

def name
  @name
end

Instance Method Details

#field(name, type) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/contentful_model/migrations/content_type.rb', line 47

def field(name, type)
  name = name.to_s
  type = type.to_s

  new_field = Contentful::Management::Field.new
  new_field.id = name.split(' ').map(&:capitalize).join('').underscore
  new_field.name = name
  new_field.type = management_type(type)
  new_field.link_type = management_link_type(type) if link?(type)
  new_field.items = management_items(type) if array?(type)

  fields << new_field

  new_field
end

#fieldsObject



71
72
73
# File 'lib/contentful_model/migrations/content_type.rb', line 71

def fields
  @fields ||= new? ? [] : fields_from_management_type
end

#new?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/contentful_model/migrations/content_type.rb', line 67

def new?
  @management_content_type.nil? || @management_content_type.id.nil?
end

#publishObject



39
40
41
42
43
44
45
# File 'lib/contentful_model/migrations/content_type.rb', line 39

def publish
  return self if new?

  @management_content_type.publish

  self
end

#remove_field(field_id) ⇒ Object



63
64
65
# File 'lib/contentful_model/migrations/content_type.rb', line 63

def remove_field(field_id)
  @management_content_type.fields.destroy(field_id)
end

#saveObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/contentful_model/migrations/content_type.rb', line 19

def save
  if new?
    @management_content_type = management.content_types(
      ContentfulModel.configuration.space,
      ContentfulModel.configuration.environment
    ).create(
      id: id || camel_case(@name),
      name: @name,
      displayField: display_field,
      fields: fields
    )
  else
    @management_content_type.fields = @fields
    @management_content_type.displayField = display_field if display_field
    @management_content_type.save
  end

  self
end