Merge branch 'routing-filter'

This commit is contained in:
Benjamin Meichsner 2010-05-25 23:16:49 +02:00
commit 00d9f060cc
29 changed files with 282 additions and 260 deletions

View File

@ -1,7 +1,5 @@
class AdminController < ApplicationController class Admin::BaseController < ApplicationController
before_filter :authenticate_admin before_filter :authenticate_admin
filter_parameter_logging :password, :password_confirmation # do not log passwort parameters
def index def index
@user = self.current_user @user = self.current_user

View File

@ -1,5 +1,4 @@
class Admin::OrdergroupsController < ApplicationController class Admin::OrdergroupsController < Admin::BaseController
before_filter :authenticate_admin
def index def index
if (params[:per_page] && params[:per_page].to_i > 0 && params[:per_page].to_i <= 100) if (params[:per_page] && params[:per_page].to_i > 0 && params[:per_page].to_i <= 100)

View File

@ -1,6 +1,4 @@
class Admin::UsersController < ApplicationController class Admin::UsersController < Admin::BaseController
before_filter :authenticate_admin
filter_parameter_logging :password, :password_confirmation # do not log passwort parameters
def index def index
if (params[:per_page] && params[:per_page].to_i > 0 && params[:per_page].to_i <= 100) if (params[:per_page] && params[:per_page].to_i > 0 && params[:per_page].to_i <= 100)

View File

@ -1,5 +1,4 @@
class Admin::WorkgroupsController < ApplicationController class Admin::WorkgroupsController < Admin::BaseController
before_filter :authenticate_admin
def index def index
if (params[:per_page] && params[:per_page].to_i > 0 && params[:per_page].to_i <= 100) if (params[:per_page] && params[:per_page].to_i > 0 && params[:per_page].to_i <= 100)

View File

