Skip to content
Browse files

Fix a problem when using scopes with a nil value for the scoped field.

  • Loading branch information...
1 parent 53fe85f commit fcd009fce34b78ff537e4fabd24404759d3db7d1 Aubrey Holland committed Oct 9, 2008
Showing with 30 additions and 5 deletions.
  1. +8 −5 lib/permalink_fu.rb
  2. +22 −0 test/permalink_fu_test.rb
View
13 lib/permalink_fu.rb
@@ -117,11 +117,14 @@ def create_unique_permalink
conditions << id
end
if self.class.permalink_options[:scope]
- scopes = [self.class.permalink_options[:scope]]
- scopes.flatten!
- scopes.each do |scope|
- conditions.first << " and #{scope} = ?"
- conditions << send(scope)
+ [self.class.permalink_options[:scope]].flatten.each do |scope|
+ value = send(scope)
+ if value
+ conditions.first << " and #{scope} = ?"
+ conditions << send(scope)
+ else
+ conditions.first << " and #{scope} IS NULL"
+ end
end
end
while self.class.exists?(conditions)
View
22 test/permalink_fu_test.rb
@@ -129,6 +129,14 @@ def self.exists?(conditions)
has_permalink :title, :scope => :foo
end
+class ScopedModelForNilScope < BaseModel
+ def self.exists?(conditions)
+ (conditions[0] == 'permalink = ? and foo IS NULL') ? (conditions[1] == 'ack') : false
+ end
+
+ has_permalink :title, :scope => :foo
+end
+
class OverrideModel < BaseModel
has_permalink :title
@@ -358,4 +366,18 @@ def test_should_update_permalink_if_changed_method_does_not_exist
@m.validate
assert_equal 'the-permalink', @m.read_attribute(:permalink)
end
+
+ def test_should_work_correctly_for_scoped_fields_with_nil_value
+ s1 = ScopedModelForNilScope.new
+ s1.title = 'ack'
+ s1.foo = 3
+ s1.validate
+ assert_equal 'ack', s1.permalink
+
+ s2 = ScopedModelForNilScope.new
+ s2.title = 'ack'
+ s2.foo = nil
+ s2.validate
+ assert_equal 'ack-2', s2.permalink
+ end
end

0 comments on commit fcd009f

Please sign in to comment.
Something went wrong with that request. Please try again.