Ruby on Rails [ Ruby On Rails.ppt ] - [Ruby - [Ruby-Doc.org ...
Multi tenant/lang application with Ruby on Rails
Click here to load reader
-
Upload
simon-courtois -
Category
Technology
-
view
702 -
download
1
description
Transcript of Multi tenant/lang application with Ruby on Rails
Multi tenant/lang applicationSimon Courtois - @happynoff
Multi-tenant you say?
mywebsite.com
otherwebsite.com
and-another.com
APP
WebsiteAPP switchermiddleware
Websiteclass 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">
domain lang
WebsiteAPP switcher ✔ Websitemiddleware
switcherclass 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!
APP switcher✔ switcher ✔ Websitemiddleware
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
middleware
middlewareAPP ✔ switcher ✔ Website✔ middleware
APP
require 'middlewares/website_switcher_middleware' !module AwesomeApp class Application < Rails::Application # ... ! config.middleware.use WebsiteSwitcherMiddleware end end
APP ✔ switcher ✔ Website✔ middleware✔ APP
Questions?
Thank youSimon Courtois - @happynoff