raulperezalejo / spree_shared

Multi-tenancy for Spree using Apartment (per tenant databases).

Home Page:http://spreecommerce.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Spree Shared

Build Status Code Climate

Multiple stores using a single Spree application instance.

Uses request subdomain to swap database, Rails cache (preferences), image paths.


Installation

Add to your Gemfile:

gem 'spree_shared', github: 'spree-contrib/spree_shared', branch: 'master'

Make sure your config/database.yml has valid db connection.

Create an initializer config/initializers/apartment.rb with the following command:

bundle exec rails generate apartment:install

Search for following line:

Apartment.configure do |config|
  ...
  # supply list of database names for migrations to run on
  config.tenant_names = lambda { ToDo_Tenant_Or_User_Model.pluck :database }
end

And change it to include two sample subdomains:

Apartment.configure do |config|
  ...
  config.tenant_names = %w(store1 store2)
end

Add the following line to host application.rb

config.middleware.use 'Apartment::Elevators::Subdomain'

Change file paths and urls by adding to config/initializers/spree.rb following:

Spree::Image.attachment_definitions[:attachment][:url] = '/spree/products/:tenant/:id/:style/:basename.:extension'
Spree::Image.attachment_definitions[:attachment][:path] = ':rails_root/public/spree/products/:tenant/:id/:style/:basename.:extension'

Then allow Paperclip to access tenant from Spree::Image by adding following to Spree initializer:

Paperclip.interpolates :tenant do |attachment, _style|
  attachment.instance.tenant
end

By default tenant will resolve to Apartment::Tenant.current_tenant but you can change it - eg. suppose you use databases like tenant_12345 and only want tenant id in file path, then add following line to config/initializers/apartment.rb

Spree::Image.tenant_proc = -> { Apartment::Tenant.current_tenant.match(/(\d+)/)[1] }

Bootstrap sample stores:

bundle exec rake spree_shared:bootstrap['store1']
bundle exec rake spree_shared:bootstrap['store2']

Setup local subdomains for sample stores, as spree_shared uses by default subdomain routing you need to confirm some local domains such as:

store1.spree.dev
store2.spree.dev

This can be done using Pow or editing your local /etc/hosts file.

Set namespace for cache engine in development.rb and/or production.rb

config.cache_store = :memory_store, { namespace: lambda { Apartment::Tenant.current_tenant } }

Contributing

See corresponding guidelines


Copyright (c) 2013-2015 Spree Commerce Inc, and other contributors, released under the New BSD License

About

Multi-tenancy for Spree using Apartment (per tenant databases).

http://spreecommerce.com

License:Other


Languages

Language:Ruby 100.0%