class ContentfulModel::Migrations::ContentType
Class for defining Content Type transformations
Constants
- MANAGEMENT_TYPE_MAPPING
Attributes
display_field[RW]
id[RW]
name[RW]
Public Class Methods
new(name = nil, management_content_type = nil)
click to toggle source
# 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
Public Instance Methods
field(name, type)
click to toggle source
# 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
fields()
click to toggle source
# File lib/contentful_model/migrations/content_type.rb, line 72 def fields @fields ||= new? ? [] : fields_from_management_type end
new?()
click to toggle source
# File lib/contentful_model/migrations/content_type.rb, line 68 def new? @management_content_type.nil? || @management_content_type.id.nil? end
publish()
click to toggle source
# 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)
click to toggle source
# File lib/contentful_model/migrations/content_type.rb, line 63 def remove_field(field_id) @management_content_type.fields.destroy(field_id) @fields = fields_from_management_type end
save()
click to toggle source
# 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.display_field = display_field if display_field @management_content_type.save end self end
Private Instance Methods
array?(type)
click to toggle source
# File lib/contentful_model/migrations/content_type.rb, line 128 def array?(type) type.end_with?('_array') end
camel_case(a_string)
click to toggle source
# File lib/contentful_model/migrations/content_type.rb, line 78 def camel_case(a_string) a_string.split(/\s|_|-/).inject([]) { |a, e| a.push(a.empty? ? e.downcase : e.capitalize) }.join end
fields_from_management_type()
click to toggle source
# File lib/contentful_model/migrations/content_type.rb, line 82 def fields_from_management_type @management_content_type.fields end
link?(type)
click to toggle source
# File lib/contentful_model/migrations/content_type.rb, line 124 def link?(type) type.end_with?('_link') end
management()
click to toggle source
# File lib/contentful_model/migrations/content_type.rb, line 132 def management @management ||= ContentfulModel::Management.new end
management_items(type)
click to toggle source
# File lib/contentful_model/migrations/content_type.rb, line 106 def management_items(type) if %i[entry_array asset_array symbol_array].include?(type.to_sym) array_type = type.split('_').first.capitalize items = Contentful::Management::Field.new if %i[entry_array asset_array].include?(type.to_sym) items.type = 'Link' items.link_type = array_type else items.type = array_type end items else raise_field_type_error(type) end end
management_link_type(type)
click to toggle source
# File lib/contentful_model/migrations/content_type.rb, line 100 def management_link_type(type) raise_field_type_error(type) unless %i[entry_link asset_link].include?(type.to_sym) type.split('_').first.capitalize end
management_type(type)
click to toggle source
# File lib/contentful_model/migrations/content_type.rb, line 86 def management_type(type) if %i[text symbol integer number date boolean location object].include?(type.to_sym) type.capitalize elsif link?(type) 'Link' elsif array?(type) 'Array' elsif MANAGEMENT_TYPE_MAPPING.key?(type.to_s) MANAGEMENT_TYPE_MAPPING[type.to_s] else raise_field_type_error(type) end end
raise_field_type_error(type)
click to toggle source
# File lib/contentful_model/migrations/content_type.rb, line 136 def raise_field_type_error(type) fail ContentfulModel::Migrations::InvalidFieldTypeError, "`:#{type}' is not a valid Field Type" end