@ -1,5 +1,6 @@
class ApplicationController < ActionController::Base class ApplicationController < ActionController::Base
filter_parameter_logging :password, :password_confirmation # do not log passwort parameters
before_filter :select_foodcoop, :authenticate, :store_controller before_filter :select_foodcoop, :authenticate, :store_controller
after_filter :remove_controller after_filter :remove_controller
@ -23,128 +24,137 @@ class ApplicationController < ActionController::Base
protected protected
def current_user def current_user
begin begin
# check if there is a valid session and return the logged-in user (its object) # check if there is a valid session and return the logged-in user (its object)
if session['user_and_subdomain'] if session[:user] and session[:foodcoop]
id, subdomain = session['user_and_subdomain'].split # for shared-host installations. check if the cookie-subdomain fits to request.
# for shared-host installations. check if the cookie-subdomain fits to request. return User.current_user = User.find(session[:user]) if session[:foodcoop] == Foodsoft.env
return User.current_user = User.find(id) if request.subdomains.first == subdomain
end
rescue
reset_session
flash[:error]= _("An error has occurred. Please login again.")
redirect_to :controller => 'login'
end end
rescue
reset_session
flash[:error]= _("An error has occurred. Please login again.")
redirect_to :controller => 'login'
end end
end
def current_user=(user) def current_user=(user)
session['user_and_subdomain'] = [user.id, request.subdomains.first].join(" ") session[:user], session[:foodcoop] = user.id, Foodsoft.env
end end
def return_to def return_to
session['return_to'] session['return_to']
end end
def return_to=(uri) def return_to=(uri)
session['return_to'] = uri session['return_to'] = uri
end end
def deny_access def deny_access
self.return_to = request.request_uri self.return_to = request.request_uri
redirect_to :controller => '/login', :action => 'denied' redirect_to :controller => '/login', :action => 'denied'
return false return false
end end
private private
def authenticate(role = 'any') def authenticate(role = 'any')
# Attempt to retrieve authenticated user from controller instance or session... # Attempt to retrieve authenticated user from controller instance or session...
if !(user = current_user) if !(user = current_user)
# No user at all: redirect to login page. # No user at all: redirect to login page.
self.return_to = request.request_uri self.return_to = request.request_uri
redirect_to :controller => '/login' redirect_to :controller => '/login'
return false return false
else
# We have an authenticated user, now check role...
# Roles gets the user through his memberships.
hasRole = case role
when "admin" then user.role_admin?
when "finance" then user.role_finance?
when "article_meta" then user.role_article_meta?
when "suppliers" then user.role_suppliers?
when "orders" then user.role_orders?
when "any" then true # no role required
else false # any unknown role will always fail
end
if hasRole
@current_user = user
else else
# We have an authenticated user, now check role... deny_access
# Roles gets the user through his memberships. end
hasRole = case role
when "admin" then user.role_admin?
when "finance" then user.role_finance?
when "article_meta" then user.role_article_meta?
when "suppliers" then user.role_suppliers?
when "orders" then user.role_orders?
when "any" then true # no role required
else false # any unknown role will always fail
end
if hasRole
@current_user = user
else
deny_access
end
end
end end
end
def authenticate_admin def authenticate_admin
authenticate('admin') authenticate('admin')
end end
def authenticate_finance def authenticate_finance
authenticate('finance') authenticate('finance')
end end
def authenticate_article_meta def authenticate_article_meta
authenticate('article_meta') authenticate('article_meta')
end end
def authenticate_suppliers def authenticate_suppliers
authenticate('suppliers') authenticate('suppliers')
end end
def authenticate_orders def authenticate_orders
authenticate('orders') authenticate('orders')
end end
# checks if the current_user is member of given group. # checks if the current_user is member of given group.
# if fails the user will redirected to startpage # if fails the user will redirected to startpage
def authenticate_membership_or_admin def authenticate_membership_or_admin
@group = Group.find(params[:id]) @group = Group.find(params[:id])
unless @group.member?(@current_user) or @current_user.role_admin? unless @group.member?(@current_user) or @current_user.role_admin?
flash[:error] = "Diese Aktion ist nur für Mitglieder der Gruppe erlaubt!" flash[:error] = "Diese Aktion ist nur für Mitglieder der Gruppe erlaubt!"
if request.xml_http_request? if request.xml_http_request?
render(:update) {|page| page.redirect_to root_path } render(:update) {|page| page.redirect_to root_path }
else else
redirect_to root_path
end
end
end
# Stores this controller instance as a thread local varibale to be accessible from outside ActionController/ActionView.
def store_controller
Thread.current[:application_controller] = self
end
# Sets the thread local variable that holds a reference to the current controller to nil.
def remove_controller
Thread.current[:application_controller] = nil
end
# Get supplier in nested resources
def find_supplier
@supplier = Supplier.find(params[:supplier_id]) if params[:supplier_id]
end
# Set config and database connection for each request
# It uses the subdomain to select the appropriate section in the config files
# Use this method as a before filter (first filter!) in ApplicationController
def select_foodcoop
if Foodsoft.config[:multi_coop_install]
if !params[:foodcoop].blank?
begin
# Set Config
Foodsoft.env = params[:foodcoop]
# Set database-connection
ActiveRecord::Base.establish_connection(Foodsoft.database)
rescue => error
flash[:error] = error.to_s
redirect_to root_path redirect_to root_path
end end
else
redirect_to root_path
end end
else
# Deactivate routing filter
RoutingFilter::Foodcoop.active = false
end end
end
# Stores this controller instance as a thread local varibale to be accessible from outside ActionController/ActionView.
def store_controller
Thread.current[:application_controller] = self
end
# Sets the thread local variable that holds a reference to the current controller to nil.
def remove_controller
Thread.current[:application_controller] = nil
end
# Get supplier in nested resources
def find_supplier
@supplier = Supplier.find(params[:supplier_id]) if params[:supplier_id]
end
# Set config and database connection for each request
# It uses the subdomain to select the appropriate section in the config files
# Use this method as a before filter (first filter!) in ApplicationController
def select_foodcoop
if Foodsoft.config[:multi_coop_install]
# Get subdomain
subdomain = request.subdomains.first
# Set Config
Foodsoft.env = subdomain
# Set database-connection
ActiveRecord::Base.establish_connection(Foodsoft.database(subdomain))
end
end
end end

View File

@ -1,6 +1,5 @@
class LoginController < ApplicationController class LoginController < ApplicationController
skip_before_filter :authenticate # no authentication since this is the login page skip_before_filter :authenticate # no authentication since this is the login page
filter_parameter_logging "password" # do not log "password" parameter
before_filter :validate_token, :only => [:password, :update_password] before_filter :validate_token, :only => [:password, :update_password]
verify :method => :post, :only => [:login, :reset_password, :new], :redirect_to => { :action => :index } verify :method => :post, :only => [:login, :reset_password, :new], :redirect_to => { :action => :index }

