Initial commit of foodsoft 2

This commit is contained in:
Benjamin Meichsner 2009-01-06 11:49:19 +01:00
commit 5b9a7e05df
657 changed files with 70444 additions and 0 deletions

183
lib/foodsoft.rb Normal file
View file

@ -0,0 +1,183 @@
require 'yaml'
# General FoodSoft module for application configuration and global methods.
#
# This library needs to be loaded in environment.rb through <code>require 'foodsoft'</code>.
#
module FoodSoft
private
@@foodcoops = Hash.new
@@database = Hash.new
@@foodsoft = Hash.new
@@subdomain = String.new
public
# Loads the configuration file config/foodsoft.yml, ..foodcoops.yml and ..database.yml
def self.load_configuration
# load foodcoops-config
@@foodcoops = YAML::load(File.open("#{RAILS_ROOT}/config/foodcoops.yml"))
# load database-config
@@database = YAML::load(File.open("#{RAILS_ROOT}/config/database.yml"))
# load foodsoft-config
@@foodsoft = YAML::load(File.open("#{RAILS_ROOT}/config/foodsoft.yml")).symbolize_keys
# validates the parsed data
self.validate
rescue => e
# raise "Failed to load configuration files: #{e.message}"
end
def self.subdomain=(subdomain)
@@subdomain = subdomain
end
def self.subdomain
return @@subdomain
end
def self.format_time(time = Time.now)
raise "FoodSoft::time_format has not been set!" unless @@foodcoops[subdomain]["time_format"]
time.strftime(@@foodcoops[subdomain]["time_format"]) unless time.nil?
end
def self.format_date(date = Time.now)
raise "FoodSoft: date_format has not been set!" unless @@foodcoops[subdomain]["date_format"]
date.strftime(@@foodcoops[subdomain]["date_format"]) unless date.nil?
end
def self.format_date_time(time = Time.now)
"#{format_date(time)} #{format_time(time)}" unless time.nil?
end
def self.format_currency(decimal)
"#{self.getCurrencyUnit} %01.2f" % decimal
end
# Returns the set host, otherwise returns nil
def self.getHost
return @@foodcoops[subdomain]["host"]
end
def self.getFoodcoopName
raise 'foodcoopName has not been set!' unless @@foodcoops[subdomain]["name"]
return @@foodcoops[subdomain]["name"]
end
def self.getFoodcoopContact
raise "contact has not been set!" unless @@foodcoops[subdomain]["contact"]
return @@foodcoops[subdomain]["contact"].symbolize_keys
end
def self.getFoodcoopUrl
return @@foodcoops[subdomain]["base_url"]
end
def self.getHelp
raise 'foodsoftHelp has not been set!' unless @@foodcoops[subdomain]["help_url"]
return @@foodcoops[subdomain]["help_url"]
end
# Returns the email sender used for system emails.
def self.getEmailSender
raise 'FoodSoft::emailSender has not been set!' unless @@foodcoops[subdomain]["email_sender"]
return @@foodcoops[subdomain]["email_sender"]
end
# Returns the price markup.
def self.getPriceMarkup
raise "FoodSoft::priceMarkup has not been set!" unless @@foodcoops[subdomain]["price_markup"]
return @@foodcoops[subdomain]["price_markup"]
end
# Returns the local decimal separator.
def self.getDecimalSeparator
if (separator = LocalizationSimplified::NumberHelper::CurrencyOptions[:separator])
return separator
else
logger.warn('No locale configured through plugin LocalizationSimplified')
return '.'
end
end
# Returns the local currency unit.
def self.getCurrencyUnit
if (unit = LocalizationSimplified::NumberHelper::CurrencyOptions[:unit])
return unit
else
logger.warn('No locale configured through plugin LocalizationSimplified')
return '$'
end
end
# Returns the delocalized version of the string, i.e. with the decimal separator local character properly replaced.
# For example, for the locale "de-DE", the comma character "," will be replaced with the standard separator ".".
def self.delocalizeDecimalString(string)
if (string && string.is_a?(String) && !string.empty?)
separator = getDecimalSeparator
if (separator != '.' && string.index(separator))
string = string.sub(separator, '.')
end
end
return string
end
# Return the specific database
def self.get_database
raise 'databse for foodcoop has not been set' unless @@database[subdomain]
return @@database[subdomain]
end
# Foodsoft-Config begins
# Returns an array with mail-adresses for the exception_notification plugin
def self.get_notification_config
raise 'FoodSoft::errorRecipients has not been set!' unless @@foodsoft[:notification]
return @@foodsoft[:notification].symbolize_keys
end
# returns shared_lists database connection
def self.get_shared_lists_config
raise "sharedLists database config has not been set" unless @@foodsoft[:shared_lists]
return @@foodsoft[:shared_lists]
end
# returns a string for an integrity hash for cookie session data
def self.get_session_secret
raise "session secret string has not been set" unless @@foodsoft[:session_secret]
return @@foodsoft[:session_secret]
end
# returns units-hash for automatic units-conversion
# this hash looks like {"KG" => 1, "500g" => 0.5, ...}
def self.get_units_factors
raise "units has not been set" unless @@foodsoft[:units]
@@foodsoft[:units]
end
# validates the yaml-parsed-config-file
def self.validate
raise "Price markup is not a proper float. please use at least one decimal place" unless @@foodcoops.each {|fc| fc["price_markup"].is_a?(Float)}
raise "Error recipients aren't set correctly. use hyphen for each recipient" unless @@foodsoft[:error_recipients].is_a?(Array)
end
end
# Automatically load configuration file:
FoodSoft::load_configuration
# Makes "number_to_percentage" locale aware.
module ActionView
module Helpers
module NumberHelper
alias_method :foodsoft_old_number_to_percentage, :number_to_percentage
# Returns the number in the localized percentage format.
def number_to_percentage(number, options = {})
foodsoft_old_number_to_percentage(number, :precision => 1, :separator => FoodSoft::getDecimalSeparator)
end
end
end
end

