如何为 Ruby 中的 Contentful 字段设置日期格式?
How do I set date format for Contentful field in Ruby?
下面是我编写的一个 Contentful 迁移,用于在 Contentful 中创建一个名为 'Trip' 的内容模型。我想做的是指定 "Start Date" 和 "End Date" 字段的格式。 Contentful 为您提供三种格式选项,可以在 UI:
中设置
- 仅限日期
- 没有时区的日期和时间
- 带时区的日期和时间
没有在我的迁移文件中指定格式,默认情况下我得到格式#3,我需要格式#1。有人熟悉如何执行此操作吗?
谢谢!
class CreateTrip < RevertableMigration
self.content_type_id = 'trip'
def up
with_space do |space|
# Create content model
content_type = space.content_types.create(
name: 'Trip',
id: content_type_id,
description: 'Content model for trip cards'
)
# Set validation
validation_for_country = Contentful::Management::Validation.new
validation_for_country.in = ['Bolivia','Haiti','India','Nicaragua', 'Puerto Rico', 'South Africa']
content_type.fields.create(id: 'image', name: 'Image', type: 'Link', link_type: 'Asset', required: true)
content_type.fields.create(id: 'country', name: 'Country', type: 'Symbol', required: true, validations: [validation_for_country])
content_type.fields.create(id: 'trip_details', name: 'Trip Details', type: 'Symbol')
content_type.fields.create(id: 'start_date', name: 'Start Date', type: 'Date', required: true)
content_type.fields.create(id: 'end_date', name: 'End Date', type: 'Date', required: true)
content_type.fields.create(id: 'trip_description', name: 'Trip Description', type: 'Text')
content_type.fields.create(id: 'link_url', name: 'Link URL', type: 'Symbol', required: true)
# Publish
content_type.save
content_type.publish
# Editor interface config
editor_interface = content_type.editor_interface.default
controls = editor_interface.controls
field = controls.detect { |e| e['fieldId'] == 'trip_details' }
field['settings'] = { 'helpText' => 'City, month, participant type, etc.' }
editor_interface.update(controls: controls)
editor_interface.reload
content_type.save
content_type.publish
end
end
end
当我通过 Contentful CLI 使用 contentful export
命令导出我的内容类型时,我可以在我的 JSON:
中看到类似的内容
{
"fieldId": "endDate",
"settings": {
"ampm": "24",
"format": "timeZ",
"helpText": "(Optional) The date and time when the event ends..."
},
"widgetId": "datePicker"
},
{
"fieldId": "internalTitle",
"widgetId": "singleLine"
},
{
"fieldId": "startDate",
"settings": {
"ampm": "24",
"format": "timeZ",
"helpText": "The date/time when this schedule starts..."
},
"widgetId": "datePicker"
}
现在,我不使用 Ruby 迁移工具,但这让我相信您可以设置 field['widgetId'] = 'datePicker'
和
field['settings'] = {
'format' => 'dateonly',
'helpText' => ...
}
如果有帮助请告诉我!
下面是我编写的一个 Contentful 迁移,用于在 Contentful 中创建一个名为 'Trip' 的内容模型。我想做的是指定 "Start Date" 和 "End Date" 字段的格式。 Contentful 为您提供三种格式选项,可以在 UI:
中设置- 仅限日期
- 没有时区的日期和时间
- 带时区的日期和时间
没有在我的迁移文件中指定格式,默认情况下我得到格式#3,我需要格式#1。有人熟悉如何执行此操作吗?
谢谢!
class CreateTrip < RevertableMigration
self.content_type_id = 'trip'
def up
with_space do |space|
# Create content model
content_type = space.content_types.create(
name: 'Trip',
id: content_type_id,
description: 'Content model for trip cards'
)
# Set validation
validation_for_country = Contentful::Management::Validation.new
validation_for_country.in = ['Bolivia','Haiti','India','Nicaragua', 'Puerto Rico', 'South Africa']
content_type.fields.create(id: 'image', name: 'Image', type: 'Link', link_type: 'Asset', required: true)
content_type.fields.create(id: 'country', name: 'Country', type: 'Symbol', required: true, validations: [validation_for_country])
content_type.fields.create(id: 'trip_details', name: 'Trip Details', type: 'Symbol')
content_type.fields.create(id: 'start_date', name: 'Start Date', type: 'Date', required: true)
content_type.fields.create(id: 'end_date', name: 'End Date', type: 'Date', required: true)
content_type.fields.create(id: 'trip_description', name: 'Trip Description', type: 'Text')
content_type.fields.create(id: 'link_url', name: 'Link URL', type: 'Symbol', required: true)
# Publish
content_type.save
content_type.publish
# Editor interface config
editor_interface = content_type.editor_interface.default
controls = editor_interface.controls
field = controls.detect { |e| e['fieldId'] == 'trip_details' }
field['settings'] = { 'helpText' => 'City, month, participant type, etc.' }
editor_interface.update(controls: controls)
editor_interface.reload
content_type.save
content_type.publish
end
end
end
当我通过 Contentful CLI 使用 contentful export
命令导出我的内容类型时,我可以在我的 JSON:
{
"fieldId": "endDate",
"settings": {
"ampm": "24",
"format": "timeZ",
"helpText": "(Optional) The date and time when the event ends..."
},
"widgetId": "datePicker"
},
{
"fieldId": "internalTitle",
"widgetId": "singleLine"
},
{
"fieldId": "startDate",
"settings": {
"ampm": "24",
"format": "timeZ",
"helpText": "The date/time when this schedule starts..."
},
"widgetId": "datePicker"
}
现在,我不使用 Ruby 迁移工具,但这让我相信您可以设置 field['widgetId'] = 'datePicker'
和
field['settings'] = {
'format' => 'dateonly',
'helpText' => ...
}
如果有帮助请告诉我!