47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/activerecord-multi-tenant/multi_tenant.rb', line 47
def multi_tenant(tenant, options = {})
self.primary_key = 'id' if primary_key.nil?
if to_s.underscore.to_sym != tenant
belongs_to(tenant)
acts_as_tenant(tenant, options)
around_save -> (record, block) { persisted? ? MultiTenant.with_id(record.public_send(tenant.to_s + '_id')) { block.call } : block.call }
around_update -> (record, block) { MultiTenant.with_id(record.public_send(tenant.to_s + '_id')) { block.call } }
around_destroy -> (record, block) { MultiTenant.with_id(record.public_send(tenant.to_s + '_id')) { block.call } }
end
if to_s.underscore.to_sym == tenant
before_create -> { self.id ||= self.class.connection.select_value("SELECT nextval('" + [self.class.table_name, self.class.primary_key, 'seq'].join('_') + "'::regclass)") }
end
end
|