43
lib/foodsoft_file.rb Normal file
View file

@ -0,0 +1,43 @@
# Module for FoodSoft-File import
# The FoodSoft-File is a cvs-file, with semicolon-seperatet columns
require 'faster_csv'
module FoodsoftFile
# parses a string from a foodsoft-file
# returns two arrays with articles and outlisted_articles
# the parsed article is a simple hash
def self.parse(file)
articles, outlisted_articles = Array.new, Array.new
row_index = 2
FasterCSV.parse(file.read, {:col_sep => ";", :headers => true}) do |row|
# check if the line is empty
unless row[2] == "" || row[2].nil?
article = {:number => row[1],
:name => row[2],
:note => row[3],
:manufacturer => row[4],
:origin => row[5],
:unit => row[6],
:price => row[7],
:tax => row[8],
:deposit => (row[9].nil? ? "0" : row[9]),
:unit_quantity => row[10],
:scale_quantity => row[11],
:scale_price => row[12],
:category => row[13]}
case row[0]
when "x"
# check if the article is outlisted
outlisted_articles << article
else
articles << article
end
end
row_index += 1
end
return [articles, outlisted_articles]
end
end

26
lib/tasks/foodsoft.rake Normal file
View file

@ -0,0 +1,26 @@
# put in here all foodsoft tasks
# => :environment loads the environment an gives easy access to the application
namespace :foodsoft do
# "rake foodsoft:create_admin"
desc "creates Administrators-group and admin-user"
task :create_admin => :environment do
puts "Create Group 'Administators'"
administrators = Group.create(:name => "Administrators",
:description => "System administrators.",
:role_admin => true,
:role_finance => true,
:role_article_meta => true,
:role_suppliers => true,
:role_orders => true)
puts "Create User 'admin' with password 'secret'"
admin = User.new(:nick => "admin", :first_name => "Anton", :last_name => "Administrator", :email => "admin@foo.test")
admin.password = "secret"
admin.save
puts "Joining 'admin' user to 'Administrators' group"
Membership.create(:group => administrators, :user => admin)
end
end

50
lib/tasks/gettext.rake Normal file
View file

@ -0,0 +1,50 @@
# these are the tasks to updates localization-data
require 'gettext/utils'
# Reopen to make RubyGettext's ERB parser parse .html.erb files
module GetText
module ErbParser
@config = {
:extnames => ['.rhtml', '.erb']
}
end
end
begin
require "#{RAILS_ROOT}/vendor/plugins/haml/lib/haml"
rescue LoadError
require 'haml' # From gem
end
module HamlParser
module_function
def target?(file)
File.extname(file) == '.haml'
end
def parse(file, ary = [])
haml = Haml::Engine.new(IO.readlines(file).join)
code = haml.precompiled.split(/$/)
RubyParser.parse_lines(file, code, ary)
end
end
GetText::RGetText.add_parser(HamlParser)
namespace :gettext do
desc 'Create mo-files for L10n'
task :makemo do
GetText.create_mofiles(true, 'po', 'locale')
end
desc 'Update pot/po files to match new version'
task :updatepo do
TEXT_DOMAIN = 'foodsoft'
APP_VERSION = '2.0'
files = Dir.glob('{app,vendor/plugins/mod_**}/**/*.rb')
files.concat(Dir.glob('{app,vendor/plugins/mod_**}/**/*.rhtml'))
files.concat(Dir.glob('{app,vendor/plugins/mod_**}/**/*.erb'))
files.concat(Dir.glob('{app,vendor/plugins/mod_**}/**/*.haml'))
GetText.update_pofiles(TEXT_DOMAIN, files, APP_VERSION)
end
end

31
lib/tasks/rails.rake Normal file
View file

@ -0,0 +1,31 @@
desc "Checks your app and gently warns you if you are using deprecated code."
task :deprecated => :environment do
deprecated = {
'@params' => 'Use params[] instead',
'@session' => 'Use session[] instead',
'@flash' => 'Use flash[] instead',
'@request' => 'Use request[] instead',
'@env' => 'Use env[] instead',
'find_all\([^_]\|$\)' => 'Use find(:all) instead',
'find_first' => 'Use find(:first) instead',
'render_partial' => 'Use render :partial instead',
'component' => 'Use of components are frowned upon',
'paginate' => 'The default paginator is slow. Writing your own may be faster',
'start_form_tag' => 'Use form_for instead',
'end_form_tag' => 'Use form_for instead',
':post => true' => 'Use :method => :post instead'
}
deprecated.each do |key, warning|
puts '--> ' + key
output = `cd '#{File.expand_path('app', RAILS_ROOT)}' && grep -n --exclude=*.svn* --exclude=.#* -r '#{key}' *`
unless output =~ /^$/
puts " !! " + warning + " !!"
puts ' ' + '.' * (warning.length + 6)
puts output
else
puts " Clean! Cheers for you!"
end
puts
end
end