Skip to content

Implicit binding model in policy#17

Closed
15g-lucas wants to merge 2 commits intoLomkit:mainfrom
15g-lucas:implicite-binding-model
Closed

Implicit binding model in policy#17
15g-lucas wants to merge 2 commits intoLomkit:mainfrom
15g-lucas:implicite-binding-model

Conversation

@15g-lucas
Copy link
Member

@15g-lucas 15g-lucas commented Apr 24, 2025

This allows the model name in ControlledPolicy to be determined directly. It is always possible to specify the name in special cases

Summary by CodeRabbit

  • New Features
    • Improved policy handling by adding a fallback mechanism to automatically determine the associated model when not explicitly set.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Apr 24, 2025

Walkthrough

The getModel() method in the ControlledPolicy class was updated to include a fallback mechanism. Now, if the $model property is not set, the method dynamically constructs and returns a default model class name by inferring it from the policy class name. Previously, the method always returned the $model property without any fallback logic.

Changes

File Change Summary
src/Policies/ControlledPolicy.php Updated the getModel() method to return the $model property if set, or infer the model class name otherwise.

Poem

In the warren of code where policies dwell,
A clever new fallback now works quite well.
If the model is missing, don’t worry or fret—
The class name’s inferred, the guess is well-set.
Hopping through logic, no carrot’s misplaced,
This rabbit ensures all models are traced! 🥕

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 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 generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @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.

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 (3)
src/Policies/ControlledPolicy.php (3)

26-30: Great improvement for DX by adding implicit model binding!

The modification allows for automatic model resolution based on the policy class name, reducing boilerplate code while maintaining backward compatibility for explicit model definitions. This follows Laravel's "convention over configuration" principle.

Consider making the base namespace configurable rather than hardcoding App\\Models\\. This would better accommodate projects with custom namespaces:

- return 'App\\Models\\'.str_replace('Policy', '', class_basename(static::class));
+ return config('access.models_namespace', 'App\\Models\\').str_replace('Policy', '', class_basename(static::class));

10-10: Consider addressing the TODO comment.

There's a TODO comment about handling additional methods like attach/restore/force_delete, but the implementation already includes methods for restore and forceDelete (lines 114-130).

Either update or remove this TODO comment since it appears partially addressed.


19-23: Update method documentation to reflect new behavior.

The current docblock doesn't mention the implicit model resolution feature introduced by your changes.

Consider updating the documentation to reflect the new behavior:

 /**
  * Returns the fully qualified model class name associated with this policy.
+ * If the $model property is empty, it will automatically determine the model
+ * class name based on the policy class name (removing 'Policy' suffix).
  *
  * @return string The fully qualified class name of the model.
  */
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between dd5890b and 84d1fc7.

📒 Files selected for processing (1)
  • src/Policies/ControlledPolicy.php (1 hunks)

@GautierDele
Copy link
Member

Need global thinking about how class interacts with each others 😉

@GautierDele
Copy link
Member

Whole system rewrote in new version

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.

2 participants