about summary refs log tree commit diff
path: root/app/models
diff options
context:
space:
mode:
authorAkihiko Odaki <nekomanma@pixiv.co.jp>2020-08-31 19:47:09 +0900
committerGitHub <noreply@github.com>2020-08-31 12:47:09 +0200
commitae871c4d46a8498a3ff5c400baa6e82174b384d0 (patch)
tree433d2134d6d4a68f85c604bc149819d52836cd6a /app/models
parent03b5f09ffceb35c53ceff7001de98da1eebf4acd (diff)
Make Array-creation behavior of Paginable more predictable (#14687)
* 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.
Diffstat (limited to 'app/models')
-rw-r--r--app/models/account_conversation.rb4
-rw-r--r--app/models/concerns/paginable.rb6
2 files changed, 5 insertions, 5 deletions
diff --git a/app/models/account_conversation.rb b/app/models/account_conversation.rb
index b43816588..5e2ddd083 100644
--- a/app/models/account_conversation.rb
+++ b/app/models/account_conversation.rb
@@ -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
 
diff --git a/app/models/concerns/paginable.rb b/app/models/concerns/paginable.rb
index 8863094f7..760cc3df4 100644
--- a/app/models/concerns/paginable.rb
+++ b/app/models/concerns/paginable.rb
@@ -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