Skip to content

Define default limit #159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 7, 2025
Merged

Conversation

15g-lucas
Copy link
Member

@15g-lucas 15g-lucas commented Mar 26, 2025

closes #157

This PR adds the possibility of defining a default pagnination limit for a specific model.

If not defined, the value remains 50.

I took the opportunity to update the Resource models with this new feature.

It will be necessary to update the doc according to.

Summary by CodeRabbit

  • New Features

    • Enhanced pagination to use a configurable default limit for displayed items.
    • Introduced a new API endpoint that demonstrates customizable pagination behavior.
  • Tests

    • Added test cases verifying that resources return the correct default number of items per page in various scenarios.

Copy link
Contributor

coderabbitai bot commented Mar 26, 2025

Walkthrough

This pull request refactors pagination by introducing a dynamic default limit. The paginate method in the Paginable trait and the search method in the ScoutBuilder class now use a $defaultLimit property instead of a hardcoded value. Multiple resource classes and stubs have been updated to include a new public $defaultLimit property. Additionally, new tests and endpoints have been added to verify that resources can override the default value. A special resource with a custom limit (32) has also been introduced along with its corresponding controller and route.

Changes

File(s) Change Summary
src/Concerns/Resource/Paginable.php
src/Query/ScoutBuilder.php
Updated pagination methods to use dynamic $defaultLimit instead of the hardcoded limit of 50.
src/Console/stubs/resource.stub
src/Console/stubs/user-resource.stub
src/Http/Resource.php
Added a public integer property $defaultLimit (defaulting to 50) with descriptive docblocks to resource stubs and the Resource class.
tests/Feature/Controllers/SearchPaginateOperationsTest.php Added a new test method to verify that the default pagination limit is used when no custom limit is provided.
tests/Support/Http/Controllers/ModelWithDefaultLimitController.php
tests/Support/Rest/Resources/ModelWithDefaultLimitResource.php
tests/Support/Routes/api.php
Introduced a new controller, a resource with a custom $defaultLimit (set to 32), and a corresponding API route to support resources with customizable pagination limits.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Controller
    participant Resource
    participant QueryBuilder
    participant PaginableTrait

    Client->>Controller: Send pagination request
    Controller->>Resource: Retrieve associated resource
    Resource->>QueryBuilder: Provide $defaultLimit value
    QueryBuilder->>PaginableTrait: Call paginate() with dynamic limit
    PaginableTrait-->>QueryBuilder: Return paginated data
    QueryBuilder-->>Resource: Forward paginated data
    Resource-->>Controller: Deliver response data
    Controller-->>Client: Return JSON response with pagination info
Loading

Assessment against linked issues

