Skip to content

A very simple multi-tenant library for ruby with mongodb (based on mongoid).

License

Notifications You must be signed in to change notification settings

flanker/simple_tenant

Repository files navigation

Simple Tenant

Build Status

A very simple multi-tenant library for ruby with mongodb (based on mongoid).

Installation

Add this line to your application's Gemfile:

gem 'simple_tenant'

And then execute:

$ bundle

Or install it yourself as:

$ gem install simple_tenant

Usage

Model definition:

  class TestModel
    include Mongoid::Document
    include SimpleTenant::ModelExtensions

    field :name, type: String
    field :number, type: Integer
    field :text, type: String
    field :deleted_at, type: Time
    field :tenant_id, type: Integer

    tenanted_by :tenant_id

    default_scope { where(deleted_at: nil) }
  end

and:

TestModel.create name: 'document with tenant', tenant_id: 828
TestModel.create name: 'document with another tenant', tenant_id: 1113
TestModel.create name: 'document without tenant'

SimpleTenant.current_tenant = 828

TestModel.count    # => 1
TestModel.first.name    # ==> document with tenant

See more usage example in specs:

  1. SimpleTenant::ModelExtensions
  2. SimpleTenant::ModelExtensions for relation
  3. SimpleTenant::ModelExtensions for inheritance

About

A very simple multi-tenant library for ruby with mongodb (based on mongoid).

Topics

Resources

License

Stars

Watchers

Forks