Added job attributes, fixed seeds.rb

Added some attributes like status_changed_at. Refactored migration
files.
Changes to the migration file makes it necessary to reimport the
database: db:drop, db:create, db:migrate, db:seed
This commit is contained in:
2024-08-02 13:47:44 +02:00
parent 9a3777be44
commit c5e55eb410
16 changed files with 54 additions and 32 deletions

View File

@@ -3,7 +3,7 @@ class JobsController < ApplicationController
# GET /jobs or /jobs.json
def index
@jobs = Job.current_jobs_of_today
@jobs = Job.currently_working_on
end
# GET /jobs/1 or /jobs/1.json
@@ -23,7 +23,7 @@ class JobsController < ApplicationController
respond_to do |format|
if @job.save
format.html { redirect_to jobs_url(Job.current_jobs_of_today), notice: 'Job was successfully created.' }
format.html { redirect_to jobs_url(Job.currently_working_on), notice: 'Job was successfully created.' }
format.json { render :show, status: :created, location: @job }
else
format.html { render :new, status: :unprocessable_entity }

View File

@@ -5,6 +5,11 @@ class Job < ApplicationRecord
has_one_attached :pdf
validates_presence_of :costumer_firstname, :costumer_lastname, :privacy_policy_accepted, :pdf
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?
# NOTE: Multiple status if paing before brinting?
enum status: {
@@ -15,37 +20,55 @@ class Job < ApplicationRecord
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
scope :created_today, -> { created_on_day(Time.now) }
scope :created_on_day, lambda { |date|
where('created_at >= ? AND created_at <= ?', date.beginning_of_day, date.end_of_day)
}
scope :upgraded_today, -> { upgraded_on_day(Time.now) }
scope :upgraded_on_day, lambda { |date|
where('upgraded_at >= ? AND upgraded_at <= ?', date.beginning_of_day, date.end_of_day)
}
# Returns all jobs with status: open print ready_for_pickup and jobs from today with status: paid canceled
# paid: only printed_at today
# canceled: only updated_at today
def self.currently_working_on
# 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
where(status: %i[open printing ready_for_pickup])
.or(Job.where(status: %i[paid canceled])
.where('status_changed_at >= ?', Time.now.beginning_of_day))
# .in_status_order
.order(created_at: :asc)
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
errors.add(:pdf, 'is too big') unless pdf.blob.byte_size <= 100.megabyte
acceptable_types = ['application/pdf']
return if acceptable_types.include?(main_image.content_type)
return if acceptable_types.include?(pdf.content_type)
errors.add(:main_image, 'must be a PDF')
errors.add(:pdf, 'must be a PDF')
end
private
def update_printed_at
self.printed_at = Time.now if ready_for_pickup? || (paid? && printed_at.nil?)
end
def update_paid_at
self.paid_at = Time.now if paid?
end
def update_status_changed_at
self.status_changed_at = Time.now
end
end