Moved order stats and cleanup stuff into rake task.

Closes #14
This commit is contained in:
Benjamin Meichsner 2010-02-09 21:45:57 +01:00
parent a6646d06a6
commit 55d2976e98
3 changed files with 30 additions and 26 deletions

View file

@ -95,7 +95,7 @@ class OrdersController < ApplicationController
def finish
order = Order.find(params[:id])
order.finish!(@current_user)
call_rake "foodsoft:notify_order_finished", :order_id => order.id
call_rake "foodsoft:finished_order_tasks", :order_id => order.id
flash[:notice] = "Die Bestellung wurde beendet."
redirect_to order
end

View file

@ -174,20 +174,6 @@ class Order < ActiveRecord::Base
# set new order state (needed by notify_order_finished)
update_attributes(:state => 'finished', :ends => Time.now, :updated_by => user)
# Update GroupOrder prices
group_orders.each { |go| go.update_price! }
# Clean up
# Delete no longer required order-history (group_order_article_quantities) and
# TODO: Do we need articles, which aren't ordered? (units_to_order == 0 ?)
order_articles.each do |oa|
oa.group_order_articles.each { |goa| goa.group_order_article_quantities.clear }
end
# Stats
# TODO: Move into background, if possible
ordergroups.each { |o| o.update_stats! }
end
end
end

View file

@ -68,13 +68,31 @@ namespace :foodsoft do
end
end
desc "Notify users of finished orders"
task :notify_order_finished => :environment do
desc "finished order tasks, cleanup, notifications, stats ..."
task :finished_order_tasks => :environment do
puts "Start: #{Time.now}"
order = Order.find(ENV["ORDER_ID"])
# Update GroupOrder prices
order.group_orders.each { |go| go.update_price! }
# Clean up
# Delete no longer required order-history (group_order_article_quantities) and
# TODO: Do we need articles, which aren't ordered? (units_to_order == 0 ?)
order.order_articles.each do |oa|
oa.group_order_articles.each { |goa| goa.group_order_article_quantities.clear }
end
# Notifications
for group_order in order.group_orders
for user in group_order.ordergroup.users
Mailer.deliver_order_result(user, group_order) if user.settings["notify.orderFinished"] == '1'
end
end
# Stats
order.ordergroups.each { |o| o.update_stats! }
puts "End: #{Time.now}"
end
end