View File

@ -6,7 +6,7 @@ class TasksController < ApplicationController
@groups = Workgroup.all @groups = Workgroup.all
end end
def myTasks def user
@unaccepted_tasks = @current_user.unaccepted_tasks @unaccepted_tasks = @current_user.unaccepted_tasks
@accepted_tasks = @current_user.accepted_tasks @accepted_tasks = @current_user.accepted_tasks
end end
@ -78,7 +78,7 @@ class TasksController < ApplicationController
task.assignments.create(:user => current_user, :accepted => true) task.assignments.create(:user => current_user, :accepted => true)
end end
flash[:notice] = "Du hast die Aufgabe übernommen" flash[:notice] = "Du hast die Aufgabe übernommen"
redirect_to my_tasks_path redirect_to user_tasks_path
end end
# deletes assignment between current_user and given task # deletes assignment between current_user and given task

View File

@ -117,7 +117,7 @@ module ApplicationHelper
end end
def tab_is_active?(tab) def tab_is_active?(tab)
tab[:active].detect {|c| c == controller.controller_path } tab[:active].detect {|c| controller.controller_path.match(c) }
end end
def icon(name, options={}) def icon(name, options={})

View File

@ -7,9 +7,9 @@ module PagesHelper
def link_to_wikipage(page, text = nil) def link_to_wikipage(page, text = nil)
if text == nil if text == nil
link_to page.title, "/wiki/#{page.title}" link_to page.title, wiki_page_path(page.permalink)
else else
link_to text, "/wiki/#{page.title}" link_to text, wiki_page_path(page.permalink)
end end
end end

View File

@ -12,9 +12,9 @@ class Mailer < ActionMailer::Base
body :body => message.body, body :body => message.body,
:sender => message.sender.nick, :sender => message.sender.nick,
:recipients => recipient.nick, :recipients => recipient.nick,
:reply => "#{Foodsoft.config[:base_url]}/messages/reply/#{message.id}", :reply => url_for(:controller => "messages", :action => "reply", :id => message.id),
:link => "#{Foodsoft.config[:base_url]}/messages/show/#{message.id}", :link => url_for(:controller => "messages", :action => "show", :id => message.id),
:profile => "#{Foodsoft.config[:base_url]}/home/profile" :profile => url_for(:controller => "home", :action => "profile")
end end
# Sends an email with instructions on how to reset the password. # Sends an email with instructions on how to reset the password.
@ -23,7 +23,7 @@ class Mailer < ActionMailer::Base
prepare_system_message(user) prepare_system_message(user)
subject "[#{Foodsoft.config[:name]}] Neues Passwort für/ New password for #{user.nick}" subject "[#{Foodsoft.config[:name]}] Neues Passwort für/ New password for #{user.nick}"
body :user => user, body :user => user,
:link => "#{Foodsoft.config[:base_url]}/login/password/#{user.id}?token=#{user.reset_password_token}" :link => url_for(:controller => "login", :action => "password", :id => user.id, :token => user.reset_password_token)
end end
# Sends an invite email. # Sends an invite email.
@ -31,7 +31,7 @@ class Mailer < ActionMailer::Base
prepare_system_message(invite) prepare_system_message(invite)
subject "Einladung in die Foodcoop #{Foodsoft.config[:name]} - Invitation to the Foodcoop" subject "Einladung in die Foodcoop #{Foodsoft.config[:name]} - Invitation to the Foodcoop"
body :invite => invite, body :invite => invite,
:link => "#{Foodsoft.config[:base_url]}/login/invite/#{invite.token}" :link => url_for(:controller => "login", :action => "invite", :id => invite.token)
end end
# Notify user of upcoming task. # Notify user of upcoming task.
@ -71,7 +71,7 @@ class Mailer < ActionMailer::Base
prepare_system_message(user) prepare_system_message(user)
subject "[#{Foodsoft.config[:name]}] #{task.name} braucht noch Leute!" subject "[#{Foodsoft.config[:name]}] #{task.name} braucht noch Leute!"
body :task => task, :user => user, body :task => task, :user => user,
:task_url => File.join(Foodsoft.config[:base_url], "tasks/workgroup", task.workgroup_id.to_s) :task_url => url_for(:controller => "tasks", :action => "workgroup", :id => task.workgroup_id)
end end
protected protected

