From b69909ef1214818354bb52ba9f0f340d31470766 Mon Sep 17 00:00:00 2001 From: Jon Allured Date: Wed, 6 Nov 2024 16:15:26 -0600 Subject: [PATCH] Add edition number to daily packets --- app/models/daily_packet.rb | 7 +++++- app/models/daily_packet/builder.rb | 1 + ...146_add_edition_number_to_daily_packets.rb | 5 ++++ spec/factories/daily_packet.rb | 1 + spec/models/daily_packet/pdf_view_spec.rb | 2 +- spec/models/daily_packet_spec.rb | 25 +++++++++++++++++++ 6 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20241106215146_add_edition_number_to_daily_packets.rb diff --git a/app/models/daily_packet.rb b/app/models/daily_packet.rb index 1c7d611..b2809bb 100644 --- a/app/models/daily_packet.rb +++ b/app/models/daily_packet.rb @@ -8,10 +8,15 @@ class DailyPacket < ApplicationRecord delegate :save_to_disk, :save_to_s3, to: :producer validates :built_on, presence: true + validates :edition_number, presence: true validates :feedbin_oldest_ago, presence: true validates :feedbin_unread_count, presence: true validates :reading_list_pace, presence: true + def self.next_edition_number + (DailyPacket.maximum(:edition_number) || 0) + 1 + end + def built_on_phrase "#{built_on.to_fs}\nweek #{built_on.cweek}" end @@ -25,7 +30,7 @@ def feedbin_unread_phrase end def headline_phrase - "Daily Packet ##{id}" + "Daily Packet ##{edition_number}" end def reading_list_phrase diff --git a/app/models/daily_packet/builder.rb b/app/models/daily_packet/builder.rb index 30e1cb7..e6011f6 100644 --- a/app/models/daily_packet/builder.rb +++ b/app/models/daily_packet/builder.rb @@ -8,6 +8,7 @@ def self.find_or_build_for(built_on) warm_fuzzy = WarmFuzzy.random attributes = { + edition_number: DailyPacket.next_edition_number, feedbin_oldest_ago: feedbin_oldest_ago, feedbin_unread_count: feedbin_unread_count, reading_list_pace: reading_list.pace, diff --git a/db/migrate/20241106215146_add_edition_number_to_daily_packets.rb b/db/migrate/20241106215146_add_edition_number_to_daily_packets.rb new file mode 100644 index 0000000..2f858de --- /dev/null +++ b/db/migrate/20241106215146_add_edition_number_to_daily_packets.rb @@ -0,0 +1,5 @@ +class AddEditionNumberToDailyPackets < ActiveRecord::Migration[7.2] + def change + add_column :daily_packets, :edition_number, :integer, null: false, default: 0 + end +end diff --git a/spec/factories/daily_packet.rb b/spec/factories/daily_packet.rb index 5fb8628..bbe25f4 100644 --- a/spec/factories/daily_packet.rb +++ b/spec/factories/daily_packet.rb @@ -1,6 +1,7 @@ FactoryBot.define do factory :daily_packet do built_on { Date.parse("2007-07-07") } + edition_number { 19 } feedbin_oldest_ago { 14 } feedbin_unread_count { 9 } reading_list_pace { 7.7 } diff --git a/spec/models/daily_packet/pdf_view_spec.rb b/spec/models/daily_packet/pdf_view_spec.rb index 60cf933..01de571 100644 --- a/spec/models/daily_packet/pdf_view_spec.rb +++ b/spec/models/daily_packet/pdf_view_spec.rb @@ -15,7 +15,7 @@ page_one_strings, page_two_strings, page_three_strings = inspector.pages.map { |page| page[:strings] } expect(page_one_strings).to eq([ - "DAILY PACKET ##{daily_packet.id}", + "DAILY PACKET #19", "11/05/2024", "week 45", "Random Warm Fuzzy", diff --git a/spec/models/daily_packet_spec.rb b/spec/models/daily_packet_spec.rb index 0d25bca..0c8816e 100644 --- a/spec/models/daily_packet_spec.rb +++ b/spec/models/daily_packet_spec.rb @@ -24,4 +24,29 @@ expect(daily_packet).to be_valid end end + + describe ".next_edition_number" do + context "with no DailyPacket records" do + it "returns 1" do + expect(DailyPacket.next_edition_number).to eq 1 + end + end + + context "with a DailyPacket record" do + it "returns the edition number after that record's value" do + FactoryBot.create(:daily_packet, edition_number: 7) + expect(DailyPacket.next_edition_number).to eq 8 + end + end + + context "with a few DailyPacket records" do + it "returns the edition number after the record with the highest value" do + FactoryBot.create(:daily_packet, edition_number: 1) + FactoryBot.create(:daily_packet, edition_number: 7) + FactoryBot.create(:daily_packet, edition_number: 3) + + expect(DailyPacket.next_edition_number).to eq 8 + end + end + end end