Method: Contentful::Management::ContentType#fields

Defined in:
lib/contentful/management/content_type.rb

#fieldsContentful::Management::ContentType

Use this method only in the context of content type. Allows you to add and create a field with specified attributes or destroy by pass field id.

Returns:

See Also:

  • README for details.

100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/contentful/management/content_type.rb', line 100

def fields
  fields = orig_fields

  fields.instance_exec(self) do |content_type|
    fields.define_singleton_method(:add) do |field|
      content_type.update(fields: content_type.merged_fields(field))
    end

    fields.define_singleton_method(:create) do |params|
      field = Contentful::Management::Field.new
      Field.property_coercions.each do |key, _value|
        snakify_key = Support.snakify(key)
        param = params[snakify_key.to_sym]
        field.send("#{snakify_key}=", param) if param
      end
      content_type.update(fields: content_type.merged_fields(field))
    end

    fields.define_singleton_method(:destroy) do |id|
      fields = content_type.fields.reject { |field| field.id == id }
      content_type.update(fields: fields)
    end
  end

  fields
end