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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of ContentType.



9
10
11
12
# File 'lib/contentful_model/migrations/content_type.rb', line 9

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

Instance Attribute Details

#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



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/contentful_model/migrations/content_type.rb', line 40

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



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

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

#new?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/contentful_model/migrations/content_type.rb', line 60

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

#publishObject



32
33
34
35
36
37
38
# File 'lib/contentful_model/migrations/content_type.rb', line 32

def publish
  return self if new?

  @management_content_type.publish

  self
end

#remove_field(field_id) ⇒ Object



56
57
58
# File 'lib/contentful_model/migrations/content_type.rb', line 56

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

#saveObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/contentful_model/migrations/content_type.rb', line 14

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

  self
end