Added pdf analyzer
Analyzer should be moved to ActiveStorage::Analyzer First there are many bugs with ActiveStorage that needs to be fixed: Files are not deleted if job is destroyed (dependent: :purge is set) If a new file is uploaded the old file is not deleted (has_one_attechment)
This commit is contained in:
@@ -2,7 +2,7 @@ class Job < ApplicationRecord
|
||||
belongs_to :operator, class_name: 'User', optional: true
|
||||
belongs_to :costumer, class_name: 'User', optional: true
|
||||
|
||||
has_one_attached :pdf, dependent: :destroy
|
||||
has_one_attached :pdf, dependent: :purge
|
||||
|
||||
validates_presence_of :costumer_firstname, :costumer_lastname, :privacy_policy_accepted, :pdf
|
||||
validate :acceptable_pdf
|
||||
@@ -11,6 +11,9 @@ class Job < ApplicationRecord
|
||||
before_save :update_paid_at, if: :will_save_change_to_status?
|
||||
before_save :update_status_changed_at, if: :will_save_change_to_status?
|
||||
|
||||
# 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: {
|
||||
open: 0,
|
||||
@@ -53,12 +56,10 @@ class Job < ApplicationRecord
|
||||
def acceptable_pdf
|
||||
return unless pdf.attached?
|
||||
|
||||
errors.add(:pdf, 'is too big') unless pdf.blob.byte_size <= 100.megabyte
|
||||
|
||||
acceptable_types = ['application/pdf']
|
||||
return if acceptable_types.include?(pdf.content_type)
|
||||
|
||||
errors.add(:pdf, 'must be a PDF')
|
||||
errors.add(:pdf, 'is too big') unless pdf.blob.byte_size <= 100.megabyte
|
||||
errors.add(:pdf, 'must be a PDF') unless acceptable_types.include?(pdf.content_type)
|
||||
end
|
||||
|
||||
def able_to_cancel?
|
||||
@@ -84,4 +85,29 @@ class Job < ApplicationRecord
|
||||
def update_status_changed_at
|
||||
self.status_changed_at = Time.now
|
||||
end
|
||||
|
||||
def analyze_pdf
|
||||
# return unless pdf.attached? && pdf.new_record?
|
||||
|
||||
# TODO: add any check if attachment has changed
|
||||
|
||||
# pdfs.each do |pdf|
|
||||
pdf.blob.open do |file|
|
||||
self.number_of_plans_a0 = 0
|
||||
self.number_of_plans_a1 = 0
|
||||
self.number_of_plans_a2 = 0
|
||||
self.number_of_plans_a3 = 0
|
||||
self.costum_qm_plan = 0
|
||||
# file = ActiveStorage::Blob.service.path_for(pdf.key).to_s
|
||||
pdf_analyzer = Services::PdfAnalyzer.new(file)
|
||||
pdf_analyzer.analyze
|
||||
self.number_of_plans_a0 += pdf_analyzer.pages_a0
|
||||
self.number_of_plans_a1 += pdf_analyzer.pages_a1
|
||||
self.number_of_plans_a2 += pdf_analyzer.pages_a2
|
||||
self.number_of_plans_a3 += pdf_analyzer.pages_a3
|
||||
self.costum_qm_plan += pdf_analyzer.costum_qm
|
||||
save
|
||||
end
|
||||
# end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -5,13 +5,16 @@
|
||||
<td class="p-3 text-sm text-hsrm-gray whitespace-nowrap"> <%= job.fullname %> </td>
|
||||
<td class="p-3 text-sm text-hsrm-gray whitespace-nowrap">
|
||||
<% if job.pdf.attached? %>
|
||||
<%= job.pdf.filename %><span class="p-1.5 bg-gray-300 font-medium rounded-lg ml-2"><%=number_to_human_size job.pdf.blob.byte_size%></span>
|
||||
<%#= link_to job.pdf.filename, rails_blob_path(job.pdf, disposition: "attachment") %>
|
||||
<%= link_to job.pdf.filename, job.pdf, download:true %>
|
||||
<span class="p-1.5 bg-gray-300 font-medium rounded-lg ml-2"><%=number_to_human_size job.pdf.blob.byte_size%></span>
|
||||
<% end %>
|
||||
</td>
|
||||
<td class="p-3 text-sm text-left text-hsrm-gray whitespace-nowrap"><span class="p-1.5 bg-gray-300 bg-opacity-50 font-medium rounded-lg"><%= job.number_of_plans_a0 %></span></td>
|
||||
<td class="p-3 text-sm text-left text-hsrm-gray whitespace-nowrap"><span class="p-1.5 bg-gray-300 bg-opacity-50 font-medium rounded-lg"><%= job.number_of_plans_a1 %></span></td>
|
||||
<td class="p-3 text-sm text-left text-hsrm-gray whitespace-nowrap"><span class="p-1.5 bg-gray-300 bg-opacity-50 font-medium rounded-lg"><%= job.number_of_plans_a2 %></span></td>
|
||||
<td class="p-3 text-sm text-left text-hsrm-gray whitespace-nowrap"><span class="p-1.5 bg-gray-300 bg-opacity-50 font-medium rounded-lg"><%= job.number_of_plans_a3 %></span></td>
|
||||
<td class="p-3 text-sm text-left text-hsrm-gray whitespace-nowrap"><span class="p-1.5 bg-gray-300 bg-opacity-50 font-medium rounded-lg"><%= job.costum_qm_plan.round(2) %></span></td>
|
||||
<td class="p-3 text-sm text-left text-hsrm-gray whitespace-nowrap">
|
||||
<span class="p-1.5 text-xs font-medium uppercase tracking-wider bg-opacity-50 text-status-<%= job.status.to_sym %> bg-status-<%= job.status %>-light rounded-lg"><%= job.status %></span>
|
||||
</td>
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
<th class="p-3 text-sm font-semibold tracking-wide text-left w-1"> A1 </th>
|
||||
<th class="p-3 text-sm font-semibold tracking-wide text-left w-1"> A2 </th>
|
||||
<th class="p-3 text-sm font-semibold tracking-wide text-left w-1"> A3 </th>
|
||||
<th class="p-3 text-sm font-semibold tracking-wide text-left w-1"> qm </th>
|
||||
<th class="p-3 text-sm font-semibold tracking-wide text-left w-1"> Status </th>
|
||||
<th class="p-3 text-sm font-semibold tracking-wide text-left w-1"> Actions </th>
|
||||
</tr>
|
||||
|
||||
Reference in New Issue
Block a user