Successfully reported this slideshow.
Your SlideShare is downloading. ×

Multi tenant/lang application with Ruby on Rails

Ad

Multi tenant/lang
application
Simon Courtois - @happynoff

Ad

Multi-tenant you say?

Ad

mywebsite.com

otherwebsite.com

and-another.com

APP

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Check these out next

1 of 14 Ad
1 of 14 Ad

More Related Content

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

×