GithubHelp home page GithubHelp logo

Comments (4)

cviebrock avatar cviebrock commented on June 10, 2024

Hmmm ... I see what you're saying. If you create a new model and manually set the slug, then it shouldn't generate one for you. That makes logical sense to me, and could probably be solved by just reversing the order of the two if statements in the needsSlugging method.

It's probably a breaking change, though. I'd need to test it out against all the unit tests to be sure (and even then, it could still be breaking the behaviour that others have come to rely on).

An alternative would be to add a new check before the other two: if the model is new and the attribute has a value, then just leave it alone. Does that make sense to you?

from eloquent-sluggable.

seitenumbruch avatar seitenumbruch commented on June 10, 2024

I agree with all your thoughts. I believe that "fixing" this – no matter how you go about it – would always result in a breaking change, since changing existing behaviour is what this is all about.

As to your two first suggestions/thoughts:

1. Reordering the if statements

could probably be solved by just reversing the order of the two if statements in the needsSlugging method

This would affect the behaviour on updates as well. In other words: When updating a model and manually setting a slug, Sluggable won't generate a slug. While this makes logical sense to me, it does undermine your suggestion in the docs:

If you want to regenerate one or more of your model's slug fields, you can set those fields to null or an empty string before the update.

This would no longer work.

2. Don't generate slug if model is new and attribute has value

add a new check before the other two: if the model is new and the attribute has a value, then just leave it alone.

While this approach would avoid the issue of no longer being able to force slug generation on update, I am not a fan of it. In the name of fixing one inconsistency it would introduce another: Manually setting a slug would result in different behaviour when updating a model than when creating one.

My thoughts

I, personally, would go with your first suggestion (reordering if-statements). Yes, you'd lose the ability to force generating a new slug on update by setting the field to null, but I am not a fan of this "hack" anyways. I'd much rather make use of the SlugService::createSlug() method and explicitly generate a new slug.

from eloquent-sluggable.

seitenumbruch avatar seitenumbruch commented on June 10, 2024

I thought about this a little more. Maybe this is of help?

Short:

protected function needsSlugging(string $attribute, array $config): bool
{
    if ($value === null && $trim($value) === '') {
        return true;
    }

    if ($this->model->isDirty($attribute)) {
        return false;
    }

    if ($this->model->exists && $config['onUpdate'] === true) {
        return true;
    }

    return false;
}

The same again, but with explanatory comments:

protected function needsSlugging(string $attribute, array $config): bool
{
    // IF 1: Always generate a new slug if the current value is invalid

    if ($value === null && $trim($value) === '') {
        return true;
    }

    // IF 2: Never generate a new slug if a valid slug (see IF 1) has been set manually

    if ($this->model->isDirty($attribute)) {
        return false;
    }

    /*
     * I believe that at this point only updates remain, since all new model creations
     * either have a manually set slug value (triggered IF 2)
     * or don't have any slug value at all (triggered IF 1).
     * 
     * I believe that what remains are existing models with valid slug values in the database.
     * However, I am not sure about that, which is why I still check for the models existence in the next if-condition.
     */

    // IF 3: Generate a new slug if the current value is valid (see IF 1)
    // and has not been set manually (see IF 2)
    // and this is an update and a new slug should be generated on updates

    if ($this->model->exists && $config['onUpdate'] === true) {
        return true;
    }

    /*
     * Now all that remains should be existing models with valid slug values in the database
     * for which onUpdate is set to false.
     */ 

    return false;
}

Please take all my input with a huge grain of salt. I am not an experienced developer at all and my understanding of PHP is very limited.

Edit: With this approach it would still be possible to force generating a new slug by setting the slug to null.

from eloquent-sluggable.

seitenumbruch avatar seitenumbruch commented on June 10, 2024

I am very sorry, I must have accidentally closed this issue :/ You can tell this is my first ever issue...

from eloquent-sluggable.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.