Skip to content

Commit

Permalink
Migrate pdf view to associated object
Browse files Browse the repository at this point in the history
  • Loading branch information
jonallured committed Nov 3, 2024
1 parent 161e92c commit 31c63be
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 13 deletions.
8 changes: 8 additions & 0 deletions app/models/daily_packet.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
class DailyPacket < ApplicationRecord
belongs_to :warm_fuzzy

has_object :pdf_view

delegate :pdf_data, to: :pdf_view

validates :built_on, presence: true
validates :reading_list_pace, presence: true

Expand All @@ -19,4 +23,8 @@ def local_path
def s3_key
"daily-packets/#{built_on.strftime("%Y-%m-%d")}.pdf"
end

def save_locally
pdf_view.save_as(local_path)
end
end
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
class DailyPacketPdfView
class DailyPacket::PdfView < ActiveRecord::AssociatedObject
include Prawn::View

def initialize(daily_packet)
@daily_packet = daily_packet
super
build
end

Expand All @@ -26,7 +26,7 @@ def draw_front_page

move_up 34

text @daily_packet.built_on_phrase, align: :right, size: 18
text daily_packet.built_on_phrase, align: :right, size: 18

stroke do
horizontal_rule
Expand All @@ -37,7 +37,7 @@ def draw_front_page
column_box([0, cursor], columns: 2, width: bounds.width) do
text "Random Warm Fuzzy", style: :bold, size: 20

warm_fuzzy = @daily_packet.warm_fuzzy
warm_fuzzy = daily_packet.warm_fuzzy
text warm_fuzzy.title, size: 16
text warm_fuzzy.body, size: 12
text "\n- #{warm_fuzzy.author}, #{warm_fuzzy.received_at.to_fs}", size: 12, align: :right
Expand All @@ -46,7 +46,7 @@ def draw_front_page

font_size(20) do
text "Reading Pace", style: :bold
text @daily_packet.reading_list_phrase
text daily_packet.reading_list_phrase
end
end
end
Expand Down
6 changes: 2 additions & 4 deletions app/models/daily_packet_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ def self.build_for(date)

def self.save_locally(date = Date.today)
daily_packet = DailyPacketService.build_for(date)
packet = DailyPacketPdfView.new(daily_packet)
packet.save_as(daily_packet.local_path)
daily_packet.save_locally
end

def self.save_to_s3(date = Date.today)
daily_packet = DailyPacketService.build_for(date)
packet = DailyPacketPdfView.new(daily_packet)
S3Api.write(daily_packet.s3_key, packet.pdf_data)
S3Api.write(daily_packet.s3_key, daily_packet.pdf_data)
end
end
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
require "rails_helper"

describe DailyPacketPdfView do
describe DailyPacket::PdfView do
it "renders the document" do
warm_fuzzy = FactoryBot.create(:warm_fuzzy, received_at: Time.at(0))
daily_packet = FactoryBot.create(:daily_packet, warm_fuzzy: warm_fuzzy)
view = DailyPacketPdfView.new(daily_packet)
view = DailyPacket::PdfView.new(daily_packet)
inspector = PDF::Inspector::Page.analyze(view.pdf_data)

expect(inspector.pages.size).to eq 3
Expand Down
4 changes: 2 additions & 2 deletions spec/models/daily_packet_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
describe ".save_locally" do
it "renders and writes the pdf data locally" do
mock_view = double(:mock_view)
expect(DailyPacketPdfView).to receive(:new).and_return(mock_view)
expect(DailyPacket::PdfView).to receive(:new).and_return(mock_view)

expect(mock_view).to receive(:save_as).with("tmp/daily_packet.pdf").and_return(nil)

Expand All @@ -16,7 +16,7 @@
describe ".save_to_s3" do
it "renders and writes the pdf data to s3" do
mock_view = double(:mock_view, pdf_data: "PDF GOES HERE")
expect(DailyPacketPdfView).to receive(:new).and_return(mock_view)
expect(DailyPacket::PdfView).to receive(:new).and_return(mock_view)

expect(S3Api).to receive(:write).with(
"daily-packets/2001-02-03.pdf",
Expand Down

0 comments on commit 31c63be

Please sign in to comment.