Skip to content

Commit

Permalink
Fix a problem when using scopes with a nil value for the scoped field.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aubrey Holland committed Oct 9, 2008
1 parent 53fe85f commit fcd009f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
13 changes: 8 additions & 5 deletions lib/permalink_fu.rb
Expand Up @@ -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)
Expand Down
22 changes: 22 additions & 0 deletions test/permalink_fu_test.rb
Expand Up @@ -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

Expand Down Expand Up @@ -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.