简体   繁体   中英

How do I set date format for Contentful field in Ruby?

Below is a Contentful migration I wrote to create a content model called 'Trip' in Contentful. What I would like to do is specify the format of the "Start Date" and "End Date" fields. Contentful gives you three formatting options that can be set in the UI:

  1. Date only
  2. Date and time without timezone
  3. Date and time with timezone

Without specifying the format in my migration file, I get format #3 by default and I need format #1. Anyone familiar with how to do this?

Thanks!

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

When I export my content types using the contentful export command via the Contentful CLI, I can see something similar to this in my 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"
        }

Now, I don't use the Ruby migration tooling, but this leads me to believe you can set field['widgetId'] = 'datePicker' and

field['settings'] = {
  'format' => 'dateonly',
  'helpText' => ...
}

Let me know if that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM