lib/saasable/middleware.rb in saasable-0.3.0 vs lib/saasable/middleware.rb in saasable-0.3.1
- old
+ new
@@ -1,20 +1,21 @@
class Saasable::Middleware
def initialize app
@app = app
- end
-
- def call env
+ @_saas_for_host = {}
+
# Loads all models so we know how to apply the scopes on Rails
Rails::Mongoid.load_models(Rails.application) if defined?(Rails::Mongoid)
-
+ end
+
+ def call env
env[:saasable] = {:current_saas => saas_for_host(env["SERVER_NAME"])}
env[:saasable][:current_saas].activate! if env[:saasable][:current_saas]
-
+
@app.call env
end
-
+
private
def saas_for_host hostname
- Saasable::Mongoid::SaasDocument.saas_document.find_by_host!(hostname) rescue nil
+ @_saas_for_host[hostname] ||= Saasable::Mongoid::SaasDocument.saas_document.find_by_host!(hostname) rescue nil
end
end
\ No newline at end of file