Skip to content

Commit

Permalink
Dont use alias_method to make it easier to override methods in use
Browse files Browse the repository at this point in the history
  • Loading branch information
stevegeek committed Sep 30, 2024
1 parent c3deb31 commit 4986b27
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
7 changes: 3 additions & 4 deletions lib/quo/collection_backed_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
11 changes: 9 additions & 2 deletions lib/quo/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 8 additions & 6 deletions test/quo/collection_backed_query_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4986b27

Please sign in to comment.