Objective Addressed Explanation
Add ability to change the default limit on a resource (#157)

Poem

I'm a coding bunny, hopping with delight,
Changing defaults so every limit's just right.
From fifty to custom bits, we leap through code,
In fields of pagination, on a new bright road.
With a twitch of my nose, I celebrate this light load! 🐰✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1e16a60 and 103e033.

📒 Files selected for processing (2)
  • tests/Support/Http/Controllers/ModelWithDefaultLimitController.php (1 hunks)
  • tests/Support/Rest/Resources/ModelWithDefaultLimitResource.php (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • tests/Support/Rest/Resources/ModelWithDefaultLimitResource.php
  • tests/Support/Http/Controllers/ModelWithDefaultLimitController.php

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@GautierDele
Copy link
Member

GautierDele commented Mar 28, 2025

Thanks for the proposal, this needs unit tests / doc update please

Be careful about ScoutBuilder line 52, there is still a 50 limit going on

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
tests/Feature/Controllers/SearchPaginateOperationsTest.php (1)

86-102: Good test coverage for the new default limit feature.

This test appropriately validates the default pagination behavior by ensuring:

  1. The correct default limit (32) from ModelResource is applied when no limit is specified
  2. Pagination metadata is properly calculated (100 items ÷ 32 per page = 4 pages)
  3. The correct number of items is returned in the response

Consider adding another test case that explicitly sends an empty search object ({'search': {}}) to ensure both scenarios work correctly:

public function test_to_get_a_list_of_paginated_resources_with_empty_search_object(): void
{
    ModelFactory::new()->count(100)->create()->fresh();

    Gate::policy(Model::class, GreenPolicy::class);

    $response = $this->post(
        '/api/models/search',
        [
            'search' => [],
        ],
        ['Accept' => 'application/json']
    );

    $response->assertStatus(200);
    $response->assertJsonPath('per_page', 32);
    $response->assertJsonPath('last_page', 4);
    $response->assertJsonCount(32, 'data');
}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0ead544 and 8fe63bf.

📒 Files selected for processing (2)
  • tests/Feature/Controllers/SearchPaginateOperationsTest.php (3 hunks)
  • tests/Support/Rest/Resources/ModelResource.php (1 hunks)
🔇 Additional comments (2)
tests/Support/Rest/Resources/ModelResource.php (1)

35-35: Appropriate implementation of the default limit feature.

This addition aligns with the PR objective of introducing a customizable default pagination limit for specific models. Setting it to 32 (different from the global default of 50) provides a good test case for model-specific limits.

tests/Feature/Controllers/SearchPaginateOperationsTest.php (1)

46-46: LGTM - Whitespace improvements.

These formatting changes improve code consistency without affecting functionality.

Also applies to: 72-72

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/Query/ScoutBuilder.php (1)

21-21: Update docblock comment to reflect new default limit behavior.

The docblock comment in line 21 still refers to "defaulting to 50 if no limit is specified" but the implementation now uses a dynamic default limit from the resource. Update the documentation to match the implementation.

- * defaulting to 50 if no limit is specified. Any extra parameters, after excluding
+ * defaulting to the resource's defaultLimit (or 50 if not set). Any extra parameters, after excluding
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8fe63bf and 7cd0ae5.

📒 Files selected for processing (5)
  • src/Console/stubs/resource.stub (1 hunks)
  • src/Console/stubs/user-resource.stub (1 hunks)
  • src/Http/Resource.php (1 hunks)
  • src/Query/ScoutBuilder.php (1 hunks)
  • tests/Support/Rest/Resources/ModelResource.php (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (4)
  • src/Console/stubs/resource.stub
  • src/Http/Resource.php
  • tests/Support/Rest/Resources/ModelResource.php
  • src/Console/stubs/user-resource.stub
🧰 Additional context used
🧬 Code Definitions (1)
src/Query/ScoutBuilder.php (1)
src/Http/Response.php (1)
  • resource (26-31)
🔇 Additional comments (1)
src/Query/ScoutBuilder.php (1)

52-53: LGTM! Great implementation of dynamic default limit.

The implementation correctly retrieves the default limit from the resource model, falling back to 50 when not specified. This change aligns perfectly with the PR objective to allow customizable pagination limits per model.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
tests/Support/Http/Controllers/ModelWithDefaultLimitController.php (1)

7-7: Remove unused import

The ModelWithResource import doesn't appear to be used in this controller.

-use Lomkit\Rest\Tests\Support\Rest\Resources\ModelWithResource;
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 42fe309 and 1e16a60.

📒 Files selected for processing (5)
  • tests/Feature/Controllers/SearchPaginateOperationsTest.php (1 hunks)
  • tests/Support/Http/Controllers/ModelWithDefaultLimitController.php (1 hunks)
  • tests/Support/Rest/Resources/ModelResource.php (1 hunks)
  • tests/Support/Rest/Resources/ModelWithDefaultLimitResource.php (1 hunks)
  • tests/Support/Routes/api.php (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • tests/Support/Rest/Resources/ModelResource.php
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/Feature/Controllers/SearchPaginateOperationsTest.php
🧰 Additional context used
🧬 Code Definitions (1)
tests/Support/Rest/Resources/ModelWithDefaultLimitResource.php (4)
src/Http/Resource.php (1)
  • Resource (21-185)
src/Http/Requests/RestRequest.php (1)
  • RestRequest (9-23)
tests/Support/Models/Model.php (1)
  • Model (10-112)
tests/Support/Rest/Resources/ModelResource.php (1)
  • ModelResource (29-160)
🔇 Additional comments (3)
tests/Support/Routes/api.php (1)

10-10: LGTM - Proper route registration for the new resource

The route registration follows the established pattern and correctly defines the new resource route for 'model-with-default-limit', associating it with the appropriate controller.

tests/Support/Rest/Resources/ModelWithDefaultLimitResource.php (1)

29-32: LGTM - Properly implemented custom default limit

The ModelWithDefaultLimitResource class correctly extends ModelResource and implements a custom default limit of 32, overriding the base Resource class default of 50. This provides a good test case for the new pagination limit feature.

tests/Support/Http/Controllers/ModelWithDefaultLimitController.php (1)

9-12: LGTM - Controller properly connected to resource

The controller correctly extends the base Controller class and properly associates with the ModelWithDefaultLimitResource class through the static $resource property.

@GautierDele
Copy link
Member

Thanks, merging !

@GautierDele GautierDele merged commit 0225504 into Lomkit:master Apr 7, 2025
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add the ability to change the default limit on a resource
2 participants