diff --git a/lib/stringex/acts_as_url/adapter/base.rb b/lib/stringex/acts_as_url/adapter/base.rb index 0c51c7a6..8a2320d2 100644 --- a/lib/stringex/acts_as_url/adapter/base.rb +++ b/lib/stringex/acts_as_url/adapter/base.rb @@ -64,8 +64,10 @@ def add_new_record_url_owner_conditions def add_scoped_url_owner_conditions [settings.scope_for_url].flatten.compact.each do |scope| - @url_owner_conditions.first << " and #{scope} = ?" - @url_owner_conditions << instance.send(scope) + scope_val = instance.send(scope) + sql_operator = scope_val.nil? ? "IS" : "=" + @url_owner_conditions.first << " AND #{scope} #{sql_operator} ?" + @url_owner_conditions << scope_val end end diff --git a/test/unit/acts_as_url_integration_test.rb b/test/unit/acts_as_url_integration_test.rb index 20ae2220..0d58d98e 100644 --- a/test/unit/acts_as_url_integration_test.rb +++ b/test/unit/acts_as_url_integration_test.rb @@ -177,6 +177,22 @@ def test_should_only_create_unique_urls_for_multiple_scopes_if_both_attributes_a assert_not_equal @doc.url, @other_doc.url end + def test_should_create_uniuque_urls_for_nil_scope_values + Document.class_eval do + acts_as_url :title, scope: %i[other another] + end + + @doc = Document.create(title: "Soft Deleted Document", + other: "scope key", + another: nil) + + @other_doc = Document.create(title: "Soft Deleted Document", + other: "scope key", + another: nil) + + assert_not_equal @doc.url, @other_doc.url + end + def test_should_allow_setting_url_attribute Document.class_eval do # Manually undefining the url method on Document which, in a real class not reused for tests,