View File

@ -4,7 +4,7 @@
Foodcoop Foodcoop
%ul %ul
%li= link_to "Mitglieder", foodcoop_users_path %li= link_to "Mitglieder", foodcoop_users_path
%li= link_to "Meine Aufgaben", :controller => "home", :action => "tasks" %li= link_to "Meine Aufgaben", user_tasks_path
%li= link_to "Nachricht schreiben", :controller => "messages", :action => "new" %li= link_to "Nachricht schreiben", :controller => "messages", :action => "new"
// Orders // Orders

View File

@ -21,11 +21,11 @@
- unless @unaccepted_tasks.empty? - unless @unaccepted_tasks.empty?
%h3 Aufgaben übernehmen %h3 Aufgaben übernehmen
Du bis für Aufgaben verantwortlich. Du bis für Aufgaben verantwortlich.
= link_to "Aufgaben übernehmen/ablehnen", my_tasks_path = link_to "Aufgaben übernehmen/ablehnen", user_tasks_path
- unless @unassigned_tasks_number == 0 - unless @unassigned_tasks_number == 0
%h3 Offene Aufgaben %h3 Offene Aufgaben
= "Es gibt #{@unassigned_tasks_number} #{link_to 'offene Aufgabe(n)', :controller => 'tasks'}" = "Es gibt #{@unassigned_tasks_number} #{link_to 'offene Aufgabe(n)', :controller => 'tasks'}"
%p{:style => "clear:both"}= link_to "Meine Aufgaben", my_tasks_path %p{:style => "clear:both"}= link_to "Meine Aufgaben", user_tasks_path
- if @ordergroup - if @ordergroup
// Current orders // Current orders

View File

