app/models/job/order_processor.rb in artfully_ose-1.2.0.pre.15 vs app/models/job/order_processor.rb in artfully_ose-1.2.0.pre.16

- old
+ new

@@ -1,28 +1,69 @@ # -# This class manages all the things we need to do after a sale is made +# This class manages all the patron-related things we need to do after an order is placed # -# TODO: Add sending email. +# * Create donation, purchase actions +# * Process memberships +# * Send confirmation emails if skip_email is true # -class OrderProcessor < Struct.new(:order, :skip_actions) +# options can include: +# :skip_actions. If set to true, no actions will be generated for this action +# :skip_email. If set to true, no email confirmation will be sent +# +class OrderProcessor < Struct.new(:order, :options) include ActionView::Helpers::TextHelper QUEUE = "order" - def self.process(order, skip_actions = false) - job = OrderProcessor.new(order, skip_actions) + def initialize(order, options = {}) + self.options = options + self.order = order + end + + def self.process(order, options = {}) + job = OrderProcessor.new(order, options) Delayed::Job.enqueue job, :queue => QUEUE end def perform - self.order.create_donation_actions unless self.skip_actions - self.order.create_purchase_action unless self.skip_actions + ActiveRecord::Base.transaction do + self.order.create_donation_actions unless skip_actions + self.order.create_purchase_action unless skip_actions + process_memberships + end - process_memberships + begin + generate_pdf + rescue Exception => e + Exceptional.context(:order_id => order.id) + Exceptional.handle(e, "Could not generate PDF for order") + end + + begin + OrderMailer.confirmation_for(order).deliver unless skip_email + rescue Exception => e + Exceptional.context(:order_id => order.id) + Exceptional.handle(e, "Could not send order confirmation for order") + end end + def skip_actions + self.options[:skip_actions] == true + end + + def skip_email + self.options[:skip_email] == true + end + def process_memberships self.order.memberships.each do |membership_item| Member.for(membership_item.product) end self.order.create_membership_action end -end \ No newline at end of file + + def generate_pdf + pdf_generator = Orders::PdfGeneration.new(order) + download_url = pdf_generator.generate + order.pdf = URI.parse(download_url) + order.save + end +end