Implemented routing filter for foodcoop select.

* Allows now a multi coop installation for one domain!
* SSL, we are coming...
* TODO: Remove all the hardcoded urls, check email-links etc.
This commit is contained in:
Benjamin Meichsner 2010-03-20 02:26:30 +01:00
parent b9576dd669
commit e7af7e82b5
4 changed files with 151 additions and 105 deletions

View file

@ -139,12 +139,17 @@ class ApplicationController < ActionController::Base
# Use this method as a before filter (first filter!) in ApplicationController # Use this method as a before filter (first filter!) in ApplicationController
def select_foodcoop def select_foodcoop
if Foodsoft.config[:multi_coop_install] if Foodsoft.config[:multi_coop_install]
# Get subdomain if !params[:foodcoop].blank?
subdomain = request.subdomains.first
# Set Config # Set Config
Foodsoft.env = subdomain Foodsoft.env = params[:foodcoop]
# Set database-connection # Set database-connection
ActiveRecord::Base.establish_connection(Foodsoft.database(subdomain)) ActiveRecord::Base.establish_connection(Foodsoft.database)
else
redirect_to root_path
end
else
# Deactivate routing filter
RoutingFilter::Foodcoop.active = false
end end
end end
end end

View file

@ -68,6 +68,7 @@ Rails::Initializer.run do |config|
config.gem "fastercsv" config.gem "fastercsv"
config.gem "prawn" config.gem "prawn"
config.gem "haml", :version => '>=2.0.6' config.gem "haml", :version => '>=2.0.6'
config.gem "routing-filter", :lib => "routing_filter"
# The internationalization framework can be changed to have another default locale (standard is :en) or more load paths. # The internationalization framework can be changed to have another default locale (standard is :en) or more load paths.
# All files from config/locales/*.rb,yml are added automatically. # All files from config/locales/*.rb,yml are added automatically.

View file

@ -1,4 +1,7 @@
ActionController::Routing::Routes.draw do |map| ActionController::Routing::Routes.draw do |map|
map.filter 'foodcoop', :file => File.join(RAILS_ROOT, "lib", "foodcoop_filter")
map.resources :pages, :collection => { :all => :get }, :member => {:version => :get, :revert => :get} map.resources :pages, :collection => { :all => :get }, :member => {:version => :get, :revert => :get}
map.wiki_page "/wiki/:permalink", :controller => 'pages', :action => 'show', :permalink => /[^\s]+/ map.wiki_page "/wiki/:permalink", :controller => 'pages', :action => 'show', :permalink => /[^\s]+/
map.wiki "/wiki", :controller => 'pages', :action => 'show', :permalink => 'Home' map.wiki "/wiki", :controller => 'pages', :action => 'show', :permalink => 'Home'

37
lib/foodcoop_filter.rb Normal file
View file

@ -0,0 +1,37 @@
require 'routing_filter/base'
module RoutingFilter
class Foodcoop < Base
def around_recognize(path, env, &block)
token = extract_token!(path) # remove the token from the beginning of the path
returning yield do |params| # invoke the given block (calls more filters and finally routing)
params[:foodcoop] = token if token # set recognized token to the resulting params hash
end
end
def around_generate(*args, &block)
token = args.extract_options!.delete(:foodcoop) # extract the passed :token option
token = Foodsoft.env if token.nil? # default to Foodsoft.env
returning yield do |result|
if token
url = result.is_a?(Array) ? result.first : result
prepend_token!(url, token)
end
end
end
protected
def extract_token!(path)
foodcoop = nil
path.sub! %r(^/([a-zA-Z0-9]*)(?=/|$)) do foodcoop = $1; '' end
foodcoop
end
def prepend_token!(url, token)
url.sub!(%r(^(http.?://[^/]*)?(.*))) { "#{$1}/#{token}#{$2}" }
end
end
end