Skip to content
This repository has been archived by the owner on Nov 16, 2021. It is now read-only.

Make attachment model classes dynamic #76

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

wimski
Copy link

@wimski wimski commented May 5, 2021

In my current project I work with UUID's for all my models. I want the Froala attachments to also use UUID's instead of integers. To do this I need to extend the models and set their $keyType property to string. This currently isn't possible so I've made the model classes dynamic so you can easily swap them with extensions.

<?php

namespace App\Models\Froala;

use Froala\NovaFroalaField\Models\Attachment as OriginalAttachment;

class Attachment extends OriginalAttachment
{
    protected $keyType = 'string';
}
// froala-field.php config
'attachment_model'  => App\Models\Froala\Attachment::class,

@wimski wimski force-pushed the feature/extend-attachment-models branch from 6ffc956 to fe84082 Compare May 5, 2021 15:19
@wimski wimski force-pushed the feature/extend-attachment-models branch from fe84082 to 3db983d Compare May 6, 2021 05:45
src/Handlers/DetachAttachment.php Outdated Show resolved Hide resolved
config/froala-field.php Outdated Show resolved Hide resolved
*/
public function __construct($attachmentModelClassName)
{
$this->attachmentModelClassName = $attachmentModelClassName;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's get model class from config directly, without requirement to pass it into constructor.
Also let's omit "name" suffix and attachment prefix, we already in DetachAttachment class context. The repetition of context is mostly redundant.

Copy link
Author

Choose a reason for hiding this comment

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

Let's get model class from config directly, without requirement to pass it into constructor.

Why would you create a tight coupling between the handlers, which are small and single-purpose classes, and the config helper? There's no need for them to know about the framework and its helpers. Injecting the model class makes them less dependent.

Also let's omit "name" suffix and attachment prefix, we already in DetachAttachment class context. The repetition of context is mostly redundant.

Why not be as explicit as possible? Is there a cost to having a slightly longer variable/property name?

$modelClassName = $this->app['config']->get('nova.froala-field.attachment_model');

/** @var Model $model */
$model = new $modelClassName();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Extract get table of class string to the own method.

Copy link
Author

Choose a reason for hiding this comment

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

That doesn't sound very DRY. Any specific reason for introducing code duplication?

@wimski wimski force-pushed the feature/extend-attachment-models branch from f7d9939 to ef40777 Compare May 19, 2021 06:54
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants