Module: Knievel::Resources::Upsertable

Included in:
Advertisers
Defined in:
lib/knievel/resources/upsertable.rb

Overview

Single-row upsert-by-external-id helper. Builds a ‘BatchUpsert*Request` carrying exactly one row and calls the resource’s ‘batch_upsert_*` endpoint — which is a single Postgres transaction with the upsert keyed off `external_id`. Returns the upserted row from `result.items.first`.

Per ‘MIGRATION_RX.md`: this is the path the rx-side wrapper used to take by hand. Folding it into the gem means callers stop reinventing the create/409/update dance and the rx wrapper can be deleted.

Subclasses configure:

* `batch_upsert_method`  - method name on `api_class`
* `batch_upsert_request_class`  - `BatchUpsert*Request` model
* `batch_upsert_row_class`      - `BatchUpsert*Row` model

Instance Method Summary collapse

Instance Method Details

#upsert(external_id:, **attrs) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/knievel/resources/upsertable.rb', line 22

def upsert(external_id:, **attrs)
  unless respond_to?(:batch_upsert_method) &&
         respond_to?(:batch_upsert_request_class) &&
         respond_to?(:batch_upsert_row_class)
    raise NotImplementedError, "#{self.class} did not configure Upsertable"
  end
  row = batch_upsert_row_class.new(attrs.merge(external_id: external_id))
  request = batch_upsert_request_class.new(items: [row])
  api = api_class.new(@api_client)
  result = api.public_send(batch_upsert_method, @project_id, request)
  result.items.first
end