mongoid-slug icon indicating copy to clipboard operation
mongoid-slug copied to clipboard

Abandoning slugs while keeping slug history

Open geraldhiller opened this issue 6 years ago • 3 comments

This is more of a question regarding a best/good practice than an issue.

I've been using mongoid_slugs for a while on my website. Now I would like to remove them and to switch back to using the db ids. I could remove the slugs keyword and the include statement from my model to get the result I want. But this would make me lose my slug history, which I don't want, as that could have unforseen negative consequences with Google.

What's the best practice for abandoning slugs while keeping the slug history?

I tried using slug :id, history: true but this suffixes my slugs with -1 58519083934c2b39e6c49a73-1, as it should if the id is in the slug table by default - although it does not get listed on document.slugs. What I am looking for is a way to tell mongoid_slug to use the id as the slug, instead of the last item of the slugs array.

geraldhiller avatar Jun 13 '18 14:06 geraldhiller

I would remove mongoid-slug and add field :_slugs, type: Array, ensure that there's an index with index :_slugs, unique: true and reimplement a lookup in a higher level of the application that falls back to _slugs whenever something doesn't look like an ID and/or cannot be found, followed by a 301 permanent redirect.

Does that help?

dblock avatar Jun 14 '18 01:06 dblock

It's definitely a good solution. I guess I was hoping for a built-in mechanism to achieve this kind of behavior. Like a "disabled" flag that you can set on the slug field so that the reverse lookup would still work, but the default id field gets served as the slug from now on. But probably my case is not common enough to warrant an option like that.

The problem for me with your solution is that I'm hesitant to add extra code just to achieve this behavior. So if there is no built-in solution, I am tending towards simply disabling the slugs and having to live with the lost history, whatever repercussions that may entail with Google.

geraldhiller avatar Jun 28 '18 08:06 geraldhiller

Feel free to PR something that accomplishes that. Instead of a disabled function I would want to split up the Mongoid::Slug module into a Mongoid::Slug::Find and Mongoid::Slug::Create so that you can include one, the other or both functionalities. Will leave this open as a feature request.

Other than above, you can roll your own Mongoid::Slug that just implements the find part and you won't have to change anything in your classes after removing the gem.

dblock avatar Jun 28 '18 12:06 dblock