Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion app/models/change_note.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
class ChangeNote < ApplicationRecord
belongs_to :edition, optional: true
belongs_to :edition
belongs_to :document

before_validation :set_columns_from_edition

def self.create_from_edition(payload, edition)
ChangeNoteFactory.new(payload, edition).build
end

private

def set_columns_from_edition
if edition.nil?
errors.add(:edition, "must exist")
raise_validation_error
end

self.document_id = edition.document_id
self.user_facing_version = edition.user_facing_version
end
end

class ChangeNoteFactory
Expand Down
3 changes: 1 addition & 2 deletions app/presenters/queries/change_history.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ def notes_for_edition_and_linked_content_blocks

def change_notes_for_edition
ChangeNote
.joins(:edition)
.where(editions: { document: edition.document })
.where(document_id: edition.document_id)
.where("user_facing_version <= ?", edition.user_facing_version)
.where.not(public_timestamp: nil)
end
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20251128170548_add_document_id_to_change_notes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddDocumentIdToChangeNotes < ActiveRecord::Migration[8.0]
def change
add_column :change_notes, :document_id, :integer
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddUserFacingVersionToChangeNotes < ActiveRecord::Migration[8.0]
def change
add_column :change_notes, :user_facing_version, :integer
end
end
11 changes: 11 additions & 0 deletions db/migrate/20251128172013_add_index_on_change_notes_document_id.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class AddIndexOnChangeNotesDocumentId < ActiveRecord::Migration[8.0]
disable_ddl_transaction!

def change
add_index :change_notes,
:document_id,
where: "public_timestamp IS NOT NULL",
algorithm: :concurrently,
name: "idx_change_notes_document_id_partial"
end
end
5 changes: 4 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[8.0].define(version: 2025_11_21_132638) do
ActiveRecord::Schema[8.0].define(version: 2025_11_28_172013) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_catalog.plpgsql"

Expand Down Expand Up @@ -41,6 +41,9 @@
t.integer "edition_id", null: false
t.datetime "created_at", precision: nil
t.datetime "updated_at", precision: nil
t.integer "document_id"
t.integer "user_facing_version"
t.index ["document_id"], name: "idx_change_notes_document_id_partial", where: "(public_timestamp IS NOT NULL)"
t.index ["edition_id"], name: "index_change_notes_on_edition_id"
end

Expand Down
16 changes: 16 additions & 0 deletions spec/models/change_note_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@
)
end

describe "#save!" do
it "won't save without an edition" do
change_note = described_class.new
expect { change_note.save! }.to raise_error(ActiveRecord::RecordInvalid, /Edition must exist/)
end

it "sets document_id and user_facing_version from edition" do
change_note = described_class.new
change_note.edition = edition
change_note.save!
change_note.reload
expect(change_note.document_id).to eq(edition.document_id)
expect(change_note.user_facing_version).to eq(edition.user_facing_version)
end
end

describe ".create_from_edition" do
subject { described_class.create_from_edition(payload, edition) }

Expand Down