@ -3,56 +3,56 @@
tabs = [ tabs = [
{ :name => "Start", :url => root_path, :active => ["index", "home"], { :name => "Start", :url => root_path, :active => ["index", "home"],
:subnav => [ :subnav => [
{ :name => "Meine Aufgaben", :url => "/home/tasks" }, { :name => "Meine Aufgaben", :url => user_tasks_path },
{ :name => "Meine Bestellgruppe", :url => "/home/ordergroup", :access_denied? => (!u.ordergroup)}, { :name => "Meine Bestellgruppe", :url => my_ordergroup_path, :access_denied? => (!u.ordergroup)},
{ :name => "Mein Profil", :url => "/home/profile"} { :name => "Mein Profil", :url => my_profile_path}
] ]
}, },
{ :name => "Foodcoop", :url => "/tasks", { :name => "Foodcoop", :url => tasks_path,
:active => ["foodcoop", "tasks", "messages", "foodcoop/ordergroups", "foodcoop/workgroups", "foodcoop/users"], :active => ["foodcoop", "tasks", "messages", "foodcoop/ordergroups", "foodcoop/workgroups", "foodcoop/users"],
:subnav => [ :subnav => [
{ :name => "Mitglieder", :url => "/foodcoop/users"}, { :name => "Mitglieder", :url => foodcoop_users_path},
{ :name => "Abeitsgruppen", :url => "/foodcoop/workgroups"}, { :name => "Abeitsgruppen", :url => foodcoop_workgroups_path},
{ :name => "Bestellgruppen", :url => "/foodcoop/ordergroups"}, { :name => "Bestellgruppen", :url => foodcoop_ordergroups_path},
{ :name => "Nachrichten", :url => "/messages"}, { :name => "Nachrichten", :url => messages_path},
{ :name => "Aufgaben", :url => "/tasks"} { :name => "Aufgaben", :url => tasks_path}
] ]
}, },
{ :name => "Wiki", :url => "/wiki", :active => ["pages", "wiki"], { :name => "Wiki", :url => wiki_path, :active => ["pages", "wiki"],
:subnav => [ :subnav => [
{ :name => "Startseite", :url => "/wiki" }, { :name => "Startseite", :url => wiki_path },
{ :name => "Alle Seiten", :url => "/pages/all" } { :name => "Alle Seiten", :url => all_pages_path }
] ]
}, },
{ :name => "Bestellungen", :url => u.ordergroup ? "/ordering/" : "/orders", { :name => "Bestellungen", :url => u.ordergroup ? ordering_path : orders_path,
:active => ["orders", "ordering"], :active => ["orders", "ordering"],
:subnav => [ :subnav => [
{ :name => "Bestellen!", :url => "/ordering" }, { :name => "Bestellen!", :url => ordering_path },
{ :name => "Meine Bestellungen", :url => "/ordering/myOrders" }, { :name => "Meine Bestellungen", :url => my_orders_path },
{ :name => "Bestellverwaltung", :url => "/orders", :access_denied? => (!u.role_orders?) } { :name => "Bestellverwaltung", :url => orders_path, :access_denied? => (!u.role_orders?) }
] ]
}, },
{ :name => "Artikel", :url => "/suppliers", { :name => "Artikel", :url => suppliers_path,
:active => ["articles", "suppliers", "deliveries", "article_categories", "stockit", "stock_takings"], :active => ["articles", "suppliers", "deliveries", "article_categories", "stockit", "stock_takings"],
:access_denied? => (!u.role_article_meta? && !u.role_suppliers?), :access_denied? => (!u.role_article_meta? && !u.role_suppliers?),
:subnav => [ :subnav => [
{ :name => "Artikel", :url => supplier_articles_path(Supplier.first) }, { :name => "Artikel", :url => supplier_articles_path(Supplier.first) },
{ :name => "Lager", :url => "/stockit" }, { :name => "Lager", :url => stock_articles_path },
{ :name => "Lieferantinnen", :url => suppliers_path, :access_denied? => (!u.role_suppliers?) }, { :name => "Lieferantinnen", :url => suppliers_path, :access_denied? => (!u.role_suppliers?) },
{ :name => "Kategorien", :url => "/article_categories"} { :name => "Kategorien", :url => article_categories_path }
] ]
}, },
{ :name => "Finanzen", :url => "/finance", { :name => "Finanzen", :url => finance_root_path,
:active => ["finance/invoices", "finance/transactions", "finance/balancing"], :active => ["finance/invoices", "finance/transactions", "finance/balancing"],
:access_denied? => (!u.role_finance?), :access_denied? => (!u.role_finance?),
:subnav => [ :subnav => [
{ :name => "Konten verwalten", :url => "/finance/transactions" }, { :name => "Konten verwalten", :url => finance_transactions_path },
{ :name => "Bestellungen abrechnen", :url => "/finance/balancing/list" }, { :name => "Bestellungen abrechnen", :url => finance_balancing_path },
{ :name => "Rechnungen", :url => finance_invoices_path } { :name => "Rechnungen", :url => finance_invoices_path }
] ]
}, },
{ :name => "Administration", :url => "/admin", { :name => "Administration", :url => admin_root_path,
:active => ["admin", "admin/users", "admin/ordergroups", "admin/workgroups"], :active => ["admin/"],
:access_denied? => (!u.role_admin?), :access_denied? => (!u.role_admin?),
:subnav => [ :subnav => [
{ :name => "Benutzerinnen", :url => admin_users_path }, { :name => "Benutzerinnen", :url => admin_users_path },
@ -77,4 +77,4 @@
</li> </li>
<% end -%> <% end -%>
<% end -%> <% end -%>
</ul> </ul>

View File

@ -15,7 +15,7 @@
#header #header
#logo #logo
%a{:href => "/"} - link_to root_path do
<span>food</span>soft <span>food</span>soft
%span{:style => "color:white; font-size:45%; letter-spacing: -1px;"}= Foodsoft.config[:name] %span{:style => "color:white; font-size:45%; letter-spacing: -1px;"}= Foodsoft.config[:name]
#nav= render :partial => 'layouts/main_tabnav' #nav= render :partial => 'layouts/main_tabnav'

View File

@ -1,6 +1,6 @@
<%= yield %> <%= yield %>
-- --
FoodSoft: <%= @foodsoftUrl %> FoodSoft: <%= url_for(:controller => "home", :action => "index", :only_path => false) %>
Foodcoop-Homepage: <%= Foodsoft.config[:base_url] %> Foodcoop-Homepage: <%= Foodsoft.config[:homepage] %>
Hilfe/Help: <%= Foodsoft.config[:help_url] %> Hilfe/Help: <%= Foodsoft.config[:help_url] %>

View File

@ -15,4 +15,4 @@
= text_field 'login', 'email' = text_field 'login', 'email'
= submit_tag 'Neues Passwort anfordern' = submit_tag 'Neues Passwort anfordern'
| |
= link_to 'Abbrechen', :action => 'login' = link_to 'Abbrechen', login_path

View File

@ -13,4 +13,4 @@
= form.password_field :password_confirmation = form.password_field :password_confirmation
= form.submit 'Speichern' = form.submit 'Speichern'
| |
= link_to 'Abbrechen', :action => 'login' = link_to 'Abbrechen', login_path

View File

@ -7,5 +7,5 @@ Sofern Du Dich noch nicht für diese Aufgabe eingetragen hast ist das jetzt die
<%= @task_url %> <%= @task_url %>
-- --
Deine Aufgaben: <%= Foodsoft.config[:base_url] %>/home/tasks Deine Aufgaben: <%= url_for(:controller => "home", :actions => "user") %>

View File

@ -9,6 +9,6 @@ Für Euch wurden die folgenden Artikel bestellt:
<% end -%> <% end -%>
Gesamtpreis: <%= @group_order.price %> Gesamtpreis: <%= @group_order.price %>
Bestellung online einsehen: <%= "#{Foodsoft.config[:base_url]}/ordering/my_order_result/#{@order.id}" %> Bestellung online einsehen: <%= url_for(:controller => "ordering", :action => "my_order_result", :id => @order.id) %>
Viele Grüße von <%= Foodsoft.config[:name] %> Viele Grüße von <%= Foodsoft.config[:name] %>

View File

@ -11,6 +11,6 @@ Aufgaben für die nächste Woche:
<% end -%> <% end -%>
<% end -%> <% end -%>
Meine Aufgaben: <%= Foodsoft.config[:base_url] %>/home/tasks Meine Aufgaben: <%= url_for(:controller => "home", :actions => "user") %>
Viele Grüße von <%= Foodsoft.config[:name] %> Viele Grüße von <%= Foodsoft.config[:name] %>

View File

@ -8,7 +8,7 @@
%li %li
Seiten Seiten
%ul %ul
%li= link_to "Meine Aufgaben", my_tasks_path %li= link_to "Meine Aufgaben", user_tasks_path
%li= link_to "Alle Aufgaben", :action => "index" %li= link_to "Alle Aufgaben", :action => "index"
%li= link_to "Erledigt Aufgaben (Archiv)", :action => "archive" %li= link_to "Erledigt Aufgaben (Archiv)", :action => "archive"

View File

@ -5,6 +5,12 @@ development: &defaults
# Don't forget to setup databases for each foodcoop. See also MULTI_COOP_INSTALL # Don't forget to setup databases for each foodcoop. See also MULTI_COOP_INSTALL
multi_coop_install: false multi_coop_install: false
# http config for this host
# Required for action mailer
protocol: http
host: localhost
port: 3000
# name of this foodcoop # name of this foodcoop
name: FC Test name: FC Test
# foodcoop contact information (used for FAX messages) # foodcoop contact information (used for FAX messages)
@ -16,9 +22,6 @@ development: &defaults
email: foodsoft@myfoodcoop.org email: foodsoft@myfoodcoop.org
phone: "030 323 23249" phone: "030 323 23249"
# base URL for this installation
base_url: http://foodsoft.fctest.de
# Homepage # Homepage
homepage: http://www.fctest.de homepage: http://www.fctest.de

View File

@ -59,6 +59,7 @@ Rails::Initializer.run do |config|
config.gem "fastercsv" config.gem "fastercsv"
config.gem "prawn", :version => '<=0.6.3' config.gem "prawn", :version => '<=0.6.3'
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,33 +1,33 @@
# Loads and returns config and databases for selected foodcoop. # Loads and returns config and databases for selected foodcoop.
# TODO: When to use class or module. It seems this could also be a Foodsoft-class?
module Foodsoft module Foodsoft
@@configs = YAML.load(File.read(RAILS_ROOT + "/config/app_config.yml")) mattr_accessor :env, :config, :database
@@databases = YAML.load(File.read(RAILS_ROOT + "/config/database.yml")) CONFIGS = YAML.load(File.read(RAILS_ROOT + "/config/app_config.yml"))
@@env = RAILS_ENV DATABASES = YAML.load(File.read(RAILS_ROOT + "/config/database.yml"))
def env=(env) class << self
@@env = env def env=(env)
raise "No config or database for this environment (#{env}) available!" if CONFIGS[env].nil? or DATABASES[env].nil?
@@config = CONFIGS[env].symbolize_keys
@@database = DATABASES[env].symbolize_keys
@@env = env
end
end end
def env
@@env
end
def config(rails_env = @@env)
raise "No config for this environment (or subdomain) available!" if @@configs[rails_env].nil?
@@configs[rails_env].symbolize_keys
end
def database(rails_env = @@env)
raise "No database for this environment (or subdomain) available!" if @@databases[rails_env].nil?
@@databases[rails_env].symbolize_keys
end
extend self
end end
# Initial load the default config and database from rails environment
Foodsoft.env = RAILS_ENV
# Set action mailer default host for url generating
url_options = {
:host => Foodsoft.config[:host],
:protocol => Foodsoft.config[:protocol]
}
url_options.merge!({:port => Foodsoft.config[:port]}) if Foodsoft.config[:port]
ActionMailer::Base.default_url_options = url_options
# Configuration of the exception_notification plugin # Configuration of the exception_notification plugin
# Mailadresses are set in config/foodsoft.yaml # Mailadresses are set in config/foodsoft.yaml
ExceptionNotifier.exception_recipients = Foodsoft.config[:notification]['error_recipients'] ExceptionNotifier.exception_recipients = Foodsoft.config[:notification]['error_recipients']
ExceptionNotifier.sender_address = Foodsoft.config[:notification]['sender_address'] ExceptionNotifier.sender_address = Foodsoft.config[:notification]['sender_address']
ExceptionNotifier.email_prefix = Foodsoft.config[:notification]['email_prefix'] ExceptionNotifier.email_prefix = Foodsoft.config[:notification]['email_prefix']

View File

@ -1,20 +1,36 @@
ActionController::Routing::Routes.draw do |map| ActionController::Routing::Routes.draw do |map|
# Use routing filter to select foodcoop config and datbase
map.filter 'foodcoop', :file => File.join(RAILS_ROOT, "lib", "foodcoop_filter")
# Root path
map.root :controller => 'home', :action => 'index'
# User specific
map.login "/login", :controller => 'login', :action => 'index'
map.logout '/logout', :controller => 'login', :action => 'logout'
map.my_profile '/home/profile', :controller => 'home', :action => 'profile'
map.my_ordergroup '/home/ordergroup', :controller => 'home', :action => 'ordergroup'
# Wiki
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'
map.logout '/logout', :controller => 'login', :action => 'logout' # Orders, ordering
map.my_profile '/home/profile', :controller => 'home', :action => 'profile'
map.my_ordergroup '/home/ordergroup', :controller => 'home', :action => 'ordergroup'
map.my_tasks '/home/tasks', :controller => 'tasks', :action => 'myTasks'
map.resources :orders, :member => { :finish => :post, :add_comment => :post } map.resources :orders, :member => { :finish => :post, :add_comment => :post }
map.with_options :controller => "ordering" do |ordering|
ordering.ordering "/ordering", :action => "index"
ordering.my_orders "/ordering/myOrders", :action => "myOrders"
end
# Foodcoop orga
map.resources :invites, :only => [:new, :create]
map.resources :tasks,
:collection => {:user => :get}
map.resources :messages, :only => [:index, :show, :new, :create], map.resources :messages, :only => [:index, :show, :new, :create],
:member => { :reply => :get, :user => :get, :group => :get } :member => { :reply => :get, :user => :get, :group => :get }
map.resources :invites, :only => [:new, :create]
map.namespace :foodcoop do |foodcoop| map.namespace :foodcoop do |foodcoop|
foodcoop.root :controller => "users", :action => "index" foodcoop.root :controller => "users", :action => "index"
foodcoop.resources :users, :only => [:index] foodcoop.resources :users, :only => [:index]
@ -23,23 +39,13 @@ ActionController::Routing::Routes.draw do |map|
:member => {:memberships => :get} :member => {:memberships => :get}
end end
map.namespace :admin do |admin| # Article management
admin.resources :users
admin.resources :workgroups, :member => { :memberships => :get }
admin.resources :ordergroups, :member => { :memberships => :get }
end
map.namespace :finance do |finance|
finance.root :controller => 'balancing'
finance.resources :invoices
end
map.resources :stock_takings, map.resources :stock_takings,
:collection => {:fill_new_stock_article_form => :get, :add_stock_article => :post} :collection => {:fill_new_stock_article_form => :get, :add_stock_article => :post}
map.resources :stock_articles, map.resources :stock_articles,
:controller => 'stockit', :as => 'stockit', :controller => 'stockit', :as => 'stockit',
:collection => {:auto_complete_for_article_name => :get, :fill_new_stock_article_form => :get} :collection => {:auto_complete_for_article_name => :get, :fill_new_stock_article_form => :get}
map.resources :suppliers, map.resources :suppliers,
:collection => { :shared_suppliers => :get } do |suppliers| :collection => { :shared_suppliers => :get } do |suppliers|
suppliers.resources :deliveries, suppliers.resources :deliveries,
@ -52,47 +58,21 @@ ActionController::Routing::Routes.draw do |map|
end end
map.resources :article_categories map.resources :article_categories
map.root :controller => 'home', :action => 'index' # Finance
map.namespace :finance do |finance|
# The priority is based upon order of creation: first created -> highest priority. finance.root :controller => 'balancing'
finance.balancing "balancing/list", :controller => 'balancing', :action => 'list'
finance.resources :invoices
finance.resources :transactions
end
# Sample of regular route: # Administration
# map.connect 'products/:id', :controller => 'catalog', :action => 'view' map.namespace :admin do |admin|
# Keep in mind you can assign values other than :controller and :action admin.root :controller => "base", :action => "index"
admin.resources :users
# Sample of named route: admin.resources :workgroups, :member => { :memberships => :get }
# map.purchase 'products/:id/purchase', :controller => 'catalog', :action => 'purchase' admin.resources :ordergroups, :member => { :memberships => :get }
# This route can be invoked with purchase_url(:id => product.id) end
# Sample resource route (maps HTTP verbs to controller actions automatically):
# map.resources :products
# Sample resource route with options:
# map.resources :products, :member => { :short => :get, :toggle => :post }, :collection => { :sold => :get }
# Sample resource route with sub-resources:
# map.resources :products, :has_many => [ :comments, :sales ], :has_one => :seller
# Sample resource route with more complex sub-resources
# map.resources :products do |products|
# products.resources :comments
# products.resources :sales, :collection => { :recent => :get }
# end
# Sample resource route within a namespace:
# map.namespace :admin do |admin|
# # Directs /admin/products/* to Admin::ProductsController (app/controllers/admin/products_controller.rb)
# admin.resources :products
# end
# You can have the root of your site routed with map.root -- just remember to delete public/index.html.
# map.root :controller => "welcome"
# See how all your routes lay out with "rake routes"
# Install the default routes as the lowest priority.
# Note: These default routes make all actions in every controller accessible via GET requests. You should
# consider removing the them or commenting them out if you're using named routes and resources.
# Install the default route as the lowest priority. # Install the default route as the lowest priority.
map.connect ':controller/:action/:id' map.connect ':controller/:action/:id'

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

View File

@ -1,23 +1,21 @@
class Wikilink < WikiCloth::WikiLinkHandler class Wikilink < WikiCloth::WikiLinkHandler
include ActionController::UrlWriter # To use named routes
def url_for(page, parent = nil)
if parent
"/pages/new?title=#{page}&parent=#{parent}"
else
"/wiki/#{page}"
end
end
def link_attributes_for(page) def link_attributes_for(page)
permalink = Page.permalink(page) permalink = Page.permalink(page)
url_options = {:host => Foodsoft.config[:host], :protocol => Foodsoft.config[:protocol]}
url_options.merge!({:port => Foodsoft.config[:port]}) if Foodsoft.config[:port]
if Page.exists?(:permalink => permalink) if Page.exists?(:permalink => permalink)
{ :href => url_for(permalink) } { :href => url_for(url_options.merge({:controller => "pages", :action => "show",
:permalink => permalink, :use_route => :wiki_page})) }
else else
{ :href => url_for(page, params[:referer]), :class => "new_wiki_link"} { :href => url_for(url_options.merge({:controller => "pages", :action => "new",
:title => page, :parent => params[:referer]})), :class => "new_wiki_link"}
end end
end end
def section_link(section) def section_link(section)
"" ""
end end
end end