Skip to content

Commit

Permalink
fix: dont regenerate contracts during migration
Browse files Browse the repository at this point in the history
  • Loading branch information
diegosteiner committed Nov 29, 2024
1 parent f8877ef commit 6779758
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
11 changes: 9 additions & 2 deletions app/models/contract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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])

Expand Down
10 changes: 5 additions & 5 deletions app/models/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)) }
Expand All @@ -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?
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 6779758

Please sign in to comment.