Skip to content
New issue

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

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

Already on GitHub? # to your account

Dont call identifiable attribute in models that dont use crud trait #3643

Conversation

pxpm
Copy link
Contributor

@pxpm pxpm commented Apr 1, 2021

refs #3635

If the related model does not use CrudTrait, identifiableAttribute will not be available to call. It's developer responsability to setup attribute or return the predefined options.

This is just a small change, it would error anyway if method don't exist on model.

@pxpm pxpm added the Minor Bug A bug that happens only in a very niche or specific use case. label Apr 1, 2021
@pxpm pxpm requested a review from promatik April 1, 2021 10:07
@pxpm pxpm requested a review from tabacitu April 1, 2021 10:07
@nbolender
Copy link
Contributor

@pxpm This makes sense to me and would solve my issue from #3635, while still automatically calling identifiableAttribute if defined by the developer. Thank you!

Comment on lines 200 to 201
if (isset($field['model']) && ! isset($field['attribute'])) {
$field['attribute'] = call_user_func([(new $field['model']), 'identifiableAttribute']);
if (method_exists($field['model'], 'identifiableAttribute')) {
Copy link
Member

Choose a reason for hiding this comment

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

Wouldn't this be the same as

if (isset($field['model']) && ! isset($field['attribute']) && method_exists($field['model'], 'identifiableAttribute'))

? One if statement instead of two?

Copy link
Member

Choose a reason for hiding this comment

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

Or maybe it makes more sense to keep the separate if, but give it a raison d'être 😅 Would it be a better UX if identifiableAttribute doesn't exist to show a custom error message, and tell the developer that model X doesn't have identifiableAttribute() so it's probably not using CrudTrait and they need to do that? Maybe it's an opportunity to turn a negative experience into something... less negative 😅

What do you think @pxpm ?

Copy link
Contributor Author

@pxpm pxpm Apr 16, 2021

Choose a reason for hiding this comment

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

Sure, it's totally fine in just one if statement, I made it two for readability purposes, maybe I was wrong.

I do not agree we should raise an exception here so there is no raison d'être voilá merci ça va 😺 for that second if. I will refactor after we brainstorm on this.

The intent of this change, is to use relational fields with Models that don't use crud trait, for example in an select2_from_array, that is a relation, but you are providing the final options ($key => $value) to the field, so you might be using that Model for the specific purpose of holding the options, but without the need of adding crud trait.

It would make sense in, let's say a relationship field, because there we need attribute or the related model to have CrudTrait so we can infer it.

What we could do to avoid the specific scenario of select_from_array, is in the field initialization cycle, we check if options are present, if they are we don't infer the attribute ? We assume that the options are final key => attribute for the field ?

This last way would allow us to do what you said about raising the exception when really dev. messed up and forgot to add CrudTrait.

Let me know,
Pedro

Copy link
Member

Choose a reason for hiding this comment

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

The intent of this change, is to use relational fields with Models that don't use crud trait

Ooooh! I get it now, ok.

What we could do to avoid the specific scenario of select_from_array, is in the field initialization cycle, we check if options are present, if they are we don't infer the attribute ? We assume that the options are final key => attribute for the field ?

Huh... that sounds smart. But do we even get the identifiableAttribute if options is already defined? I don't think so.

This last way would allow us to do what you said about raising the exception when really dev. messed up and forgot to add CrudTrait.

Yeah no, sounds like I was overthinking this one too. Let's not throw an error in this case. We're not sure it's an error - the dev might have chosen to point to a non-CrudTrait model. And we're fine with that.

Sorry 🙏 But yeah let's merge the two ifs into one please.

@tabacitu tabacitu assigned tabacitu and unassigned tabacitu Apr 16, 2021
Copy link
Contributor

@promatik promatik left a comment

Choose a reason for hiding this comment

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

@pxpm I merged both if statements as Tabacitu suggested ✌

@promatik promatik assigned pxpm and unassigned tabacitu and promatik Apr 25, 2021
@scrutinizer-notifier
Copy link

The inspection completed: No new issues

@pxpm
Copy link
Contributor Author

pxpm commented Apr 26, 2021

Thanks @promatik \o

Good to go!

@tabacitu tabacitu merged commit 9982ce6 into master May 3, 2021
@tabacitu tabacitu deleted the dont-call-identifiable-attribute-in-models-that-dont-use-crud-trait branch May 3, 2021 04:53
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Minor Bug A bug that happens only in a very niche or specific use case.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants