Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Multi tenant/lang
application
Simon Courtois - @happynoff
Multi-tenant you say?
mywebsite.com

otherwebsite.com

and-another.com

APP
APP

middleware

switcher

Website
Website
domain
lang

class Website < ActiveRecord::Base
def self.current
Thread.current[:website]
end
!
def self.current=(...
APP

middleware

switcher

✔ Website
Website
switcher

class WebsiteSwitcher
attr_reader :domain, :host, :website

!

!

!

!

def initialize(host)
@host = host.downca...
APP

middleware

✔ switcher
switcher

✔ Website
middleware

require 'website_switcher'

!

class WebsiteSwitcherMiddleware
def initialize(app)
@app = app
end

!
!
!

def ...
APP

✔ middleware
middleware

✔ switcher

✔ Website
require 'middlewares/website_switcher_middleware'

!
APP

module AwesomeApp
class Application < Rails::Application
# ...

...
✔ APP
APP

✔ middleware

✔ switcher

✔ Website
Questions

?
Thank you
Simon Courtois - @happynoff
Upcoming SlideShare
Loading in …5
×

Multi tenant/lang application with Ruby on Rails

1,420 views

Published on

Slides of my "Multi tenant/lang application" at Paris.rb on 2014-03-04.

Published in: Technology
  • Be the first to comment

Multi tenant/lang application with Ruby on Rails

  1. 1. Multi tenant/lang application Simon Courtois - @happynoff
  2. 2. Multi-tenant you say?
  3. 3. mywebsite.com otherwebsite.com and-another.com APP
  4. 4. APP middleware switcher Website
  5. 5. Website domain lang class Website < ActiveRecord::Base def self.current Thread.current[:website] end ! def self.current=(website) Thread.current[:website] = website end end Website.current = Website.first ! Website.current # => #<Website domain=“mywebsite.com" lang="en">
  6. 6. APP middleware switcher ✔ Website Website
  7. 7. switcher class WebsiteSwitcher attr_reader :domain, :host, :website ! ! ! ! def initialize(host) @host = host.downcase.chomp('.') end def domain @domain ||= ActionDispatch::Http::URL.extract_domain(host) end def website @website ||= Website.where(domain: domain).first || Website.where(domain: 'mywebsite.com') end def switch! Website.current = website I18n.locale = website.lang end end WebsiteSwitcher.new('www.mywebsite.com').switch!
  8. 8. APP middleware ✔ switcher switcher ✔ Website
  9. 9. middleware require 'website_switcher' ! class WebsiteSwitcherMiddleware def initialize(app) @app = app end ! ! ! def call(env) request = Rack::Request.new(env) WebsiteSwitcher.new(request.host).switch! @app.call(env) end end
  10. 10. APP ✔ middleware middleware ✔ switcher ✔ Website
  11. 11. require 'middlewares/website_switcher_middleware' ! APP module AwesomeApp class Application < Rails::Application # ... ! config.middleware.use WebsiteSwitcherMiddleware end end
  12. 12. ✔ APP APP ✔ middleware ✔ switcher ✔ Website
  13. 13. Questions ?
  14. 14. Thank you Simon Courtois - @happynoff

×