Skip to content

Commit

Permalink
Make FieldSlipJobTrackers more independent of Projects
Browse files Browse the repository at this point in the history
  • Loading branch information
mo-nathan committed Apr 20, 2024
1 parent 2147f19 commit 9b6e5c0
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 16 deletions.
5 changes: 2 additions & 3 deletions app/classes/field_slip_view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ class FieldSlipView
PHOTO_RIGHT = 1.75.cm
PHOTO_BOX_SIZE = 4.mm

def initialize(title, tracker, logo)
@title = title
def initialize(tracker, logo)
@tracker = tracker
@logo = logo
end
Expand Down Expand Up @@ -202,7 +201,7 @@ def qr_code(code)
svg(qr_svg("http://mushroomobserver.org/qr/#{code}"), at: [QR_LEFT, QR_TOP],
width: QR_SIZE)
font("#{Prawn::ManualBuilder::DATADIR}/fonts/DejaVuSans.ttf") do
text_box("#{@title} Field Slip:",
text_box("#{@tracker.title} Field Slip:",
at: [QR_RIGHT + QR_MARGIN, QR_TOP],
height: FONT_SIZE,
width: title_width,
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/projects/field_slips_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ def create

tracker = FieldSlipJobTracker.create(prefix: @project.field_slip_prefix,
start: @project.next_field_slip,
title: @project.title,
count: 6 * pages)
if tracker
@project.next_field_slip = tracker.last + 1
if @project.save
FieldSlipJob.perform_later(@project.id, tracker.id)
FieldSlipJob.perform_later(tracker.id)
flash_notice(:field_slips_created_job.t(filename: tracker.filename))
else
tracker.destroy
Expand Down
13 changes: 5 additions & 8 deletions app/jobs/field_slip_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,19 @@
class FieldSlipJob < ApplicationJob
queue_as :default

def perform(project_id, tracker_id)
log("Starting FieldSlipJob.perform(#{project_id}, #{tracker_id})")
def perform(tracker_id)
log("Starting FieldSlipJob.perform(#{tracker_id})")
cleanup_old_pdfs(tracker_id)
project = Project.find(project_id)
raise(:field_slip_job_no_project.t(id: project_id)) unless project

tracker = FieldSlipJobTracker.find(tracker_id)
raise(:field_slip_job_no_tracker.t(id: tracker_id)) unless tracker

tracker.processing
icon = "public/logo-120.png" # Will be replaced with project.logo
view = FieldSlipView.new(project.title, tracker, icon)
icon = "public/logo-120.png"
view = FieldSlipView.new(tracker, icon)
view.render
view.save_as(tracker.filepath)
tracker.done
log("Done with FieldSlipJob.perform(#{project_id}, #{tracker_id})")
log("Done with FieldSlipJob.perform(#{tracker_id})")
tracker.filepath
end

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

class AddTitleToFieldSlipJobTrackers < ActiveRecord::Migration[7.1]
def change
add_column(:field_slip_job_trackers, :title, :string,
limit: 100, default: "", null: false)
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.1].define(version: 2024_04_19_201108) do
ActiveRecord::Schema[7.1].define(version: 2024_04_20_135421) do
create_table "api_keys", id: :integer, charset: "utf8mb3", force: :cascade do |t|
t.datetime "created_at", precision: nil
t.datetime "last_used", precision: nil
Expand Down Expand Up @@ -93,6 +93,7 @@
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "pages", default: 0, null: false
t.string "title", limit: 100, default: "", null: false
end

create_table "field_slips", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/field_slip_job_trackers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@ fsjt_page_one:
start: 0
count: 6
prefix: EOL
title: EOL Project
status: <%= FieldSlipJobTracker.statuses[:Done] %>

fsjt_page_two:
start: 6
count: 12
prefix: EOL
title: EOL Project
status: <%= FieldSlipJobTracker.statuses[:Starting] %>

fsjt_old:
start: 6
count: 12
prefix: OLD
title: Old Project
status: <%= FieldSlipJobTracker.statuses[:Starting] %>
updated_at: 2023-04-14 16:31:00
6 changes: 3 additions & 3 deletions test/jobs/field_slip_job_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class FieldSlipJobTest < ActiveJob::TestCase
test "it should perform" do
job = FieldSlipJob.new
tracker = field_slip_job_trackers(:fsjt_page_two)
job.perform(projects(:eol_project).id, tracker.id)
job.perform(tracker.id)
assert(File.exist?(tracker.filepath))
File.delete(tracker.filepath)
end
Expand All @@ -15,7 +15,7 @@ class FieldSlipJobTest < ActiveJob::TestCase
old_tracker_id = field_slip_job_trackers(:fsjt_old).id
job = FieldSlipJob.new
tracker = field_slip_job_trackers(:fsjt_page_two)
job.perform(projects(:eol_project).id, tracker.id)
job.perform(tracker.id)
File.delete(tracker.filepath)
assert_nil(FieldSlipJobTracker.find_by(id: old_tracker_id))
end
Expand All @@ -25,7 +25,7 @@ class FieldSlipJobTest < ActiveJob::TestCase
FileUtils.touch(old_filepath)
job = FieldSlipJob.new
tracker = field_slip_job_trackers(:fsjt_page_two)
job.perform(projects(:eol_project).id, tracker.id)
job.perform(tracker.id)
File.delete(tracker.filepath)
assert_not(File.exist?(old_filepath))
end
Expand Down

0 comments on commit 9b6e5c0

Please sign in to comment.