Method: Webhookdb::SpecHelpers::Postgres.create_model

Defined in:
lib/webhookdb/spec_helpers/postgres.rb

.create_model(name) ⇒ Object

Create an anonymous model with the given table name. Can be a symbol, string, or [:schema, :table] array.



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/webhookdb/spec_helpers/postgres.rb', line 93

module_function def create_model(name, &)
  Webhookdb::SpecHelpers::Postgres.current_test_model_uid += 1

  qualifier = Sequel
  prefix = name
  if name.is_a?(Array)
    qualifier = Sequel[name[0]]
    Webhookdb::Postgres::Model.create_schema!(qualifier)
    prefix = name[1]
  end
  table_name = :"testtable_#{prefix}_#{Webhookdb::SpecHelpers::Postgres.current_test_model_uid}"
  qualified_name = qualifier[table_name]

  Webhookdb::Postgres.logger.info "Creating table: %p" % [qualified_name]
  Webhookdb::Postgres::Model.db.create_table!(qualified_name, &)
  clsname = table_name.to_s.classify
  clsfqn = "#{Webhookdb::SpecHelpers::Postgres::Models}::#{clsname}"
  cls = Class.new(Webhookdb::Postgres::Model(qualified_name)) do
    define_singleton_method(:name) { clsfqn }
  end
  Webhookdb::SpecHelpers::Postgres::Models.const_set(clsname, cls)
  # Object.const_get(clsfqn)
  return cls
end