Skip to content
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

Add the ability to auto-determine the presenter #26

Closed
wants to merge 4 commits into from
Closed

Add the ability to auto-determine the presenter #26

wants to merge 4 commits into from

Conversation

drjermy
Copy link

@drjermy drjermy commented Aug 16, 2023

I wonder whether you'd be interested in considering this pull-request.

The idea here is to automatically use a presenter based on naming convention. So, in the User Model, the UserPresenter is loaded without needing to specify it in the $presenters array.

It also guesses the name for a named presenter, e.g. $users->present('certificate')->title would load the UserCertificatePresenter.

@ousid
Copy link
Member

ousid commented Sep 14, 2023

Hi, @drjermy

Sorry for the late response/review. I was quite busy.

This is an interesting PR to merge, could you please elaborate more about the idea with examples?

Thanks!

@drjermy
Copy link
Author

drjermy commented Sep 17, 2023

Currently when using Presenters, we setup by:

  • implement CanPresent
  • use UsesPresenters

And then, have to add a $presenters array that defines the Presenters. In most cases, we define a single default presenter.

So, rather than doing this, why not automatically look for a Presenter in a standard location (Where php artisan make:presenter puts a presenter) with the name "ModelPresenter" where Model is the model name.

If $presenters is set, then it overwrites the defaults.

So, if I had a model called User and implemented CanPresent and added UsesPresenters, then the default presenter would autoload UserPresenter.php which I'd created using php artisan make:presenter

@drjermy
Copy link
Author

drjermy commented Oct 15, 2023

@ousid any update?

@ousid
Copy link
Member

ousid commented Oct 15, 2023

@ousid any update?

Hey, I'm seeing failed tests, can you please take a look and see what's wrong exactly?

Thanks!

{
$modelNameModifier = $type === 'default' ? '' : $type;

return str(get_class())
Copy link
Member

Choose a reason for hiding this comment

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

It seems the tests are failing, regards, to the str method. Consider using the Illuminate\Support\Str facade, maybe?

if (array_key_exists($type, $this->presenters)) {
return new $this->presenters[$type]($this);
$presenter = $this->getDefaultPresenterName($type);

Copy link
Member

Choose a reason for hiding this comment

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

I see a Code smell in here, consider using aggressive condition statement, to clean the code, and understandable.

Suggested change
if (
is_array($this->presenters) &&
array_key_exists($type, $this->presenters ?? [])
) {
$presenter =$this->presetners[$type];
}
if (is_string($this->presenter ?? null)) {
$presenter = $this->presenter;
}
if(class_exists($presenter)) {
return new $presenter($this);
}
throw new PresenterException();

{
public function slug()
{
return str($this->model->title)->slug()->toString();
Copy link
Member

Choose a reason for hiding this comment

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

Consider using Str facade.

{
public function caps()
{
return str($this->model->title)->upper()->toString();
Copy link
Member

Choose a reason for hiding this comment

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

Same here, use the Str facade, or invoke the str function on the namespaces.

@@ -111,7 +111,7 @@ If you want to change the directory, you have two options.
First options is to set the full namespace while you're creating the presenter class

```bash
php artisna presneter:make App\Models\Presenter\UserPresenter
php artisan presneter:make App\Models\Presenter\UserPresenter
Copy link
Member

Choose a reason for hiding this comment

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

Maybe you want to create an alias for the command? Something like php artisan make:presenter, without removing the current?

Copy link
Author

Choose a reason for hiding this comment

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

This was just a spelling error change.

Copy link
Member

@ousid ousid left a comment

Choose a reason for hiding this comment

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

@drjermy
Please take a look at the reviews, and consider fix the failing tests (most of them relate to the str method)

@ousid
Copy link
Member

ousid commented Oct 17, 2023

Tests still failing, could you please run them locally, and once all passes, tag me for a review.

Thanks!

@ousid
Copy link
Member

ousid commented Nov 14, 2023

Closing this PR, because of the failing tests.

@ousid ousid closed this Nov 14, 2023
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