Skip to content

[7.x] Simplify hidden attributes on models#30348

Merged
taylorotwell merged 2 commits intolaravel:masterfrom
tontonsb:patch-9
Oct 21, 2019
Merged

[7.x] Simplify hidden attributes on models#30348
taylorotwell merged 2 commits intolaravel:masterfrom
tontonsb:patch-9

Conversation

@tontonsb
Copy link
Copy Markdown
Contributor

This PR is aimed to make the method behaviour more predicatable, consistent and to slightly simplify the codebase.

The state before this PR is the following:

  • Methods makeHidden and makeVisible are featured on documentation and tested, addHidden and addVisible are not. And each of add* only has a single use in the codebase.
  • Method makeHidden may produce surprising results on an edge case:
    • Calling makeHidden['col1'] on a model that has $visible = ['col1'] would clear (and thus disable) the whitelist, making everything visible (except col1).
  • Method addVisible may produce unexpected results on two cases
    • If the model's $visible was empty, addVisible('col1') will make everything but col1 invisible;
    • If col1 is hidden, addVisible('col1') would not make it visible.
  • Other than the three quirks above, the differences between makeHidden/makeVisible and addHidden/addVisible are just these:
    • make* returns model, add* returns void.
    • add* accepts unwrapped lists of arguments ('col1', 'col2') in addition to single string ('col1') and arrays (['col1', 'col2']) which are accepted by both.

To solve the inconsistencies and surprises, simplify the work with the methods and reduce amount of similar methods, I propose here to:

  • Make makeHidden and makeVisible accept unwrapped lists of arguments ('col1', 'col2') in addition to current ('col1') and (['col1', 'col2']). That's for convenience and consistency with methods like with().
  • Remove addHidden and addVisible.
  • Move previous addVisible logic into makeVisible (it's still one line).
  • Simplify makeHidden to only add entries to $hidden, but not remove the from $visible.

Explanation on the final point: adding to blacklist $hidden is enough as the blacklist is applied last. The only case where removing from $visible would affect behaviour is the aforementioned quirk that makes everything else visible and such behaviour just shouldn't be here.

@GrahamCampbell GrahamCampbell changed the title Simplify makeHidden, makeVisible on models. Remove addHidden, addVisible. [7.x] Simplify hidden attributes on models Oct 20, 2019
@taylorotwell taylorotwell merged commit 77b6492 into laravel:master Oct 21, 2019
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