From 4986b27ce3ccb329d006cb48e51457d4422079b1 Mon Sep 17 00:00:00 2001 From: Stephen Ierodiaconou Date: Mon, 30 Sep 2024 15:54:52 +0200 Subject: [PATCH] Dont use alias_method to make it easier to override methods in use --- lib/quo/collection_backed_query.rb | 7 +++---- lib/quo/query.rb | 11 +++++++++-- test/quo/collection_backed_query_test.rb | 14 ++++++++------ 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/lib/quo/collection_backed_query.rb b/lib/quo/collection_backed_query.rb index 4d45bc1..2846c4c 100644 --- a/lib/quo/collection_backed_query.rb +++ b/lib/quo/collection_backed_query.rb @@ -31,9 +31,6 @@ def self.wrap(data = nil, props: {}, &block) def count @total_count || underlying_query.size end - # @rbs override - alias_method :total_count, :count - alias_method :size, :count # @rbs override def page_count @@ -73,7 +70,9 @@ def group(*options) end # @rbs override - alias_method :includes, :preload + def includes(*options) + preload(*options) + end # The default implementation of `query` calls `collection` and preloads the includes, however you can also # override this method to return an ActiveRecord::Relation or any other query-like object as usual in a Query object. diff --git a/lib/quo/query.rb b/lib/quo/query.rb index 053f9c6..d69511b 100644 --- a/lib/quo/query.rb +++ b/lib/quo/query.rb @@ -229,8 +229,15 @@ def count #: Integer count_query(underlying_query) end - alias_method :total_count, :count - alias_method :size, :count + # Total number of items without paging applied. + def total_count #: Integer + count + end + + # Alias for count + def size #: Integer + count + end # Gets the actual count of elements in the page of results (assuming paging is being used, otherwise the count of # all results) diff --git a/test/quo/collection_backed_query_test.rb b/test/quo/collection_backed_query_test.rb index 05e7708..f37dd96 100644 --- a/test/quo/collection_backed_query_test.rb +++ b/test/quo/collection_backed_query_test.rb @@ -80,15 +80,17 @@ def collection end test "#includes" do - q = Quo::CollectionBackedQuery.wrap([1, 2, 3]).new - included = q.includes(:itself) - assert_equal [1, 2, 3], included.to_a + author = Author.create!(name: "John") + q = Quo::CollectionBackedQuery.wrap([author]).new + q = q.includes(:posts) + assert q.first.posts.loaded? end test "#preload" do - q = Quo::CollectionBackedQuery.wrap([1, 2, 3]).new - preloaded = q.preload(:itself) - assert_equal [1, 2, 3], preloaded.to_a + author = Author.create!(name: "John") + q = Quo::CollectionBackedQuery.wrap([author]).new + q = q.preload(:posts) + assert q.first.posts.loaded? end test "#first" do