Skip to content

Commit

Permalink
Avoid generating duplicate handle for new record.
Browse files Browse the repository at this point in the history
Thanks to mungler for finding the bug and proposing a solution.
  • Loading branch information
alexreisner committed Aug 19, 2010
1 parent 5418383 commit 7c9d727
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lib/handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,20 @@ def handle_based_on(attribute, options = {})
h = Handler.generate_handle(send(attribute), options[:separator])
if options[:unique]

# generate a condition for finding an existing record with a
# given handle
find_dupe = lambda{ |h|
conds = ["#{options[:write_to]} = ?", h]
unless new_record?
conds[0] << " AND id != ?"
conds << id
end
conds
}

# increase number while *other* records exist with the same handle
# (record might be saved and should keep its handle)
while self.class.all(:conditions => ["#{options[:write_to]} = ? AND id != ?", h, id]).size > 0
while self.class.all(:conditions => find_dupe.call(h)).size > 0
h = Handler.next_handle(h, options[:separator])
end
end
Expand Down

0 comments on commit 7c9d727

Please sign in to comment.