From 99a66259f0487165a94fbde3e7b77772ddb38b9e Mon Sep 17 00:00:00 2001 From: Stas Date: Fri, 20 Nov 2015 18:27:00 +0200 Subject: [PATCH] 107821878-refactoring --- app/jobs/domain_delete_confirm_job.rb | 2 +- app/mailers/application_mailer.rb | 4 +-- app/mailers/contact_mailer.rb | 4 +-- app/mailers/domain_mailer.rb | 36 +++++++++++++-------------- app/models/contact.rb | 2 +- app/models/domain.rb | 10 ++++---- app/models/epp/domain.rb | 6 ++--- spec/mailers/contact_mailer_spec.rb | 6 ++--- spec/mailers/domain_mailer_spec.rb | 22 ++++++++-------- 9 files changed, 46 insertions(+), 46 deletions(-) diff --git a/app/jobs/domain_delete_confirm_job.rb b/app/jobs/domain_delete_confirm_job.rb index aa3effb12..de5e02737 100644 --- a/app/jobs/domain_delete_confirm_job.rb +++ b/app/jobs/domain_delete_confirm_job.rb @@ -9,7 +9,7 @@ class DomainDeleteConfirmJob < Que::Job domain.apply_pending_delete! domain.clean_pendings! when RegistrantVerification::REJECTED - DomainMailer.pending_delete_rejected_notification(domain_id).deliver + DomainMailer.pending_delete_rejected_notification(domain_id, deliver_emails).deliver domain.statuses.delete(DomainStatus::PENDING_DELETE_CONFIRMATION) domain.poll_message!(:poll_pending_delete_rejected_by_registrant) domain.clean_pendings! diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb index 339638d80..b5b46e6f2 100644 --- a/app/mailers/application_mailer.rb +++ b/app/mailers/application_mailer.rb @@ -17,8 +17,8 @@ class ApplicationMailer < ActionMailer::Base end # turn on delivery on specific (epp) request only, thus rake tasks does not deliver anything - def delivery_off?(model) - return false if model.deliver_emails == true + def delivery_off?(model, deliver_email= false) + return false if deliver_emails == true logger.info "EMAIL SENDING WAS NOT ACTIVATED " \ "BY MODEL OBJECT: id ##{model.try(:id)} deliver_emails returned false" true diff --git a/app/mailers/contact_mailer.rb b/app/mailers/contact_mailer.rb index 2f858a585..71e635540 100644 --- a/app/mailers/contact_mailer.rb +++ b/app/mailers/contact_mailer.rb @@ -1,10 +1,10 @@ class ContactMailer < ApplicationMailer include Que::Mailer - def email_updated(email, contact_id) + def email_updated(email, contact_id, should_deliver) @contact = Contact.find_by(id: contact_id) return unless email || @contact - return if delivery_off?(contact) + return if delivery_off?(contact, should_deliver) return if whitelist_blocked?(email) begin diff --git a/app/mailers/domain_mailer.rb b/app/mailers/domain_mailer.rb index 70f5d8f39..a149ff2ed 100644 --- a/app/mailers/domain_mailer.rb +++ b/app/mailers/domain_mailer.rb @@ -1,10 +1,10 @@ class DomainMailer < ApplicationMailer include Que::Mailer - def pending_update_request_for_old_registrant(domain_id) + def pending_update_request_for_old_registrant(domain_id, should_deliver) @domain = Domain.find_by(id: domain_id) return unless @domain - return if delivery_off?(@domain) + return if delivery_off?(@domain, should_deliver) if @domain.registrant_verification_token.blank? logger.warn "EMAIL NOT DELIVERED: registrant_verification_token is missing for #{@domain.name}" @@ -27,10 +27,10 @@ class DomainMailer < ApplicationMailer name: @domain.name)} [#{@domain.name}]") end - def pending_update_notification_for_new_registrant(domain_id) + def pending_update_notification_for_new_registrant(domain_id, should_deliver) @domain = Domain.find_by(id: domain_id) return unless @domain - return if delivery_off?(@domain) + return if delivery_off?(@domain, should_deliver) if @domain.registrant_verification_token.blank? logger.warn "EMAIL NOT DELIVERED: registrant_verification_token is missing for #{@domain.name}" @@ -51,10 +51,10 @@ class DomainMailer < ApplicationMailer name: @domain.name)} [#{@domain.name}]") end - def registrant_updated_notification_for_new_registrant(domain_id) + def registrant_updated_notification_for_new_registrant(domain_id, should_deliver) @domain = Domain.find_by(id: domain_id) return unless @domain - return if delivery_off?(@domain) + return if delivery_off?(@domain, should_deliver) return if whitelist_blocked?(@domain.registrant_email) mail(to: format(@domain.registrant_email), @@ -62,10 +62,10 @@ class DomainMailer < ApplicationMailer name: @domain.name)} [#{@domain.name}]") end - def registrant_updated_notification_for_old_registrant(domain_id) + def registrant_updated_notification_for_old_registrant(domain_id, should_deliver) domain = Domain.find_by(id: domain_id) return unless domain - return if delivery_off?(@domain) + return if delivery_off?(@domain, should_deliver) @old_registrant_email = domain.registrant_email # Nb! before applying pending updates @@ -107,10 +107,10 @@ class DomainMailer < ApplicationMailer name: @domain.name)} [#{@domain.name}]") end - def pending_deleted(domain_id) + def pending_deleted(domain_id, should_deliver) @domain = Domain.find_by(id: domain_id) return unless @domain - return if delivery_off?(@domain) + return if delivery_off?(@domain, should_deliver) if @domain.registrant_verification_token.blank? logger.warn "EMAIL NOT DELIVERED: registrant_verification_token is missing for #{@domain.name}" @@ -133,10 +133,10 @@ class DomainMailer < ApplicationMailer name: @domain.name)} [#{@domain.name}]") end - def pending_delete_rejected_notification(domain_id) + def pending_delete_rejected_notification(domain_id, should_deliver) @domain = Domain.find_by(id: domain_id) return unless @domain - return if delivery_off?(@domain) + return if delivery_off?(@domain, should_deliver) # no delivery off control, driggered by que, no epp request if @domain.registrant_verification_token.blank? @@ -155,10 +155,10 @@ class DomainMailer < ApplicationMailer name: @domain.name)} [#{@domain.name}]") end - def pending_delete_expired_notification(domain_id) + def pending_delete_expired_notification(domain_id, should_deliver) @domain = Domain.find_by(id: domain_id) return unless @domain - return if delivery_off?(@domain) + return if delivery_off?(@domain, should_deliver) # no delivery off control, driggered by cron, no epp request return if whitelist_blocked?(@domain.registrant.email) @@ -167,10 +167,10 @@ class DomainMailer < ApplicationMailer name: @domain.name)} [#{@domain.name}]") end - def delete_confirmation(domain_id) + def delete_confirmation(domain_id, should_deliver) @domain = Domain.find_by(id: domain_id) return unless @domain - return if delivery_off?(@domain) + return if delivery_off?(@domain, should_deliver) return if whitelist_blocked?(@domain.registrant.email) mail(to: format(@domain.registrant.email), @@ -178,9 +178,9 @@ class DomainMailer < ApplicationMailer name: @domain.name)} [#{@domain.name}]") end - def force_delete(domain_id) + def force_delete(domain_id, should_deliver) @domain = Domain.find_by(id: domain_id) - return if delivery_off?(@domain) + return if delivery_off?(@domain, should_deliver) emails = ([@domain.registrant.email] + @domain.admin_contacts.map { |x| format(x.email) }).uniq return if whitelist_blocked?(emails) diff --git a/app/models/contact.rb b/app/models/contact.rb index 9f623a262..20c11ae80 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -50,7 +50,7 @@ class Contact < ActiveRecord::Base emails << domains.map(&:registrant_email) if domains.present? emails = emails.flatten.uniq emails.each do |e| - ContactMailer.email_updated(e, id).deliver + ContactMailer.email_updated(e, id, deliver_emails).deliver end end diff --git a/app/models/domain.rb b/app/models/domain.rb index 8d02f7f83..6730497e9 100644 --- a/app/models/domain.rb +++ b/app/models/domain.rb @@ -241,7 +241,7 @@ class Domain < ActiveRecord::Base DomainMailer.pending_update_expired_notification_for_new_registrant(id).deliver end if domain.pending_delete? || domain.pending_delete_confirmation? - DomainMailer.pending_delete_expired_notification(id).deliver + DomainMailer.pending_delete_expired_notification(id, deliver_emails).deliver end domain.clean_pendings! unless Rails.env.test? @@ -428,8 +428,8 @@ class Domain < ActiveRecord::Base new_registrant_email = registrant.email new_registrant_name = registrant.name - DomainMailer.pending_update_request_for_old_registrant(id).deliver - DomainMailer.pending_update_notification_for_new_registrant(id).deliver + DomainMailer.pending_update_request_for_old_registrant(id, deliver_emails).deliver + DomainMailer.pending_update_notification_for_new_registrant(id, deliver_emails).deliver reload # revert back to original @@ -489,7 +489,7 @@ class Domain < ActiveRecord::Base pending_delete_confirmation! save(validate: false) # should check if this did succeed - DomainMailer.pending_deleted(id).deliver + DomainMailer.pending_deleted(id, deliver_emails).deliver end def pricelist(operation, period_i = nil, unit = nil) @@ -619,7 +619,7 @@ class Domain < ActiveRecord::Base registrar.messages.create!( body: I18n.t('force_delete_set_on_domain', domain: name) ) - DomainMailer.force_delete(id).deliver + DomainMailer.force_delete(id, deliver_emails).deliver return true end false diff --git a/app/models/epp/domain.rb b/app/models/epp/domain.rb index f31105f4d..69ffeb7f3 100644 --- a/app/models/epp/domain.rb +++ b/app/models/epp/domain.rb @@ -478,7 +478,7 @@ class Epp::Domain < Domain # rubocop: enable Metrics/CyclomaticComplexity def apply_pending_update! - old_registrant_email = DomainMailer.registrant_updated_notification_for_old_registrant(id) + old_registrant_email = DomainMailer.registrant_updated_notification_for_old_registrant(id, deliver_emails) preclean_pendings user = ApiUser.find(pending_json['current_user_id']) frame = Nokogiri::XML(pending_json['frame']) @@ -488,7 +488,7 @@ class Epp::Domain < Domain return unless update(frame, user, false) clean_pendings! self.deliver_emails = true # turn on email delivery - DomainMailer.registrant_updated_notification_for_new_registrant(id).deliver + DomainMailer.registrant_updated_notification_for_new_registrant(id, deliver_emails).deliver old_registrant_email.deliver true end @@ -497,7 +497,7 @@ class Epp::Domain < Domain preclean_pendings statuses.delete(DomainStatus::PENDING_DELETE_CONFIRMATION) statuses.delete(DomainStatus::PENDING_DELETE) - DomainMailer.delete_confirmation(id).deliver + DomainMailer.delete_confirmation(id, deliver_emails).deliver # TODO: confirm that this actually makes sense clean_pendings! if valid? && set_pending_delete! diff --git a/spec/mailers/contact_mailer_spec.rb b/spec/mailers/contact_mailer_spec.rb index f0402e3db..2cb8fe344 100644 --- a/spec/mailers/contact_mailer_spec.rb +++ b/spec/mailers/contact_mailer_spec.rb @@ -8,7 +8,7 @@ describe ContactMailer do describe 'email changed notification when delivery turned off' do before :all do @contact = Fabricate(:contact, email: 'test@example.ee') - @mail = ContactMailer.email_updated('test@example.com', @contact.id) + @mail = ContactMailer.email_updated('test@example.com', @contact.id, deliver_emails) end it 'should not render email subject' do @@ -34,7 +34,7 @@ describe ContactMailer do @contact = @domain.registrant @contact.reload # until figured out why registrant_domains not loaded @contact.deliver_emails = true - @mail = ContactMailer.email_updated('info@example.org', @contact.id) + @mail = ContactMailer.email_updated('info@example.org', @contact.id, deliver_emails) end it 'should render email subject' do @@ -60,7 +60,7 @@ describe ContactMailer do @contact = @domain.registrant @contact.reload # until figured out why registrant_domains not loaded @contact.deliver_emails = true - @mail = ContactMailer.email_updated('info@ääöü.org', @contact.id) + @mail = ContactMailer.email_updated('info@ääöü.org', @contact.id, deliver_emails) end it 'should render email subject' do diff --git a/spec/mailers/domain_mailer_spec.rb b/spec/mailers/domain_mailer_spec.rb index 20662a2ba..37605a668 100644 --- a/spec/mailers/domain_mailer_spec.rb +++ b/spec/mailers/domain_mailer_spec.rb @@ -9,7 +9,7 @@ describe DomainMailer do before :all do @registrant = Fabricate(:registrant, email: 'test@example.com') @domain = Fabricate(:domain, registrant: @registrant) - @mail = DomainMailer.pending_update_request_for_old_registrant(@domain.id) + @mail = DomainMailer.pending_update_request_for_old_registrant(@domain.id, deliver_emails) end it 'should not render email subject' do @@ -38,7 +38,7 @@ describe DomainMailer do @domain.registrant_verification_token = '123' @domain.registrant_verification_asked_at = Time.zone.now @domain.registrant = @new_registrant - @mail = DomainMailer.pending_update_request_for_old_registrant(@domain.id) + @mail = DomainMailer.pending_update_request_for_old_registrant(@domain.id, deliver_emails) end it 'should render email subject' do @@ -71,7 +71,7 @@ describe DomainMailer do @domain.registrant_verification_token = '123' @domain.registrant_verification_asked_at = Time.zone.now @domain.registrant = @new_registrant - @mail = DomainMailer.pending_update_notification_for_new_registrant(@domain.id) + @mail = DomainMailer.pending_update_notification_for_new_registrant(@domain.id, deliver_emails) end it 'should render email subject' do @@ -100,7 +100,7 @@ describe DomainMailer do @domain.registrant_verification_token = '123' @domain.registrant_verification_asked_at = Time.zone.now @domain.registrant = @new_registrant - @mail = DomainMailer.pending_update_notification_for_new_registrant(@domain.id) + @mail = DomainMailer.pending_update_notification_for_new_registrant(@domain.id, deliver_emails) end it 'should render email subject' do @@ -153,7 +153,7 @@ describe DomainMailer do @registrant = Fabricate(:registrant, email: 'test@example.com') @domain = Fabricate(:domain, registrant: @registrant) @domain.deliver_emails = true - @mail = DomainMailer.registrant_updated_notification_for_new_registrant(@domain.id) + @mail = DomainMailer.registrant_updated_notification_for_new_registrant(@domain.id, deliver_emails) end it 'should render email subject' do @@ -178,7 +178,7 @@ describe DomainMailer do @registrant = Fabricate(:registrant, email: 'test@example.com') @domain = Fabricate(:domain, registrant: @registrant) @domain.deliver_emails = true - @mail = DomainMailer.registrant_updated_notification_for_old_registrant(@domain.id) + @mail = DomainMailer.registrant_updated_notification_for_old_registrant(@domain.id, deliver_emails) end it 'should render email subject' do @@ -202,7 +202,7 @@ describe DomainMailer do before :all do @registrant = Fabricate(:registrant, email: 'test@example.com') @domain = Fabricate(:domain, registrant: @registrant) - @mail = DomainMailer.pending_deleted(@domain.id) + @mail = DomainMailer.pending_deleted(@domain.id, deliver_emails) end it 'should not render email subject' do @@ -229,7 +229,7 @@ describe DomainMailer do @domain.deliver_emails = true @domain.registrant_verification_token = '123' @domain.registrant_verification_asked_at = Time.zone.now - @mail = DomainMailer.pending_deleted(@domain.id) + @mail = DomainMailer.pending_deleted(@domain.id, deliver_emails) end it 'should render email subject' do @@ -260,7 +260,7 @@ describe DomainMailer do @domain.deliver_emails = true @domain.registrant_verification_token = '123' @domain.registrant_verification_asked_at = Time.zone.now - @mail = DomainMailer.pending_delete_rejected_notification(@domain.id) + @mail = DomainMailer.pending_delete_rejected_notification(@domain.id, deliver_emails) end it 'should render email subject' do @@ -287,7 +287,7 @@ describe DomainMailer do @domain.deliver_emails = true @domain.registrant_verification_token = '123' @domain.registrant_verification_asked_at = Time.zone.now - @mail = DomainMailer.pending_delete_expired_notification(@domain.id) + @mail = DomainMailer.pending_delete_expired_notification(@domain.id, deliver_emails) end it 'should render email subject' do @@ -314,7 +314,7 @@ describe DomainMailer do @domain.deliver_emails = true @domain.registrant_verification_token = '123' @domain.registrant_verification_asked_at = Time.zone.now - @mail = DomainMailer.delete_confirmation(@domain.id) + @mail = DomainMailer.delete_confirmation(@domain.id, deliver_emails) end it 'should render email subject' do