Files
plottservice/app/models/job.rb
2024-08-01 23:15:05 +02:00

52 lines
1.7 KiB
Ruby

class Job < ApplicationRecord
belongs_to :operator, class_name: 'User', optional: true
belongs_to :costumer, class_name: 'User', optional: true
has_one_attached :pdf
validates_presence_of :costumer_firstname, :costumer_lastname, :privacy_policy_accepted, :pdf
# NOTE: Multiple status if paing before brinting?
enum status: {
open: 0,
printing: 1,
ready_for_pickup: 2,
paid: 3,
canceled: 4
}
scope :of_the_day, ->(date) { where('created_at >= ? AND created_at <= ?', date.beginning_of_day, date.end_of_day) }
scope :of_today, -> { of_the_day(Time.now) }
# 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 ready_for_pickup paid canceled]) }
# TODO: add logic (all with status: open, printing, ready_for_pickup. with status paid and canceled only from today)
scope :done_on_day, ->(date) { where('created_at >= ? AND created_at <= ?', date.beginning_of_day, date.end_of_day) }
def self.current_jobs_of_today
# NOTE: use Time.now instead of Date.today to take the timezone into account
of_the_day(Time.now).in_status_order
end
def self.current_jobs_of_(date)
# NOTE: use Time.now instead of Date.today to take the timezone into account
of_the_day(date).in_status_order
end
def fullname
[costumer_firstname, ' ', costumer_lastname].join
end
# TODO: Implement validation
def acceptable_pdf
return unless pdf.attached?
errors.add(:main_image, 'is too big') unless main_image.blob.byte_size <= 100.megabyte
acceptable_types = ['application/pdf']
return if acceptable_types.include?(main_image.content_type)
errors.add(:main_image, 'must be a PDF')
end
end