Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Conditionally turn off :dependent => :destory on FriendlyId::Slugs #268

Closed

Conversation

erikogan
Copy link
Contributor

Hi,

We're using acts_as_paranoid on some of our sluggable models. This means they’re not actually deleted when calling destroy. If we delete the slugs, we’re losing the history (and worse, setting up a condition where a unique contstraint may end up being violated)

There is a chicken & egg problem here with FriendlyId::Configuration objects and use. We’d love to get your feedback on the proposed solution.

Thanks!

module Configuration
def dependent_destroy
return @dependent_destroy if defined?(@dependent_destroy)
@dependent_destroy = options.delete(:dependent_destroy) != false ? :destroy : nil
Copy link
Collaborator

Choose a reason for hiding this comment

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

how about:

@dependent_destroy = (:destroy if options.delete(:dependent_destroy))

seeing as you're saying != false, wouldn't it be fine to accept any truthy value?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Then the entire method could be:

def dependent_destroy
  @dependent_destroy ||= (:destroy if options.delete(:dependent_destroy))
end

Unless I'm missing something about why you're using defined?(@dependent_destroy) (for example, a false value would trigger the ||=)

Thanks

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The != false is to cover the case where the :dependent_destroy is nill (most likely because it has not been set, as this is the default case) We wanted to maintain the same functionality in the default case.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ahh, that makes sense.

@norman
Copy link
Owner

norman commented Apr 17, 2012

Yeah, I think this is fine. I worry a little about letting FriendlyId 4 bloat up with a bunch of configuration options and switches, like FriendlyId 3 had. But I don't really see any better way to handle this situation, and it seems like a rather reasonable use case. At least the "bloat" is now isolated to an optional add on.

Leaflet
end

test "run!" do
Copy link
Owner

Choose a reason for hiding this comment

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

How about a slightly more descriptive test name here, to better match the rest of the test suite?

@norman
Copy link
Owner

norman commented Apr 28, 2012

Another comment on this: isn't possible to just redefine the slugs relationship in the model? Then there would be no need for another config option.

@norman
Copy link
Owner

norman commented Jun 28, 2012

Closing this as stale. If you still think it should be merged, please see my comments and let me know.

@norman norman closed this Jun 28, 2012
@twe4ked
Copy link

twe4ked commented Aug 27, 2012

@jasoncodes and I simply overrode the dependent destroy method.

# prevent slugs being destroyed
def has_many_dependent_for_slugs; end

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.

4 participants