Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Part validation behaviour after upgrading Mongo version #1862

Merged
merged 1 commit into from
Mar 28, 2024
Merged
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
5 changes: 5 additions & 0 deletions app/models/travel_advice_edition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class TravelAdviceEdition
validates :version_number, presence: true, uniqueness: { scope: :country_slug }
validate :alert_status_contains_valid_values
validate :first_version_cant_be_minor_update
validate :parts_valid?
validates_with SafeHtml
validates_with LinkValidator

Expand Down Expand Up @@ -249,6 +250,10 @@ def first_version_cant_be_minor_update
end
end

def parts_valid?
parts.map(&:valid?).all?
end

def validate_scheduled_publication_time
errors.add(:scheduled_publication_time, "can't be in the past") if scheduled_publication_time && scheduled_publication_time <= Time.zone.now
end
Expand Down
38 changes: 27 additions & 11 deletions spec/models/travel_advice_edition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -569,24 +569,40 @@
end

context "parts" do
it "should merge part validation errors with parent document's errors" do
it "should merge part validation errors with parent document's errors when parts are created as nested attributes" do
edition = build(:travel_advice_edition,
parts: [
{ _id: "54c10d4d759b743528000010", order: "1", title: "", slug: "part-1" },
{ _id: "54c10d4d759b743528000011", order: "3", title: "Part 3", slug: "" },
{ _id: "54c10d4d759b743528000012", order: "2", title: "Part 2", slug: "part-2" },
])

expect(edition).not_to be_valid

expect(edition.errors[:part]).to eq(["1: Title can't be blank and 3: Slug can't be blank and Slug is invalid"])
end

it "should merge part validation errors with parent document's errors when parts are created as built parts" do
edition = create(:travel_advice_edition)
edition.parts = [
Part.new(_id: "54c10d4d759b743528000010", order: "1", title: "", slug: "overview"),
Part.new(_id: "54c10d4d759b743528000011", order: "2", title: "Prepare for your appointment", slug: ""),
Part.new(_id: "54c10d4d759b743528000012", order: "3", title: "Valid", slug: "valid"),
]
edition.parts.build(_id: "54c10d4d759b743528000010", order: "1", title: "", slug: "part-1")
edition.parts.build(_id: "54c10d4d759b743528000011", order: "3", title: "Part 3", slug: "")
edition.parts.build(_id: "54c10d4d759b743528000012", order: "2", title: "Part 2", slug: "part-2")

expect(edition).not_to be_valid

expect(edition.errors[:part]).to eq(["1: Title can't be blank and 2: Slug can't be blank and Slug is invalid"])
expect(edition.errors[:part]).to eq(["1: Title can't be blank and 3: Slug can't be blank and Slug is invalid"])
end

it "#whole_body returns ordered parts" do
edition = create(:travel_advice_edition)
edition.parts.build(_id: "54c10d4d759b743528000010", order: "1", title: "Part 1", slug: "part_1")
edition.parts.build(_id: "54c10d4d759b743528000011", order: "3", title: "Part 3", slug: "part_3")
edition.parts.build(_id: "54c10d4d759b743528000012", order: "2", title: "Part 2", slug: "part_2")
edition = create(:travel_advice_edition,
parts: [
{ _id: "54c10d4d759b743528000010", order: "1", title: "Part 1", slug: "part-1" },
{ _id: "54c10d4d759b743528000011", order: "3", title: "Part 3", slug: "part-3" },
{ _id: "54c10d4d759b743528000012", order: "2", title: "Part 2", slug: "part-2" },
])

expect(edition).to be_valid

expect(edition.whole_body).to eq("# Part 1\n\n\n\n# Part 2\n\n\n\n# Part 3\n\n")
end
end
Expand Down
Loading