-
-
Notifications
You must be signed in to change notification settings - Fork 592
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
Slugs should always find their sluggable, even when they are soft deleted by acts_as_paranoid #838
Conversation
ddaaf09
to
a812a59
Compare
2 failing builds remaining. Haven't investigated why. https://travis-ci.org/norman/friendly_id/builds/283262267?utm_source=github_status&utm_medium=notification |
a812a59
to
e12cfc6
Compare
…eted ... by acts_as_paranoid Previously, when a sluggable is soft deleted and a slug persists (i.e. :dependent => false), slug can't find its corresponding sluggable, which renders the slug invalid starting in Rails 5.0 with the error message 'Sluggable must exist'. Based on [the related Rails issue thread](rails/rails#18233), either we set the main apps default by setting mattr_accessor AR::Base#belongs_to_required_by_default back to false, or we explicitly write our expectations in each call to belongs_to. We opted for the latter since other gems, depending on the load order, can still overwrite this global switch, which makes the validation on FriendlyId::Slug inconsistent.
e12cfc6
to
d00f1a8
Compare
Hey, thanks for this! I'm uncomfortable adding the development dependency on
What do you think of these changes below? diff --git a/friendly_id.gemspec b/friendly_id.gemspec
index 52aac43..883e20b 100644
--- a/friendly_id.gemspec
+++ b/friendly_id.gemspec
@@ -26,7 +26,6 @@ Gem::Specification.new do |s|
s.add_development_dependency 'i18n'
s.add_development_dependency 'ffaker'
s.add_development_dependency 'simplecov'
- s.add_development_dependency 'acts_as_paranoid'
s.description = <<-EOM
FriendlyId is the "Swiss Army bulldozer" of slugging and permalink plugins for
diff --git a/lib/friendly_id/slug.rb b/lib/friendly_id/slug.rb
index 8f549f8..9a6694c 100644
--- a/lib/friendly_id/slug.rb
+++ b/lib/friendly_id/slug.rb
@@ -3,7 +3,7 @@ module FriendlyId
#
# @see FriendlyId::History
class Slug < ActiveRecord::Base
- belongs_to :sluggable, :polymorphic => true, required: true
+ belongs_to :sluggable, :polymorphic => true
def sluggable
sluggable_type.constantize.unscoped { super }
diff --git a/test/history_test.rb b/test/history_test.rb
index 6d00915..3c95158 100644
--- a/test/history_test.rb
+++ b/test/history_test.rb
@@ -212,27 +212,25 @@ class DependentDestroyTest < HistoryTest
end
if ActiveRecord::VERSION::STRING >= '5.0'
- class HistoryTestWithActsAsParanoid < HistoryTest
- require 'acts_as_paranoid'
+ class HistoryTestWithParanoidDeletes < HistoryTest
class ParanoidRecord < ActiveRecord::Base
- acts_as_paranoid
extend FriendlyId
friendly_id :name, :use => :history, :dependent => false
+
+ default_scope { where(deleted_at: nil) }
end
def model_class
ParanoidRecord
end
- test 'slug should have a sluggable even when soft deleted via acts_as_paranoid' do
+ test 'slug should have a sluggable even when soft deleted by a library' do
transaction do
- assert FriendlyId::Slug.belongs_to_required_by_default
-
assert FriendlyId::Slug.find_by_slug('paranoid').nil?
record = model_class.create(name: 'paranoid')
assert FriendlyId::Slug.find_by_slug('paranoid').present?
- record.destroy
+ record.update_attribute(:deleted_at, Time.now)
orphan_slug = FriendlyId::Slug.find_by_slug('paranoid')
assert orphan_slug.present?, 'Orphaned slug should exist' Thanks! |
Copied based on @parndt’s code review comment norman#838 (comment)
@parndt took your suggestions in the next commit I just pushed. Thanks for the feedback, that is indeed a better way of implementing the soft deletion. |
Probably fixes #822, as the test case is likely what he has encountered. His report most probably required 2 things to be true:
friendly_id ... :dependent => false
Previously, when a sluggable is soft deleted and a slug persists
(i.e. :dependent => false), slug can't find its corresponding
sluggable.
Additionally, in Rails 5.0, belongs_to_required_by_default has been
introduced to be true by default (unless changed back to false), therefore making Friendly::Slug
invalid with the error message 'Sluggable must exist'.