Code Monkey home page Code Monkey logo

multitenant's Introduction

Build Status

Multitenant

When building multitenant applications, never let an unscoped Model.all accidentally leak data to an unintended audience.

Usage

class User < ActiveRecord::Base
  belongs_to :tenant
  belongs_to_multitenant
  
  validates :email, uniqueness: true                    # application-wide uniqueness
  validates :alias, uniqueness: { scope: :tenant_id }   # tenant-wide uniqueness
end

Multitenant.with_tenant current_tenant do
  # queries within this block are automatically
  # scoped to the current tenant
  User.all

  # new objects created within this block are automatically
  # assigned to the current tenant
  User.create :name => 'Bob'
end

Features

  • Rails 3 compatible
  • Restrict database queries to only lookup objects for the current tenant
  • Auto assign newly created objects to the current tenant

Contributing

  • Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
  • Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
  • Fork the project
  • Start a feature/bugfix branch
  • Commit and push until you are happy with your contribution
  • Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Credits

Thanks to Lars Klevan for inspiring this project.

Copyright

Copyright (c) 2011 Ryan Sonnek. See LICENSE.txt for further details.

multitenant's People

Contributors

ledermann avatar mingan avatar racx avatar sapanakothari avatar wireframe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

multitenant's Issues

multitenant 0.2.0 gem needs to be update

Hi,

Thank you for creating a smart gem!
I want to use multitenant 0.2.0 gem for my production project, but it not contains recent fix that renamed activerecord helper.
So, we need to call belongs_to_tenant instead of belongs_to_multitenant (it's confusable...).

Could you release new version of multitenant gem?

Thanks,
juno

Uniqueness validation

Today, I noticed that when using validates :field, uniqueness: true on a model with belongs_to_multitenant doesn't cause the validation query to be restricted to the tenant.

My goal was a tenant-scoped uniqueness, but one could argue that application wide uniqueness can be meaningful in multitenant applications as well. In that spirit, I don't think it is a bug, but a sentence like "Multitenant doesn't affect Rails built-in uniqueness validation; for tenant-scoped validation use validates :field, uniqueness: { scope: :tenant_id }" in the readme would be helpful.

More examples/documentation

Thanks for this gem. I'm new to Ruby/Rails so this looks gem looks super useful for me. I am wondering exactly how I should go about using it. I understand the User class example in the readme (adding belongs_to_multitenant), but I still need more clarification on the rest of the implementation. What type of object should the "current_tenant" be? And where should that get set? If you could provide a simple example for an app that uses Devise, it would go a long way towards clarifying it for me. I plan on using this gem to help turn an app that was once single-tenant to now be multi-tenant. Thanks in advance! P.S. I would be happy to help in any way that I can.

Thread safety

IMHO, using

attr_accessor :current_tenant

is not thread-safe. You should use

Thread.current[:current_tenant]

instead.

multitenant requires multitenant (>= 0)

I just discovered multitenant, and wanted to use it on a project that I'm doing. But I was unable to install the gem, getting an odd (recursive?) error that I put in the issue title.

I got this error using Ruby Gems 1.8.2, with both Ruby 1.8.7 and 1.9.2.

Any ideas?

Reuven

Support for Rails 3.0

So far as I see the gem does not really need Rails 3.1, it should work with Rails 3.0 too.

For this, the gemspec dependency should be lowered, e.g. "ActiveRecord >= 3"

#with_tenant method doesn't do it's job for ActiveRecord relations

The problem I have faced is the following: with_tenant method works correctly only for cases where ActiveRecord relation queries db inside the with_tenant block. Otherwise, it is simply ignored.

In other words it works fine here (let's say there are 4 items and only 3 are with the tenant):

Multitenant.with_tenant @tenant do
  @items = Item.where("name IS NOT NULL").to_a 
end
puts @items.count # 3; all correct

But fails to work here:

Multitenant.with_tenant @tenant do
  @items = Item.where("name IS NOT NULL")
end
puts @items.count # 4; incorrect!

Spec that indicates the failure can be found here 81fcb09

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.