Module: Webhookdb::Postgres::ModelUtilities

Includes:
TSort
Included in:
Model
Defined in:
lib/webhookdb/postgres/model_utilities.rb

Overview

A collection of utilities that can be added to model superclasses.

Defined Under Namespace

Modules: ClassMethods, DatasetMethods, InstanceMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(model_class) ⇒ Object

Extension callback – register the model_class with Webhookdb::Postgres.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/webhookdb/postgres/model_utilities.rb', line 13

def self.extended(model_class)
  super

  # Sequel::Model API -- load some plugins
  model_class.plugin(:dirty)
  model_class.plugin(:json_serializer)
  model_class.plugin(:many_through_many)
  model_class.plugin(:subclasses)
  model_class.plugin(:tactical_eager_loading)
  model_class.plugin(:update_or_create)
  model_class.plugin(:validation_helpers)

  model_class.include(Appydays::Loggable)
  model_class.extend(ClassMethods)
  model_class.include(InstanceMethods)
  model_class.dataset_module(DatasetMethods)
  model_class.include(Webhookdb::Postgres::Validations)

  Webhookdb::Postgres.register_model_superclass(model_class)
end

Instance Method Details

#find_or_create_or_find(params) ⇒ Object

Like find_or_create, but will find again if the create call fails due to a Sequel::UniqueConstraintViolation, which is usually caused by a race condition.



191
192
193
194
195
196
197
198
# File 'lib/webhookdb/postgres/model_utilities.rb', line 191

def find_or_create_or_find(params, &)
  # Set a savepoint, because the DB error will abort the current transaction.
  self.db.transaction(savepoint: true) do
    return self.find_or_create(params, &)
  end
rescue Sequel::UniqueConstraintViolation
  return self.find(params)
end