FIX: Ensure load more directory items has a .json url

FIX: Ensure load more directory items has a .json url

The /directory_items route needs to have a .json url, but the rails url helper _path doesn’t return the format of the route.

I tried passing in a format options to directory_items_path. Which works in the rails console

[8] pry(main)> directory_items_path(params.merge(:format => :json))
=> "/directory_items.json?page=1"

but when I added that some logic to the controller it comes out as

/directory_items?format=json&page=1

(which is actually how I expect it to work based on how you pass in the format param). Anyways, because I couldn’t figure out how to pass a format to the _path helper I just used URI.parse to append .json manually.

diff --git a/app/controllers/directory_items_controller.rb b/app/controllers/directory_items_controller.rb
index d01d71b..a0af52a 100644
--- a/app/controllers/directory_items_controller.rb
+++ b/app/controllers/directory_items_controller.rb
@@ -65,6 +65,8 @@ class DirectoryItemsController < ApplicationController
 
     more_params = params.slice(:period, :order, :asc).permit!
     more_params[:page] = page + 1
+    load_more_uri = URI.parse(directory_items_path(more_params))
+    load_more_directory_items_json = "#{load_more_uri.path}.json?#{load_more_uri.query}"
 
     # Put yourself at the top of the first page
     if result.present? && current_user.present? && page == 0
@@ -84,7 +86,7 @@ class DirectoryItemsController < ApplicationController
                      meta: {
                         last_updated_at: last_updated_at,
                         total_rows_directory_items: result_count,
-                        load_more_directory_items: directory_items_path(more_params)
+                        load_more_directory_items: load_more_directory_items_json
                       }
                     )
   end
diff --git a/spec/requests/directory_items_controller_spec.rb b/spec/requests/directory_items_controller_spec.rb
index 73a76e5..80817ef 100644
--- a/spec/requests/directory_items_controller_spec.rb
+++ b/spec/requests/directory_items_controller_spec.rb
@@ -50,6 +50,7 @@ describe DirectoryItemsController do
 
       expect(json['directory_items'].length).to eq(4)
       expect(json['meta']['total_rows_directory_items']).to eq(4)
+      expect(json['meta']['load_more_directory_items']).to include('.json')
     end
 
     it "fails when the directory is disabled" do

GitHub sha: ee366f7a

This commit has been mentioned on Discourse Meta. There might be relevant details there:

https://meta.discourse.org/t/potential-api-bug-get-a-public-list-of-users-endpoint/160378/6