Number of plans can no be incremented or decremented

This commit is contained in:
2024-08-13 13:00:59 +02:00
parent 3a660d42de
commit eaf7fdd2be
4 changed files with 70 additions and 40 deletions

View File

@@ -5,18 +5,23 @@ class Job < ApplicationRecord
has_one_attached :pdf, dependent: :purge
validates_presence_of :costumer_firstname, :costumer_lastname, :privacy_policy_accepted, :pdf
# validates_numericality_of {:number_of_plans_a0,:number_of_plans_a1, :number_of_plans_a2, :number_of_plans_a3}, greater_than_or_equal_to: 0
validates :number_of_plans_a0, :number_of_plans_a1, :number_of_plans_a2, :number_of_plans_a3, numericality: { greater_than_or_equal_to: 0 }
validate :acceptable_pdf
before_save :update_printed_at, if: :will_save_change_to_status?
before_save :update_paid_at, if: :will_save_change_to_status?
before_save :update_status_changed_at, if: :will_save_change_to_status?
before_save :set_cost_qm
before_save :calc_cost, if: :printed_pages_changes?
# TODO: works only when job is created. Should move analyzer to activestorage :https://discuss.rubyonrails.org/t/active-storage-in-production-lessons-learned-and-in-depth-look-at-how-it-works/83289
after_create_commit :analyze_pdf
# NOTE: Multiple status if paing before brinting?
enum status: {
enum :status, {
open: 0,
printing: 1,
pickup: 2,
@@ -24,6 +29,8 @@ class Job < ApplicationRecord
canceled: 4
}
AVAILABLE_PAGE_FORMATS = [ :a0, :a1, :a2, :a3 ]
# NOTE: only named status are returned because of WHERE/IN clause for the enum values
scope :in_status_order, -> { in_order_of(:status, %w[open printing pickup paid canceled]) }
@@ -86,8 +93,24 @@ class Job < ApplicationRecord
end
end
def increment_page(din)
return false unless AVAILABLE_PAGE_FORMATS.include?(din.to_sym)
public_send("number_of_plans_#{din}=", public_send("number_of_plans_#{din}") + 1) if respond_to? "number_of_plans_#{din}="
save
end
def decrement_page(din)
return false unless AVAILABLE_PAGE_FORMATS.include?(din.to_sym)
public_send("number_of_plans_#{din}=", public_send("number_of_plans_#{din}") - 1) if respond_to? "number_of_plans_#{din}="
save
end
private
def printed_pages_changes?
costum_qm_plan_previously_changed? || number_of_plans_a0_changed? || number_of_plans_a1_changed? || number_of_plans_a2_changed? || number_of_plans_a3_changed?
end
def update_printed_at
self.printed_at = Time.now if pickup? || (paid? && printed_at.nil?)
end