-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate sitemaps with Controller and Actions instead of sitemap_gene…
…rator (#268) Related to #265 Implement sitemap generation with a primary index referencing a per-city index, and the per-city index listing out a sitemap per-city-per-day containing all service requests for that day.
- Loading branch information
1 parent
1a99f74
commit 265d959
Showing
17 changed files
with
142 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# frozen_string_literal: true | ||
|
||
class SitemapsController < ApplicationController | ||
def index | ||
@cities = City.all | ||
|
||
respond_to do |format| | ||
format.xml | ||
end | ||
end | ||
|
||
def city_index | ||
@city = City.find_by!(slug: params[:slug]) | ||
|
||
oldest_service_request = @city.service_requests.order(:created_at, :id).limit(1).pick(:created_at)&.to_date || Date.current | ||
@dates = oldest_service_request..Date.current | ||
|
||
respond_to do |format| | ||
format.xml | ||
end | ||
end | ||
|
||
def city_day | ||
@city = City.find_by!(slug: params[:slug]) | ||
@date = Date.parse(params[:date]) | ||
@service_requests = @city.service_requests.where(requested_datetime: @date.all_day) | ||
|
||
respond_to do |format| | ||
format.xml | ||
end | ||
end | ||
|
||
def static | ||
@links = [ | ||
root_url, | ||
about_url, | ||
] + City.all.map { |city| city_url(city) } | ||
|
||
respond_to do |format| | ||
format.xml | ||
end | ||
end | ||
|
||
private | ||
|
||
def format_lastmod(updated) | ||
updated.utc.strftime("%Y-%m-%d") | ||
end | ||
helper_method :format_lastmod | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# frozen_string_literal: true | ||
xml.instruct! :xml, version: "1.0", encoding: "UTF-8" | ||
xml.urlset xmlns: "http://www.sitemaps.org/schemas/sitemap/0.9" do | ||
@service_requests.each do |service_request| | ||
xml.url do | ||
xml.loc service_request_url(service_request, format: :xml) | ||
xml.lastmod format_lastmod(service_request.updated_at) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# frozen_string_literal: true | ||
xml.instruct! :xml, version: "1.0", encoding: "UTF-8" | ||
xml.sitemapindex xmlns: "http://www.sitemaps.org/schemas/sitemap/0.9" do | ||
@dates.each do |date| | ||
xml.sitemap do | ||
xml.loc city_day_sitemap_url(@city.slug, date.strftime("%Y-%m-%d"), format: :xml) | ||
xml.lastmod date.strftime("%Y-%m-%dT%H:%M:%S%:z") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# frozen_string_literal: true | ||
xml.instruct! :xml, version: "1.0", encoding: "UTF-8" | ||
xml.sitemapindex xmlns: "http://www.sitemaps.org/schemas/sitemap/0.9" do | ||
xml.sitemap do | ||
xml.loc static_sitemap_url(format: :xml) | ||
end | ||
|
||
@cities.each do |city| | ||
xml.sitemap do | ||
xml.loc city_sitemap_index_url(city.slug, format: :xml) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
xml.instruct! :xml, version: "1.0", encoding: "UTF-8" | ||
xml.urlset xmlns: "http://www.sitemaps.org/schemas/sitemap/0.9" do | ||
@links.each do |link| | ||
xml.url do | ||
xml.loc link | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
7 changes: 7 additions & 0 deletions
7
db/migrate/20240730192533_add_order_index_to_service_requests.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class AddOrderIndexToServiceRequests < ActiveRecord::Migration[7.1] | ||
disable_ddl_transaction! | ||
|
||
def change | ||
add_index :service_requests, [:city_id, :created_at, :id], algorithm: :concurrently | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# frozen_string_literal: true | ||
require 'rails_helper' | ||
|
||
RSpec.describe 'Sitemaps' do | ||
let(:city) { City.first } | ||
let(:service_request) { create(:service_request, city:) } | ||
|
||
before do | ||
City.load! | ||
end | ||
|
||
it 'visits the primary sitemap index' do | ||
visit '/sitemap.xml' | ||
expect(page).to have_content static_sitemap_url(format: :xml) | ||
expect(page).to have_content city_sitemap_index_url(City.first.slug, format: :xml) | ||
end | ||
|
||
it 'visits the per-city index' do | ||
visit "/sitemaps/#{city.slug}.xml" | ||
expect(page).to have_content city_day_sitemap_url(city.slug, date: Date.current, format: :xml) | ||
end | ||
|
||
it 'visits the per-city-per-day sitemap' do | ||
visit "/sitemaps/#{city.slug}/#{service_request.requested_datetime.to_date}.xml" | ||
expect(page).to have_content(service_request_url(service_request)) | ||
end | ||
|
||
it 'visits the static sitemap' do | ||
visit '/sitemaps/static.xml' | ||
expect(page).to have_content(root_url) | ||
end | ||
end |