Skip to content

ikraamg/solidus_multi_tenant

Repository files navigation

⚠️ Important: This project is under active development. It is valid for insipiration as similar work has been successfully used in production, however this extention has not yet been validated.

Solidus Multi Tenant

CircleCI codecov

This extension provides a way to manage multiple tenants within a single Solidus installation using an active fork of the Apartment gem. Please refer to the gem documentation for more information on setup as this gem enhances the implemntation to get quickly up and running with Solidus.

Solidus works better with Apartment for multi-tenancy as it needs minimal application modifications, however, if you are looking for a row-level multi-tenancy solution with Solidus, please check out my other work with solidus_acts_as_tenant.

Installation

Add solidus_multi_tenant to your Gemfile:

gem 'solidus_multi_tenant'

Bundle your dependencies and run the installation generator:

bin/rails generate solidus_multi_tenant:install

Scope the cache store to the current tenant

  config.cache_store = :memory_store, {namespace: -> { Apartment::Tenant.current }}

Add this to your .irbrc file to load the TenantSelector to support tenant switching in the console

if defined?(Rails)
  TS = SolidusActAsTenant::Utils::TenantSelector.new

  IRB.conf[:IRB_RC] = proc do
    #   * TS.ask             => anytime in console, to switch tenant from a list
    #   * TS.current         => same as Apartment::Tenant.current
    #   * TS.tenants         => hash of tenants. Example: { 0 => "Demo Company" }
    #   * TS.switch_tenant!(tenant_name) => same as Apartment.current = Spree::Tenant.find_by(name: tenant_name)
    TS.ask
  end
end

Load the spree settings per tenant into the namespaced cache

TODO: Double check if this is a valid suggestion and if it is, does it work because spree preferences are not namespaced

#initializers/spree.rb
::Apartment::Tenant.each_with_default do |_tenant_name| -

Usage

Development

Testing the extension

First bundle your dependencies, then run bin/rake. bin/rake will default to building the dummy app if it does not exist, then it will run specs. The dummy app can be regenerated by using bin/rake extension:test_app.

bin/rake

To run Rubocop static code analysis run

bundle exec rubocop

When testing your application's integration with this extension you may use its factories. You can load Solidus core factories along with this extension's factories using this statement:

SolidusDevSupport::TestingSupport::Factories.load_for(SolidusMultiTenant::Engine)

Running the sandbox

To run this extension in a sandboxed Solidus application, you can run bin/sandbox. The path for the sandbox app is ./sandbox and bin/rails will forward any Rails commands to sandbox/bin/rails.

Here's an example:

$ bin/rails server
=> Booting Puma
=> Rails 6.0.2.1 application starting in development
* Listening on tcp://127.0.0.1:3000
Use Ctrl-C to stop

Releasing new versions

Please refer to the dedicated page in the Solidus wiki.

License

Copyright (c) 2025 Ikraam Ghoor, released under the New BSD License.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published