From ec2e761e7f43890ea37e0e3adbf832d7ae83a0a6 Mon Sep 17 00:00:00 2001 From: benni Date: Fri, 24 Aug 2012 19:52:38 +0200 Subject: [PATCH] Fixed multi coop routing and changed config accessors. --- .gitignore | 1 - MULTI_COOP_INSTALL | 5 ++- app/controllers/application_controller.rb | 32 ++++++++--------- app/controllers/orders_controller.rb | 6 ++-- app/controllers/sessions_controller.rb | 1 + app/mailers/mailer.rb | 26 +++++++------- app/models/article.rb | 6 ++-- app/models/article_price.rb | 2 +- app/models/group_order_article.rb | 2 +- app/models/shared_article.rb | 2 +- app/models/shared_supplier.rb | 2 +- app/views/group_orders/_form.html.haml | 2 +- app/views/group_orders/order.html.haml | 2 +- app/views/layouts/application.haml | 2 +- app/views/layouts/email.text.erb | 4 +-- app/views/layouts/login.haml | 2 +- app/views/login/invite.haml | 4 +-- app/views/mailer/negative_balance.text.erb | 2 +- app/views/mailer/order_result.text.erb | 2 +- app/views/mailer/upcoming_tasks.text.erb | 2 +- app/views/messages/new.haml | 6 ++-- app/views/orders/faxPdf.pdf.prawn | 4 +-- app/views/shared/_loginInfo.haml | 4 +-- config/app_config.yml.SAMPLE | 8 ++++- config/initializers/load_app_config.rb | 33 ++++++----------- config/routes.rb | 4 +-- lib/foodsoft_config.rb | 42 ++++++++++++++++++++++ lib/wikilink.rb | 4 +-- 28 files changed, 124 insertions(+), 88 deletions(-) create mode 100644 lib/foodsoft_config.rb diff --git a/.gitignore b/.gitignore index 402363fa..20caa636 100644 --- a/.gitignore +++ b/.gitignore @@ -5,7 +5,6 @@ db/*.sqlite3 nbproject/ config/environments/development.rb capfile -config/environments/fcschinke09.rb *.swp public/**/*_cached.* .idea diff --git a/MULTI_COOP_INSTALL b/MULTI_COOP_INSTALL index 602306c4..843cffa3 100644 --- a/MULTI_COOP_INSTALL +++ b/MULTI_COOP_INSTALL @@ -1 +1,4 @@ -TODO.. \ No newline at end of file +MULTI_COOP_INSTALL +------------------ + +TODO ... \ No newline at end of file diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 0f3f9a72..60d85d6e 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -5,8 +5,6 @@ class ApplicationController < ActionController::Base before_filter :select_foodcoop, :authenticate, :store_controller, :items_per_page, :set_redirect_to after_filter :remove_controller - helper_method :current_user - # Returns the controller handling the current request. def self.current Thread.current[:application_controller] @@ -18,10 +16,11 @@ class ApplicationController < ActionController::Base # check if there is a valid session and return the logged-in user (its object) if session[:user_id] and params[:foodcoop] # for shared-host installations. check if the cookie-subdomain fits to request. - @current_user ||= User.find_by_id(session[:user_id]) if params[:foodcoop] == Foodsoft.env + @current_user ||= User.find_by_id(session[:user_id]) if session[:scope] == FoodsoftConfig.scope end end - + helper_method :current_user + def deny_access self.return_to = request.request_uri redirect_to login_url, :alert => 'Access denied!' @@ -104,28 +103,22 @@ class ApplicationController < ActionController::Base # 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? + if FoodsoftConfig[:multi_coop_install] + if params[:foodcoop].present? begin - # Set Config - Foodsoft.env = params[:foodcoop] - # Set database-connection - ActiveRecord::Base.establish_connection(Foodsoft.database) + # Set Config and database connection + FoodsoftConfig.select_foodcoop params[:foodcoop] rescue => error - flash[:error] = error.to_s - redirect_to root_path + redirect_to root_url, alert: error.message end else - redirect_to root_path + redirect_to root_url end - else - # Deactivate routing filter - RoutingFilter::Foodcoop.active = false end end def items_per_page - 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 @per_page = params[:per_page].to_i else @per_page = 20 @@ -143,4 +136,9 @@ class ApplicationController < ActionController::Base end default end + + # Always stay in foodcoop url scope + def default_url_options(options = {}) + {foodcoop: FoodsoftConfig.scope} + end end diff --git a/app/controllers/orders_controller.rb b/app/controllers/orders_controller.rb index 205d8dde..b177e4fb 100644 --- a/app/controllers/orders_controller.rb +++ b/app/controllers/orders_controller.rb @@ -127,13 +127,13 @@ class OrdersController < ApplicationController def text_fax_template order = Order.find(params[:id]) supplier = order.supplier - contact = Foodsoft.config[:contact].symbolize_keys - text = "Bestellung für" + " #{Foodsoft.config[:name]}" + contact = FoodsoftConfig[:contact].symbolize_keys + text = "Bestellung für" + " #{FoodsoftConfig[:name]}" text += "\n" + "Kundennummer" + ": #{supplier.customer_number}" unless supplier.customer_number.blank? text += "\n" + "Liefertag" + ": " text += "\n\n#{supplier.name}\n#{supplier.address}\nFAX: #{supplier.fax}\n\n" text += "****** " + "Versandadresse" + "\n\n" - text += "#{Foodsoft.config[:name]}\n#{contact[:street]}\n#{contact[:zip_code]} #{contact[:city]}\n\n" + text += "#{FoodsoftConfig[:name]}\n#{contact[:street]}\n#{contact[:zip_code]} #{contact[:city]}\n\n" text += "****** " + "Artikel" + "\n\n" text += "Nummer" + " " + "Menge" + " " + "Name" + "\n" # now display all ordered articles diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 00ac595b..6cb238d1 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -10,6 +10,7 @@ class SessionsController < ApplicationController user = User.authenticate(params[:nick], params[:password]) if user session[:user_id] = user.id + session[:scope] = FoodsoftConfig.scope # Save scope in session to not allow switching between foodcoops with one account redirect_to session['return_to'] || root_url, :notice => "Logged in!" else flash.now.alert = "Invalid email or password" diff --git a/app/mailers/mailer.rb b/app/mailers/mailer.rb index 820fad2c..1bae467b 100644 --- a/app/mailers/mailer.rb +++ b/app/mailers/mailer.rb @@ -4,15 +4,15 @@ class Mailer < ActionMailer::Base layout 'email' # Use views/layouts/email.txt.erb - default from: "FoodSoft <#{Foodsoft.config[:email_sender]}>", - sender: Foodsoft.config[:email_sender], - errors_to: Foodsoft.config[:email_sender] + default from: "FoodSoft <#{FoodsoftConfig[:email_sender]}>", + sender: FoodsoftConfig[:email_sender], + errors_to: FoodsoftConfig[:email_sender] # Sends an email copy of the given internal foodsoft message. def foodsoft_message(message, recipient) @message = message - mail subject: "[#{Foodsoft.config[:name]}] " + message.subject, + mail subject: "[#{FoodsoftConfig[:name]}] " + message.subject, to: recipient.email, from: "#{message.sender.nick} <#{message.sender.email}>" end @@ -24,7 +24,7 @@ class Mailer < ActionMailer::Base @link = url_for(:controller => "login", :action => "password", :id => user.id, :token => user.reset_password_token) mail :to => user.email, - :subject => "[#{Foodsoft.config[:name]}] Neues Passwort für/ New password for #{user.nick}" + :subject => "[#{FoodsoftConfig[:name]}] Neues Passwort für/ New password for #{user.nick}" end # Sends an invite email. @@ -33,7 +33,7 @@ class Mailer < ActionMailer::Base @link = url_for(:controller => "login", :action => "invite", :id => invite.token) mail :to => invite.email, - :subject => "Einladung in die Foodcoop #{Foodsoft.config[:name]} - Invitation to the Foodcoop" + :subject => "Einladung in die Foodcoop #{FoodsoftConfig[:name]} - Invitation to the Foodcoop" end # Notify user of upcoming task. @@ -42,7 +42,7 @@ class Mailer < ActionMailer::Base @task = task mail :to => user.email, - :subject => "[#{Foodsoft.config[:name]}] Aufgaben werden fällig!" + :subject => "[#{FoodsoftConfig[:name]}] Aufgaben werden fällig!" end # Sends order result for specific Ordergroup @@ -51,7 +51,7 @@ class Mailer < ActionMailer::Base @group_order = group_order mail :to => user.email, - :subject => "[#{Foodsoft.config[:name]}] Bestellung beendet: #{group_order.order.name}" + :subject => "[#{FoodsoftConfig[:name]}] Bestellung beendet: #{group_order.order.name}" end # Notify user if account balance is less than zero @@ -60,17 +60,17 @@ class Mailer < ActionMailer::Base @transaction = transaction mail :to => user.email, - :subject => "[#{Foodsoft.config[:name]}] Gruppenkonto im Minus" + :subject => "[#{FoodsoftConfig[:name]}] Gruppenkonto im Minus" end def feedback(user, feedback) @user = user @feedback = feedback - mail :to => Foodsoft.config[:notification]["error_recipients"], + mail :to => FoodsoftConfig[:notification]["error_recipients"], :from => "#{user.nick} <#{user.email}>", - :sender => Foodsoft.config[:notification]["sender_address"], - :errors_to => Foodsoft.config[:notification]["sender_address"], + :sender => FoodsoftConfig[:notification]["sender_address"], + :errors_to => FoodsoftConfig[:notification]["sender_address"], :subject => "[Foodsoft] Feeback von #{user.email}" end @@ -79,7 +79,7 @@ class Mailer < ActionMailer::Base @user = user mail :to => user.email, - :subject => "[#{Foodsoft.config[:name]}] \"#{task.name}\" braucht noch Leute!" + :subject => "[#{FoodsoftConfig[:name]}] \"#{task.name}\" braucht noch Leute!" end end diff --git a/app/models/article.rb b/app/models/article.rb index 3a21b1a1..3c961058 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -33,7 +33,7 @@ class Article < ActiveRecord::Base # The price for the foodcoop-member. def fc_price - (gross_price * (Foodsoft.config[:price_markup] / 100 + 1)).round(2) + (gross_price * (FoodsoftConfig[:price_markup] / 100 + 1)).round(2) end # Returns true if article has been updated at least 2 days ago @@ -120,8 +120,8 @@ class Article < ActiveRecord::Base false end else # get factors for fc and supplier - fc_unit_factor = Foodsoft.config[:units][self.unit] - supplier_unit_factor = Foodsoft.config[:units][self.shared_article.unit] + fc_unit_factor = FoodsoftConfig[:units][self.unit] + supplier_unit_factor = FoodsoftConfig[:units][self.shared_article.unit] if fc_unit_factor and supplier_unit_factor convertion_factor = fc_unit_factor / supplier_unit_factor new_price = BigDecimal((convertion_factor * shared_article.price).to_s).round(2) diff --git a/app/models/article_price.rb b/app/models/article_price.rb index 3a42104a..1769c632 100644 --- a/app/models/article_price.rb +++ b/app/models/article_price.rb @@ -27,7 +27,7 @@ class ArticlePrice < ActiveRecord::Base # The price for the foodcoop-member. def fc_price - (gross_price * (Foodsoft.config[:price_markup] / 100 + 1)).round(2) + (gross_price * (FoodsoftConfig[:price_markup] / 100 + 1)).round(2) end end diff --git a/app/models/group_order_article.rb b/app/models/group_order_article.rb index 36c7ca51..2a3c2823 100644 --- a/app/models/group_order_article.rb +++ b/app/models/group_order_article.rb @@ -168,7 +168,7 @@ class GroupOrderArticle < ActiveRecord::Base # will be the value depending of the article results. def total_price unless order_article.order.finished? - if Foodsoft.config[:tolerance_is_costly] + if FoodsoftConfig[:tolerance_is_costly] order_article.article.fc_price * (quantity + tolerance) else order_article.article.fc_price * quantity diff --git a/app/models/shared_article.rb b/app/models/shared_article.rb index 58562357..c75ba4e9 100644 --- a/app/models/shared_article.rb +++ b/app/models/shared_article.rb @@ -1,7 +1,7 @@ class SharedArticle < ActiveRecord::Base # connect to database from sharedLists-Application - SharedArticle.establish_connection(Foodsoft.config[:shared_lists]) + SharedArticle.establish_connection(FoodsoftConfig[:shared_lists]) # set correct table_name in external DB set_table_name :articles diff --git a/app/models/shared_supplier.rb b/app/models/shared_supplier.rb index 5038bce5..1620ae31 100644 --- a/app/models/shared_supplier.rb +++ b/app/models/shared_supplier.rb @@ -1,7 +1,7 @@ class SharedSupplier < ActiveRecord::Base # connect to database from sharedLists-Application - SharedSupplier.establish_connection(Foodsoft.config[:shared_lists]) + SharedSupplier.establish_connection(FoodsoftConfig[:shared_lists]) # set correct table_name in external DB set_table_name :suppliers diff --git a/app/views/group_orders/_form.html.haml b/app/views/group_orders/_form.html.haml index eb7aca4b..9c3204bc 100644 --- a/app/views/group_orders/_form.html.haml +++ b/app/views/group_orders/_form.html.haml @@ -4,7 +4,7 @@ #{data_to_js(@ordering_data)} setGroupBalance(#{@ordering_data[:available_funds]}); setDecimalSeparator(","); - setToleranceBehaviour(#{Foodsoft.config[:tolerance_is_costly]}); + setToleranceBehaviour(#{FoodsoftConfig[:tolerance_is_costly]}); setStockit(#{@order.stockit?}); }); diff --git a/app/views/group_orders/order.html.haml b/app/views/group_orders/order.html.haml index 96182982..cc0b89b6 100644 --- a/app/views/group_orders/order.html.haml +++ b/app/views/group_orders/order.html.haml @@ -29,7 +29,7 @@ %b= h category %td{:colspan => "9"} - order_articles.each do |order_article| - - if Foodsoft.config[:tolerance_is_costly] + - if FoodsoftConfig[:tolerance_is_costly] - article_total = @price[i] * (@tolerance[i] + @quantity[i]) - else - article_total = @price[i] * @quantity[i] diff --git a/app/views/layouts/application.haml b/app/views/layouts/application.haml index 3c0ed10e..11b6c9eb 100644 --- a/app/views/layouts/application.haml +++ b/app/views/layouts/application.haml @@ -20,7 +20,7 @@ #logo = link_to root_path do foodsoft - %span{:style => "color:white; font-size:45%; letter-spacing: -1px;"}= Foodsoft.config[:name] + %span{:style => "color:white; font-size:45%; letter-spacing: -1px;"}= FoodsoftConfig[:name] #nav= render :partial => 'layouts/main_tabnav' #main diff --git a/app/views/layouts/email.text.erb b/app/views/layouts/email.text.erb index fdc39233..6c02affb 100644 --- a/app/views/layouts/email.text.erb +++ b/app/views/layouts/email.text.erb @@ -2,5 +2,5 @@ -- FoodSoft: <%= root_url %> -Foodcoop-Homepage: <%= Foodsoft.config[:homepage] %> -Hilfe/Help: <%= Foodsoft.config[:help_url] %> +Foodcoop-Homepage: <%= FoodsoftConfig[:homepage] %> +Hilfe/Help: <%= FoodsoftConfig[:help_url] %> diff --git a/app/views/layouts/login.haml b/app/views/layouts/login.haml index 6580ab43..3f922dce 100644 --- a/app/views/layouts/login.haml +++ b/app/views/layouts/login.haml @@ -15,4 +15,4 @@ = yield #meta Foodcoop - = link_to_if Foodsoft.config[:homepage], Foodsoft.config[:name], Foodsoft.config[:homepage] + = link_to_if FoodsoftConfig[:homepage], FoodsoftConfig[:name], FoodsoftConfig[:homepage] diff --git a/app/views/login/invite.haml b/app/views/login/invite.haml index bc36d284..77c37888 100644 --- a/app/views/login/invite.haml +++ b/app/views/login/invite.haml @@ -1,9 +1,9 @@ -- title "Einladung in die #{Foodsoft.config[:name]}" +- title "Einladung in die #{FoodsoftConfig[:name]}" %p Du bist eingeladen worden als Mitglied der Gruppe %b= @invite.group.name in der Foodcoop - = Foodsoft.config[:name] + = FoodsoftConfig[:name] mitzumachen. %p Wenn Du mitmachen möchtest, dann fülle bitte dieses Formular aus. diff --git a/app/views/mailer/negative_balance.text.erb b/app/views/mailer/negative_balance.text.erb index f44aea6e..15e5e39b 100644 --- a/app/views/mailer/negative_balance.text.erb +++ b/app/views/mailer/negative_balance.text.erb @@ -6,4 +6,4 @@ Es wurden <%= @transaction.amount %> für "<%= @transaction.note %>" abgebucht, Bitte zahlt so bald wie möglich wieder Geld ein, um das Gruppenkonto auszugleichen. -Viele Grüße von <%= Foodsoft.config[:name] %> \ No newline at end of file +Viele Grüße von <%= FoodsoftConfig[:name] %> \ No newline at end of file diff --git a/app/views/mailer/order_result.text.erb b/app/views/mailer/order_result.text.erb index ce03bcea..97e9aaf4 100644 --- a/app/views/mailer/order_result.text.erb +++ b/app/views/mailer/order_result.text.erb @@ -11,4 +11,4 @@ Gesamtpreis: <%= @group_order.price %> Bestellung online einsehen: <%= group_order_url(@group_order) %> -Viele Grüße von <%= Foodsoft.config[:name] %> \ No newline at end of file +Viele Grüße von <%= FoodsoftConfig[:name] %> \ No newline at end of file diff --git a/app/views/mailer/upcoming_tasks.text.erb b/app/views/mailer/upcoming_tasks.text.erb index b2349a25..3f41a8d0 100644 --- a/app/views/mailer/upcoming_tasks.text.erb +++ b/app/views/mailer/upcoming_tasks.text.erb @@ -12,4 +12,4 @@ Aufgaben für die nächste Woche: Meine Aufgaben: <%= user_tasks_url %> -Viele Grüße von <%= Foodsoft.config[:name] %> \ No newline at end of file +Viele Grüße von <%= FoodsoftConfig[:name] %> \ No newline at end of file diff --git a/app/views/messages/new.haml b/app/views/messages/new.haml index 3b1713ab..2a10fdb6 100644 --- a/app/views/messages/new.haml +++ b/app/views/messages/new.haml @@ -21,18 +21,18 @@ - title "Neue Nachricht" = simple_form_for @message do |f| - - if Foodsoft.config[:mailing_list].blank? + - if FoodsoftConfig[:mailing_list].blank? = f.input :sent_to_all, :as => :boolean - else %b Nachrichten an alle verschickst Du bitte über den Verteiler: - = mail_to Foodsoft.config[:mailing_list] + = mail_to FoodsoftConfig[:mailing_list] %br/ %small{:style => "color:grey"} Eventuell musst Du Dich dem Verteiler erst bekannt machen. %br/ z.b. mit einer Mail an - = mail_to Foodsoft.config[:mailing_list_subscribe] + = mail_to FoodsoftConfig[:mailing_list_subscribe] #recipients = f.input :recipient_tokens, :input_html => { 'data-pre' => User.find_all_by_id(@message.recipients_ids).map { |u| u.token_attributes }.to_json } diff --git a/app/views/orders/faxPdf.pdf.prawn b/app/views/orders/faxPdf.pdf.prawn index 5294b0b6..293e08d6 100644 --- a/app/views/orders/faxPdf.pdf.prawn +++ b/app/views/orders/faxPdf.pdf.prawn @@ -1,5 +1,5 @@ # Get ActiveRecord objects -contact = Foodsoft.config[:contact].symbolize_keys +contact = FoodsoftConfig[:contact].symbolize_keys # Define header and footer #pdf.header [pdf.margin_box.left,pdf.margin_box.top+30] do @@ -12,7 +12,7 @@ end # From paragraph pdf.bounding_box [pdf.margin_box.right-200,pdf.margin_box.top], :width => 200 do - pdf.text Foodsoft.config[:name], :align => :right + pdf.text FoodsoftConfig[:name], :align => :right pdf.move_down 5 pdf.text contact[:street], :align => :right pdf.move_down 5 diff --git a/app/views/shared/_loginInfo.haml b/app/views/shared/_loginInfo.haml index fa2e60cb..cf421d81 100644 --- a/app/views/shared/_loginInfo.haml +++ b/app/views/shared/_loginInfo.haml @@ -2,8 +2,8 @@ %li = image_tag 'b_user.png' , :size => '7x10', :border => 0, :alt => "Profil" = link_to h(@current_user.nick), my_profile_path, { :title => "Profil bearbeiten" } - - if Foodsoft.config[:homepage] - %li= link_to Foodsoft.config[:name], Foodsoft.config[:homepage], { :title => "Go to your FoodCoop-Hompage" } + - if FoodsoftConfig[:homepage] + %li= link_to FoodsoftConfig[:name], FoodsoftConfig[:homepage], { :title => "Go to your FoodCoop-Hompage" } %li= link_to "Hilfe", 'http://dev.foodcoops.net/wiki/FoodsoftDoku' %li= link_to "Feedback", new_feedback_path, :title => "Fehler gefunden? Vorschlag? Idee? Kritik?" %li= link_to "Abmelden", logout_path \ No newline at end of file diff --git a/config/app_config.yml.SAMPLE b/config/app_config.yml.SAMPLE index ebb3f127..218d40b7 100644 --- a/config/app_config.yml.SAMPLE +++ b/config/app_config.yml.SAMPLE @@ -1,10 +1,13 @@ # Foodsoft configuration -development: &defaults +default: &defaults # If you wanna serve more than one foodcoop with one installation # Don't forget to setup databases for each foodcoop. See also MULTI_COOP_INSTALL multi_coop_install: false + # If multi_coop_install you have to use a coop name, which you you wanna be selected by default + default_scope: 'f' + # http config for this host # Required for action mailer protocol: http @@ -75,6 +78,9 @@ development: &defaults 100g: 0.1 50g: 0.05 +development: + <<: *defaults + test: <<: *defaults diff --git a/config/initializers/load_app_config.rb b/config/initializers/load_app_config.rb index 601dfd11..1509536e 100644 --- a/config/initializers/load_app_config.rb +++ b/config/initializers/load_app_config.rb @@ -1,28 +1,15 @@ -# 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 - mattr_accessor :env, :config, :database - CONFIGS = YAML.load(File.read(File.join(Rails.root, "/config/app_config.yml"))) - DATABASES = YAML.load(File.read(File.join(Rails.root, "/config/database.yml"))) - - class << self - 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 # Initial load the default config and database from rails environment -Foodsoft.env = Rails.env +# See config/app_config.yml for further details +# Load Config, start by selecting defaults via current environment +require 'foodsoft_config' +FoodsoftConfig.init # Set action mailer default host for url generating url_options = { - :host => Foodsoft.config[:host], - :protocol => Foodsoft.config[:protocol] + :host => FoodsoftConfig[:host], + :protocol => FoodsoftConfig[:protocol] } -url_options.merge!({:port => Foodsoft.config[:port]}) if Foodsoft.config[:port] +url_options.merge!({:port => FoodsoftConfig[:port]}) if FoodsoftConfig[:port] Foodsoft::Application.configure do config.action_mailer.default_url_options = url_options @@ -31,9 +18,9 @@ Foodsoft::Application.configure do # Configuration of the exception_notification plugin # Mailadresses are set in config/app_config.yml config.middleware.use ExceptionNotifier, - :email_prefix => Foodsoft.config[:notification]['email_prefix'], - :sender_address => Foodsoft.config[:notification]['sender_address'], - :exception_recipients => Foodsoft.config[:notification]['error_recipients'] + :email_prefix => FoodsoftConfig[:notification]['email_prefix'], + :sender_address => FoodsoftConfig[:notification]['sender_address'], + :exception_recipients => FoodsoftConfig[:notification]['error_recipients'] end end diff --git a/config/routes.rb b/config/routes.rb index 9e6dd7ea..41d9fad3 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -6,9 +6,9 @@ Foodsoft::Application.routes.draw do get "sessions/new" - root :to => redirect("/#{Foodsoft.env}") + root :to => redirect("/#{FoodsoftConfig.scope}") - scope '/:foodcoop', :defaults => { :foodcoop => Foodsoft.env } do + scope '/:foodcoop' do # Root path root :to => 'home#index' diff --git a/lib/foodsoft_config.rb b/lib/foodsoft_config.rb new file mode 100644 index 00000000..4fdd9742 --- /dev/null +++ b/lib/foodsoft_config.rb @@ -0,0 +1,42 @@ +class FoodsoftConfig + mattr_accessor :scope, :config + APP_CONFIG = YAML.load(File.read(File.join(Rails.root, "/config/app_config.yml"))) + + class << self + + def init + # Load initial config from development or production + set_config Rails.env + # Overwrite scope to have a better namescope than 'production' + self.scope = config[:default_scope] or raise "No default_scope is set" + end + + # Set config and database connection for specific foodcoop + # Only needed in multi coop mode + def select_foodcoop(foodcoop) + set_config foodcoop + setup_database + end + + # Provides a nice accessor for config values + # FoodsoftConfig[:name] # => 'FC Test' + def [](key) + config[key] + end + + private + + def set_config(foodcoop) + raise "No config for this environment (#{foodcoop}) available!" if APP_CONFIG[foodcoop].nil? + self.config = APP_CONFIG[foodcoop].symbolize_keys + self.scope = foodcoop + end + + def setup_database + database_config = ActiveRecord::Base.configurations[Rails.env] + database_config.merge!(config[:database]) if config[:database].present? + ActiveRecord::Base.establish_connection(database_config) + end + + end +end \ No newline at end of file diff --git a/lib/wikilink.rb b/lib/wikilink.rb index 6512c3fb..56515c7f 100644 --- a/lib/wikilink.rb +++ b/lib/wikilink.rb @@ -3,8 +3,8 @@ class Wikilink < WikiCloth::WikiLinkHandler def link_attributes_for(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] + url_options = {:host => FoodsoftConfig[:host], :protocol => FoodsoftConfig[:protocol]} + url_options.merge!({:port => FoodsoftConfig[:port]}) if FoodsoftConfig[:port] if Page.exists?(:permalink => permalink) { :href => url_for(url_options.merge({:controller => "pages", :action => "show",