Write implementation

This is the official documentation of the agent_ruby Ruby agent.

Making your records editable is achieved by implementing the create, update and delete methods.

The 3 methods take a filter as a parameter but note that, unlike the list method, there is no need to support paging.

module App
  module Collections
    class MyCollection < ForestAdminDatasourceToolkit::Collection
      include ForestAdminDatasourceToolkit::Schema
      include ForestAdminDatasourceToolkit::Components::Query
      include HTTParty

      base_uri 'https://my-api'

      def initialize(datasource)
        super(datasource, 'Article')

        add_field('id', ColumnSchema.new(
          # ...
          is_read_only: true,
        ))

        add_field('title', ColumnSchema.new(
          # ...
          is_read_only: false,
        ))
      end

      def create(caller, records)
        results = []

        records.each do |record|
          response = self.class.post(
            '/my-collection',
            body: record.to_json,
            headers: { 'Content-Type' => 'application/json' }
          )

          results << JSON.parse(response.body)
        end

        results
      end

      def update(caller, filter, patch)
        record_ids = list(caller, filter, Projection.new(['id']))

        record_ids.each do |record|
          self.class.patch(
            "/my-collection/#{record['id']}",
            body: patch.to_json,
            headers: { 'Content-Type' => 'application/json' }
          )
        end
      end

      def delete(caller, filter)
        record_ids = list(caller, filter, Projection.new(['id']))

        record_ids.each do |record|
          self.class.delete(
            "/my-collection/#{record['id']}",
            headers: { 'Content-Type' => 'application/json' }
          )
        end
      end

      private

      def list(caller, filter, projection)
        # Implémentation de la méthode `list` pour récupérer les IDs des enregistrements
        raise NotImplementedError, 'list method is not implemented'
      end
    end
  end
end

Last updated

Was this helpful?