Class: ContentfulModel::Migrations::ContentType
- Inherits:
-
Object
- Object
- ContentfulModel::Migrations::ContentType
- Defined in:
- lib/contentful_model/migrations/content_type.rb
Overview
Class for defining Content Type transformations
Instance Attribute Summary collapse
-
#id ⇒ Object
Returns the value of attribute id.
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
- #field(name, type) ⇒ Object
- #fields ⇒ Object
-
#initialize(name = nil, management_content_type = nil) ⇒ ContentType
constructor
A new instance of ContentType.
- #new? ⇒ Boolean
- #publish ⇒ Object
- #remove_field(field_id) ⇒ Object
- #save ⇒ Object
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
#id ⇒ Object
Returns the value of attribute id.
7 8 9 |
# File 'lib/contentful_model/migrations/content_type.rb', line 7 def id @id end |
#name ⇒ Object
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 |
#fields ⇒ Object
64 65 66 |
# File 'lib/contentful_model/migrations/content_type.rb', line 64 def fields @fields ||= new? ? [] : fields_from_management_type end |
#new? ⇒ 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 |
#publish ⇒ Object
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 |
#save ⇒ Object
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 |