]> cat aescling's git repositories - mastodon.git/commitdiff
Make Array-creation behavior of Paginable more predictable (#14687)
authorAkihiko Odaki <nekomanma@pixiv.co.jp>
Mon, 31 Aug 2020 10:47:09 +0000 (19:47 +0900)
committerGitHub <noreply@github.com>
Mon, 31 Aug 2020 10:47:09 +0000 (12:47 +0200)
* Make Array-creation behavior of Paginable more predictable

Paginable.paginate_by_id usually returns ActiveRecord::Relation, but it
returns an Array if min_id option is present. The behavior caused problems
fixed with the following commits:
552e886b648faa2a2229d86c7fd9abc8bb5ff99c
b63ede5005d33b52266650ec716d345f166e2df0
64ef37b89de806f49cc59e011aa0ee2039c82c46

To prevent from recurring similar problems, this commit introduces two
changes:
- The scope now always returns an Array whether min_id option is present
  or not.
- The scope is renamed to to_a_paginated_by_id to clarify it returns an
  Array.

* Transform Paginable.to_a_paginated_by_id from a scope to a class method

https://api.rubyonrails.org/classes/ActiveRecord/Scoping/Named/ClassMethods.html#method-i-scope
> The method is intended to return an ActiveRecord::Relation object, which
> is composable with other scopes.

Paginable.to_a_paginated_by_id returns an Array and is not appropriate
as a scope.

app/controllers/api/v1/admin/accounts_controller.rb
app/controllers/api/v1/admin/reports_controller.rb
app/controllers/api/v1/bookmarks_controller.rb
app/controllers/api/v1/conversations_controller.rb
app/controllers/api/v1/crypto/encrypted_messages_controller.rb
app/controllers/api/v1/favourites_controller.rb
app/controllers/api/v1/scheduled_statuses_controller.rb
app/controllers/concerns/cache_concern.rb
app/models/account_conversation.rb
app/models/concerns/paginable.rb

index c35ea5ab254ffe77f33f193ec9febb3818fae72b..24c7fbef12ff0b8968cee847eeaa5d76be46dbc8 100644 (file)
@@ -79,7 +79,7 @@ class Api::V1::Admin::AccountsController < Api::BaseController
   private
 
   def set_accounts
-    @accounts = filtered_accounts.order(id: :desc).includes(user: [:invite_request, :invite]).paginate_by_id(limit_param(LIMIT), params_slice(:max_id, :since_id, :min_id))
+    @accounts = filtered_accounts.order(id: :desc).includes(user: [:invite_request, :invite]).to_a_paginated_by_id(limit_param(LIMIT), params_slice(:max_id, :since_id, :min_id))
   end
 
   def set_account
index 1d48d3160fc7b28e54bf5f1932ddd53ef3ef1aac..c8f4cd8d80c828cf94e32df107720a81733ab19a 100644 (file)
@@ -63,7 +63,7 @@ class Api::V1::Admin::ReportsController < Api::BaseController
   private
 
   def set_reports
-    @reports = filtered_reports.order(id: :desc).with_accounts.paginate_by_id(limit_param(LIMIT), params_slice(:max_id, :since_id, :min_id))
+    @reports = filtered_reports.order(id: :desc).with_accounts.to_a_paginated_by_id(limit_param(LIMIT), params_slice(:max_id, :since_id, :min_id))
   end
 
   def set_report
index 5c72f4a1a58c9eac9a24478aa5914b047796877c..aa3fb88f08f19267b896dbb8d9b196f8699a9c4e 100644 (file)
@@ -21,7 +21,7 @@ class Api::V1::BookmarksController < Api::BaseController
   end
 
   def results
-    @_results ||= account_bookmarks.eager_load(:status).paginate_by_id(
+    @_results ||= account_bookmarks.eager_load(:status).to_a_paginated_by_id(
       limit_param(DEFAULT_STATUSES_LIMIT),
       params_slice(:max_id, :since_id, :min_id)
     )
index bc801337945d6d1019caad248a5518317a36f5b5..6c7583403758e2e2877b58c563a0a5ed60fd675f 100644 (file)
@@ -32,7 +32,7 @@ class Api::V1::ConversationsController < Api::BaseController
 
   def paginated_conversations
     AccountConversation.where(account: current_account)
-                       .paginate_by_id(limit_param(LIMIT), params_slice(:max_id, :since_id, :min_id))
+                       .to_a_paginated_by_id(limit_param(LIMIT), params_slice(:max_id, :since_id, :min_id))
   end
 
   def insert_pagination_headers
index c764915e578e19dbd249fdcf503b7c8827079e6a..68cf4384f79bd0c5d2f0389f67f021b4a3628146 100644 (file)
@@ -26,7 +26,7 @@ class Api::V1::Crypto::EncryptedMessagesController < Api::BaseController
   end
 
   def set_encrypted_messages
-    @encrypted_messages = @current_device.encrypted_messages.paginate_by_id(limit_param(LIMIT), params_slice(:max_id, :since_id, :min_id))
+    @encrypted_messages = @current_device.encrypted_messages.to_a_paginated_by_id(limit_param(LIMIT), params_slice(:max_id, :since_id, :min_id))
   end
 
   def insert_pagination_headers
index 71a707d2a500dd124e63a606e9e8e0a121d27563..21836bc170cbbc7915cd188c84e90e2163ab83e0 100644 (file)
@@ -21,7 +21,7 @@ class Api::V1::FavouritesController < Api::BaseController
   end
 
   def results
-    @_results ||= account_favourites.eager_load(:status).paginate_by_id(
+    @_results ||= account_favourites.eager_load(:status).to_a_paginated_by_id(
       limit_param(DEFAULT_STATUSES_LIMIT),
       params_slice(:max_id, :since_id, :min_id)
     )
index 9950296f3bc8f72b2ccd9930144daf4e6d630aa9..f90642a7389d7a540621ff8b03ac68952a4357c5 100644 (file)
@@ -32,7 +32,7 @@ class Api::V1::ScheduledStatusesController < Api::BaseController
   private
 
   def set_statuses
-    @statuses = current_account.scheduled_statuses.paginate_by_id(limit_param(DEFAULT_STATUSES_LIMIT), params_slice(:max_id, :since_id, :min_id))
+    @statuses = current_account.scheduled_statuses.to_a_paginated_by_id(limit_param(DEFAULT_STATUSES_LIMIT), params_slice(:max_id, :since_id, :min_id))
   end
 
   def set_status
index 189b9201262c5b96a39020c1e1a364e203318687..abbdb410a59fb8d7fd626c4272b72061c23a0c05 100644 (file)
@@ -49,6 +49,6 @@ module CacheConcern
   end
 
   def cache_collection_paginated_by_id(raw, klass, limit, options)
-    cache_collection raw.cache_ids.paginate_by_id(limit, options), klass
+    cache_collection raw.cache_ids.to_a_paginated_by_id(limit, options), klass
   end
 end
index b43816588521b479130462aaf501a3cad7773a6e..5e2ddd0839f1ef50a56762f140c4010716aed368 100644 (file)
@@ -36,11 +36,11 @@ class AccountConversation < ApplicationRecord
   end
 
   class << self
-    def paginate_by_id(limit, options = {})
+    def to_a_paginated_by_id(limit, options = {})
       if options[:min_id]
         paginate_by_min_id(limit, options[:min_id]).reverse
       else
-        paginate_by_max_id(limit, options[:max_id], options[:since_id])
+        paginate_by_max_id(limit, options[:max_id], options[:since_id]).to_a
       end
     end
 
index 8863094f7d467be0ac73ae1279648d9f24b4347d..760cc3df4d43edb13c7524513f423afa3c753770 100644 (file)
@@ -20,12 +20,12 @@ module Paginable
       query
     }
 
-    scope :paginate_by_id, ->(limit, options = {}) {
+    def self.to_a_paginated_by_id(limit, options = {})
       if options[:min_id].present?
         paginate_by_min_id(limit, options[:min_id]).reverse
       else
-        paginate_by_max_id(limit, options[:max_id], options[:since_id])
+        paginate_by_max_id(limit, options[:max_id], options[:since_id]).to_a
       end
-    }
+    end
   end
 end