From 67797580e17ca4a3942eb6fee3a7a57353b48605 Mon Sep 17 00:00:00 2001 From: Diego Steiner Date: Fri, 29 Nov 2024 17:29:04 +0000 Subject: [PATCH] fix: dont regenerate contracts during migration --- app/models/contract.rb | 11 +++++++++-- app/models/invoice.rb | 10 +++++----- ...0241129161448_set_locale_for_existing_contracts.rb | 5 ++++- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/app/models/contract.rb b/app/models/contract.rb index 3c68c58ef..ae1aeb2f8 100644 --- a/app/models/contract.rb +++ b/app/models/contract.rb @@ -30,15 +30,18 @@ class Contract < ApplicationRecord has_one_attached :pdf has_one_attached :signed_pdf + attr_accessor :skip_generate_pdf + scope :valid, -> { where(valid_until: nil) } scope :sent, -> { where.not(sent_at: nil) } scope :unsent, -> { where(sent_at: nil) } scope :ordered, -> { order(valid_from: :asc) } scope :signed, -> { where.not(signed_at: nil) } - before_save :supersede, :generatate_pdf, :set_signed_at + before_save :supersede, :set_signed_at + before_save :generate_pdf, if: :generate_pdf? - def generatate_pdf + def generate_pdf I18n.with_locale(locale || I18n.locale) do self.pdf = { io: StringIO.new(Export::Pdf::ContractPdf.new(self).render_document), @@ -48,6 +51,10 @@ def generatate_pdf end end + def generate_pdf? + !skip_generate_pdf && (pdf.blank? || changed?) + end + def supersede(**attributes) return unless was_sent? && changed.intersect?(%w[text]) diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 0de2f293b..9de427713 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -53,6 +53,8 @@ class Invoice < ApplicationRecord has_one :organisation, through: :booking has_one_attached :pdf + attr_accessor :skip_generate_pdf + scope :ordered, -> { order(payable_until: :ASC, created_at: :ASC) } scope :unpaid, -> { kept.where(arel_table[:amount_open].gt(0)) } scope :unsettled, -> { kept.where.not(type: 'Invoices::Offer').where.not(arel_table[:amount_open].eq(0)) } @@ -77,7 +79,7 @@ class Invoice < ApplicationRecord end def generate_pdf? - kept? && ref.present? && (pdf.blank? || changed?) + kept? && ref.present? && !skip_generate_pdf && (pdf.blank? || changed?) end def generate_ref? @@ -93,10 +95,8 @@ def supersede! def generate_pdf I18n.with_locale(locale || I18n.locale) do - self.pdf = { - io: StringIO.new(Export::Pdf::InvoicePdf.new(self).render_document), - filename:, content_type: 'application/pdf' - } + self.pdf = { io: StringIO.new(Export::Pdf::InvoicePdf.new(self).render_document), + filename:, content_type: 'application/pdf' } end end diff --git a/db/migrate/20241129161448_set_locale_for_existing_contracts.rb b/db/migrate/20241129161448_set_locale_for_existing_contracts.rb index 51bb3fd35..1a8f88b55 100644 --- a/db/migrate/20241129161448_set_locale_for_existing_contracts.rb +++ b/db/migrate/20241129161448_set_locale_for_existing_contracts.rb @@ -1,7 +1,10 @@ class SetLocaleForExistingContracts < ActiveRecord::Migration[8.0] def up Booking.where(concluded: false).find_each do |booking| - booking.contract&.update(locale: booking.locale) + next if booking.contract.blank? + + booking.contract.skip_generate_pdf = true + booking.contract.update(locale: booking.locale) end end end