Skip to content

Commit

Permalink
Merge pull request #655 from alphagov/unset-backend-for-redirects-and…
Browse files Browse the repository at this point in the history
…-gone

Unset the backend_id for redirects and gone routes
  • Loading branch information
theseanything authored Sep 2, 2024
2 parents 68364cb + 0634428 commit d77aee1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
5 changes: 5 additions & 0 deletions app/models/route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class Route
end

before_validation :default_segments_mode
before_save :remove_backend_id_for_non_backend_routes
after_create :cleanup_child_gone_routes

scope :excluding, ->(route) { where(id: { :$ne => route.id }) }
Expand Down Expand Up @@ -131,6 +132,10 @@ def validate_backend_id
end
end

def remove_backend_id_for_non_backend_routes
self.backend_id = nil unless backend?
end

def cleanup_child_gone_routes
return unless route_type == "prefix"

Expand Down
20 changes: 20 additions & 0 deletions spec/models/route_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,12 @@
end
end

describe "backend_id" do
it "is set to nil" do
expect(route.backend_id).to be nil
end
end

context "and segments_mode set to 'ignore'" do
subject(:route) { FactoryBot.build(:redirect_route, segments_mode: "ignore") }

Expand Down Expand Up @@ -249,6 +255,20 @@
end
end

describe "changing backend route to redirect route" do
it "will clear the backend_id" do
route = FactoryBot.create(:backend_route)
route.update!(
handler: "redirect",
redirect_to: "/",
redirect_type: "permanent",
)
route.reload

expect(route.backend_id).to be nil
end
end

describe "as_json" do
subject(:route) { FactoryBot.build(:redirect_route) }

Expand Down

0 comments on commit d77aee1

Please sign in to comment.