FIX: move total rows count & load more URL inside meta.

FIX: move total rows count & load more URL inside meta.

We’re fetching “total rows count” from root attributes only if meta object not found. https://github.com/discourse/discourse/blob/2b78bd01ab2431aa90b642326d170df7c51e492b/app/assets/javascripts/discourse/models/store.js#L236

diff --git a/app/controllers/directory_items_controller.rb b/app/controllers/directory_items_controller.rb
index 9acc1a2..d01d71b 100644
--- a/app/controllers/directory_items_controller.rb
+++ b/app/controllers/directory_items_controller.rb
@@ -81,8 +81,11 @@ class DirectoryItemsController < ApplicationController
 
     last_updated_at = DirectoryItem.last_updated_at(period_type)
     render_json_dump(directory_items: serialize_data(result, DirectoryItemSerializer),
-                     meta: { last_updated_at: last_updated_at },
-                     total_rows_directory_items: result_count,
-                     load_more_directory_items: directory_items_path(more_params))
+                     meta: {
+                        last_updated_at: last_updated_at,
+                        total_rows_directory_items: result_count,
+                        load_more_directory_items: directory_items_path(more_params)
+                      }
+                    )
   end
 end
diff --git a/spec/requests/directory_items_controller_spec.rb b/spec/requests/directory_items_controller_spec.rb
index 8dc0cca..31f416f 100644
--- a/spec/requests/directory_items_controller_spec.rb
+++ b/spec/requests/directory_items_controller_spec.rb
@@ -44,12 +44,12 @@ describe DirectoryItemsController do
 
       expect(json).to be_present
       expect(json['directory_items']).to be_present
-      expect(json['total_rows_directory_items']).to be_present
-      expect(json['load_more_directory_items']).to be_present
+      expect(json['meta']['total_rows_directory_items']).to be_present
+      expect(json['meta']['load_more_directory_items']).to be_present
       expect(json['meta']['last_updated_at']).to be_present
 
       expect(json['directory_items'].length).to eq(4)
-      expect(json['total_rows_directory_items']).to eq(4)
+      expect(json['meta']['total_rows_directory_items']).to eq(4)
     end
 
     it "fails when the directory is disabled" do
@@ -66,7 +66,7 @@ describe DirectoryItemsController do
       json = ::JSON.parse(response.body)
       expect(json).to be_present
       expect(json['directory_items'].length).to eq(1)
-      expect(json['total_rows_directory_items']).to eq(1)
+      expect(json['meta']['total_rows_directory_items']).to eq(1)
       expect(json['directory_items'][0]['user']['username']).to eq('eviltrout')
     end
 
@@ -77,7 +77,7 @@ describe DirectoryItemsController do
       json = ::JSON.parse(response.body)
       expect(json).to be_present
       expect(json['directory_items'].length).to eq(1)
-      expect(json['total_rows_directory_items']).to eq(1)
+      expect(json['meta']['total_rows_directory_items']).to eq(1)
       expect(json['directory_items'][0]['user']['username']).to eq('stage_user')
     end
 
@@ -88,7 +88,7 @@ describe DirectoryItemsController do
       json = ::JSON.parse(response.body)
       expect(json).to be_present
       expect(json['directory_items'].length).to eq(2)
-      expect(json['total_rows_directory_items']).to eq(2)
+      expect(json['meta']['total_rows_directory_items']).to eq(2)
       expect(json['directory_items'][0]['user']['username']).to eq(walter_white.username) | eq(user.username)
       expect(json['directory_items'][1]['user']['username']).to eq(walter_white.username) | eq(user.username)
     end
@@ -100,7 +100,7 @@ describe DirectoryItemsController do
       json = ::JSON.parse(response.body)
       expect(json).to be_present
       expect(json['directory_items'].length).to eq(2)
-      expect(json['total_rows_directory_items']).to eq(2)
+      expect(json['meta']['total_rows_directory_items']).to eq(2)
       expect(json['directory_items'][0]['user']['username']).to eq(evil_trout.username) | eq(stage_user.username)
       expect(json['directory_items'][1]['user']['username']).to eq(evil_trout.username) | eq(stage_user.username)
     end
diff --git a/test/javascripts/fixtures/directory-fixtures.js b/test/javascripts/fixtures/directory-fixtures.js
index ab1b647..dab359e 100644
--- a/test/javascripts/fixtures/directory-fixtures.js
+++ b/test/javascripts/fixtures/directory-fixtures.js
@@ -602,8 +602,11 @@ export default {
         post_count: 146
       }
     ],
-    total_rows_directory_items: 12546,
-    load_more_directory_items:
-      "/directory_items?id=all&order=likes_received&page=1"
+    meta: {
+      last_updated_at: "2020-04-02T02:19:20.000Z",
+      total_rows_directory_items: 12546,
+      load_more_directory_items:
+        "/directory_items?id=all&order=likes_received&page=1"
+    }
   }
 };

GitHub sha: 2f9879a9

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

This is probably my fault - thanks for the fix!

1 Like

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

https://meta.discourse.org/t/changed-api-for-directory-items/150669/1

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