From 31c63be66b624fe39040882f300bd0c68c30e3eb Mon Sep 17 00:00:00 2001 From: Jon Allured Date: Sun, 3 Nov 2024 12:19:44 -0600 Subject: [PATCH] Migrate pdf view to associated object --- app/models/daily_packet.rb | 8 ++++++++ .../pdf_view.rb} | 10 +++++----- app/models/daily_packet_service.rb | 6 ++---- .../pdf_view_spec.rb} | 4 ++-- spec/models/daily_packet_service_spec.rb | 4 ++-- 5 files changed, 19 insertions(+), 13 deletions(-) rename app/models/{daily_packet_pdf_view.rb => daily_packet/pdf_view.rb} (89%) rename spec/models/{daily_packet_pdf_view_spec.rb => daily_packet/pdf_view_spec.rb} (93%) diff --git a/app/models/daily_packet.rb b/app/models/daily_packet.rb index 8d250ef..963f945 100644 --- a/app/models/daily_packet.rb +++ b/app/models/daily_packet.rb @@ -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 @@ -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 diff --git a/app/models/daily_packet_pdf_view.rb b/app/models/daily_packet/pdf_view.rb similarity index 89% rename from app/models/daily_packet_pdf_view.rb rename to app/models/daily_packet/pdf_view.rb index 5e04301..88d5b34 100644 --- a/app/models/daily_packet_pdf_view.rb +++ b/app/models/daily_packet/pdf_view.rb @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/app/models/daily_packet_service.rb b/app/models/daily_packet_service.rb index 08ca82d..69f8b67 100644 --- a/app/models/daily_packet_service.rb +++ b/app/models/daily_packet_service.rb @@ -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 diff --git a/spec/models/daily_packet_pdf_view_spec.rb b/spec/models/daily_packet/pdf_view_spec.rb similarity index 93% rename from spec/models/daily_packet_pdf_view_spec.rb rename to spec/models/daily_packet/pdf_view_spec.rb index e74504e..ac2ecb0 100644 --- a/spec/models/daily_packet_pdf_view_spec.rb +++ b/spec/models/daily_packet/pdf_view_spec.rb @@ -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 diff --git a/spec/models/daily_packet_service_spec.rb b/spec/models/daily_packet_service_spec.rb index cd8af6c..6e67302 100644 --- a/spec/models/daily_packet_service_spec.rb +++ b/spec/models/daily_packet_service_spec.rb @@ -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) @@ -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",