From aa4bd7f39711e0af26c0c7a53458339dcad81d09 Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Mon, 29 Jan 2018 15:34:35 +0100 Subject: [PATCH 1/2] Improve code style and docs of job --- app/jobs/order_cycle_notification_job.rb | 7 +++++-- app/mailers/producer_mailer.rb | 16 +++++++++------- spec/jobs/order_cycle_notification_job_spec.rb | 11 +++++++---- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/app/jobs/order_cycle_notification_job.rb b/app/jobs/order_cycle_notification_job.rb index b18348813e..91f9121515 100644 --- a/app/jobs/order_cycle_notification_job.rb +++ b/app/jobs/order_cycle_notification_job.rb @@ -1,6 +1,9 @@ +# Deliers an email with a report of the order cycle to each of its suppliers OrderCycleNotificationJob = Struct.new(:order_cycle_id) do def perform - order_cycle = OrderCycle.find order_cycle_id - order_cycle.suppliers.each { |supplier| ProducerMailer.order_cycle_report(supplier, order_cycle).deliver } + order_cycle = OrderCycle.find(order_cycle_id) + order_cycle.suppliers.each do |supplier| + ProducerMailer.order_cycle_report(supplier, order_cycle).deliver + end end end diff --git a/app/mailers/producer_mailer.rb b/app/mailers/producer_mailer.rb index 70690970af..0bf05b787d 100644 --- a/app/mailers/producer_mailer.rb +++ b/app/mailers/producer_mailer.rb @@ -12,12 +12,14 @@ class ProducerMailer < Spree::BaseMailer subject = "[#{Spree::Config.site_name}] #{I18n.t('producer_mailer.order_cycle.subject', producer: producer.name)}" - if has_orders? order_cycle, producer - mail(to: @producer.contact.email, - from: from_address, - subject: subject, - reply_to: @coordinator.contact.email, - cc: @coordinator.contact.email) + if has_orders?(order_cycle, producer) + mail( + to: @producer.contact.email, + from: from_address, + subject: subject, + reply_to: @coordinator.contact.email, + cc: @coordinator.contact.email + ) end end @@ -30,7 +32,7 @@ class ProducerMailer < Spree::BaseMailer def line_items_from(order_cycle, producer) Spree::LineItem. - joins(:order => :order_cycle, :variant => :product). + joins(order: :order_cycle, variant: :product). where('order_cycles.id = ?', order_cycle). merge(Spree::Product.in_supplier(producer)). merge(Spree::Order.by_state('complete')) diff --git a/spec/jobs/order_cycle_notification_job_spec.rb b/spec/jobs/order_cycle_notification_job_spec.rb index 674bdbdd24..aa2eb2b2b4 100644 --- a/spec/jobs/order_cycle_notification_job_spec.rb +++ b/spec/jobs/order_cycle_notification_job_spec.rb @@ -2,11 +2,14 @@ require 'spec_helper' describe OrderCycleNotificationJob do let(:order_cycle) { create(:order_cycle) } + let(:mail) { double(:mail, deliver: true) } - it "sends a mail to each supplier" do - mail = double(:mail) - allow(mail).to receive(:deliver) - expect(ProducerMailer).to receive(:order_cycle_report).twice.and_return(mail) + before do + allow(ProducerMailer).to receive(:order_cycle_report).twice.and_return(mail) + end + + it 'sends a mail to each supplier' do run_job OrderCycleNotificationJob.new(order_cycle.id) + expect(ProducerMailer).to have_received(:order_cycle_report).twice end end From c79641c77c3dedcbc79feb3b0c12973f614060da Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Wed, 31 Jan 2018 15:23:31 +0100 Subject: [PATCH 2/2] Fix typos in documentation --- app/jobs/order_cycle_notification_job.rb | 2 +- app/models/order_cycle.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/jobs/order_cycle_notification_job.rb b/app/jobs/order_cycle_notification_job.rb index 91f9121515..d4efa6de13 100644 --- a/app/jobs/order_cycle_notification_job.rb +++ b/app/jobs/order_cycle_notification_job.rb @@ -1,4 +1,4 @@ -# Deliers an email with a report of the order cycle to each of its suppliers +# Delivers an email with a report of the order cycle to each of its suppliers OrderCycleNotificationJob = Struct.new(:order_cycle_id) do def perform order_cycle = OrderCycle.find(order_cycle_id) diff --git a/app/models/order_cycle.rb b/app/models/order_cycle.rb index 9ddde06edd..9dcf118bab 100644 --- a/app/models/order_cycle.rb +++ b/app/models/order_cycle.rb @@ -76,7 +76,7 @@ class OrderCycle < ActiveRecord::Base enterprises = Enterprise.managed_by(user) # Order cycles where I managed an enterprise at either end of an outgoing exchange - # ie. coordinator or distibutor + # ie. coordinator or distributor joins(:exchanges).merge(Exchange.outgoing). where('exchanges.receiver_id IN (?) OR exchanges.sender_id IN (?)', enterprises, enterprises). select('DISTINCT order_cycles.*')