Module: MultiTenant::DatabaseStatements

Defined in:
lib/activerecord-multi-tenant/query_rewriter.rb

Instance Method Summary collapse

Instance Method Details

#delete(arel, name = nil, binds = []) ⇒ Object


239
240
241
242
243
244
245
# File 'lib/activerecord-multi-tenant/query_rewriter.rb', line 239

def delete(arel, name = nil, binds = [])
  model = MultiTenant.multi_tenant_model_for_arel(arel)
  if model.present? && !MultiTenant.with_write_only_mode_enabled? && MultiTenant.current_tenant_id.present?
    arel.where(MultiTenant::TenantEnforcementClause.new(model.arel_table[model.partition_key]))
  end
  super(arel, name, binds)
end

#join_to_delete(delete, *args) ⇒ Object


222
223
224
225
226
227
228
229
# File 'lib/activerecord-multi-tenant/query_rewriter.rb', line 222

def join_to_delete(delete, *args)
  delete = super(delete, *args)
  model = MultiTenant.multi_tenant_model_for_table(delete.ast.left.table_name)
  if model.present? && !MultiTenant.with_write_only_mode_enabled? && MultiTenant.current_tenant_id.present?
    delete.where(MultiTenant::TenantEnforcementClause.new(model.arel_table[model.partition_key]))
  end
  delete
end

#join_to_update(update, *args) ⇒ Object


213
214
215
216
217
218
219
220
# File 'lib/activerecord-multi-tenant/query_rewriter.rb', line 213

def join_to_update(update, *args)
  update = super(update, *args)
  model = MultiTenant.multi_tenant_model_for_table(update.ast.relation.table_name)
  if model.present? && !MultiTenant.with_write_only_mode_enabled? && MultiTenant.current_tenant_id.present?
    update.where(MultiTenant::TenantEnforcementClause.new(model.arel_table[model.partition_key]))
  end
  update
end

#update(arel, name = nil, binds = []) ⇒ Object


231
232
233
234
235
236
237
# File 'lib/activerecord-multi-tenant/query_rewriter.rb', line 231

def update(arel, name = nil, binds = [])
  model = MultiTenant.multi_tenant_model_for_arel(arel)
  if model.present? && !MultiTenant.with_write_only_mode_enabled? && MultiTenant.current_tenant_id.present?
    arel.where(MultiTenant::TenantEnforcementClause.new(model.arel_table[model.partition_key]))
  end
  super(arel, name, binds)
end