<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -1 +1 @@
-ActiveRecord::Base.send :extend, PermalinkFu
\ No newline at end of file
+ActiveRecord::Base.send :include, PermalinkFu
\ No newline at end of file</diff>
      <filename>init.rb</filename>
    </modified>
    <modified>
      <diff>@@ -14,24 +14,61 @@ module PermalinkFu
     end
   end
   
-  # Specifies the given field(s) as a permalink, meaning it is passed through PermalinkFu.escape and set to the permalink_field.  This
-  # is done
-  #
-  # class Foo &lt; ActiveRecord::Base
-  #   # stores permalink form of #title to the #permalink attribute
-  #   has_permalink :title
-  #
-  #   # stores a permalink form of &quot;#{category}-#{title}&quot; to the #permalink attribute
-  #
-  #   has_permalink [:category, :title]
-  #
-  #   # stores permalink form of #title to the #category_permalink attribute
-  #   has_permalink [:category, :title], :category_permalink
-  # end
-  #
-  def has_permalink(attr_names = [], permalink_field = nil)
-    permalink_field ||= 'permalink'
-    before_validation { |record| record.send(&quot;#{permalink_field}=&quot;, Array(attr_names).collect { |attr_name| PermalinkFu.escape(record.send(attr_name).to_s) }.join('-')) if record.send(permalink_field).to_s.empty? }
+  def self.included(base)
+    base.extend ClassMethods
+    class &lt;&lt; base
+      attr_accessor :permalink_attributes
+      attr_accessor :permalink_field
+    end
+  end
+  
+  module ClassMethods
+    # Specifies the given field(s) as a permalink, meaning it is passed through PermalinkFu.escape and set to the permalink_field.  This
+    # is done
+    #
+    # class Foo &lt; ActiveRecord::Base
+    #   # stores permalink form of #title to the #permalink attribute
+    #   has_permalink :title
+    #
+    #   # stores a permalink form of &quot;#{category}-#{title}&quot; to the #permalink attribute
+    #
+    #   has_permalink [:category, :title]
+    #
+    #   # stores permalink form of #title to the #category_permalink attribute
+    #   has_permalink [:category, :title], :category_permalink
+    #
+    #   
+    # end
+    #
+    def has_permalink(attr_names = [], permalink_field = 'permalink')
+      self.permalink_attributes = Array(attr_names)
+      self.permalink_field      = permalink_field
+      before_validation :create_unique_permalink
+    end
+  end
+  
+protected
+  def create_unique_permalink
+    if send(self.class.permalink_field).to_s.empty?
+      send(&quot;#{self.class.permalink_field}=&quot;, create_permalink_for(self.class.permalink_attributes))
+    end
+    base       = send(self.class.permalink_field)
+    counter    = 1
+    conditions = [&quot;#{self.class.permalink_field} = ?&quot;, send(self.class.permalink_field)]
+    unless new_record?
+      conditions.first &lt;&lt; &quot; and id != ?&quot;
+      conditions       &lt;&lt; id
+    end
+    while self.class.count(:all, :conditions =&gt; conditions) &gt; 0
+      conditions[1] = &quot;#{base}-#{counter += 1}&quot;
+      send(&quot;#{self.class.permalink_field}=&quot;, conditions[1])
+    end
+  end
+
+  def create_permalink_for(attr_names)
+    attr_names.collect do |attr_name| 
+      PermalinkFu.escape(send(attr_name).to_s)
+    end.join('-')
   end
 end
 </diff>
      <filename>lib/permalink_fu.rb</filename>
    </modified>
    <modified>
      <diff>@@ -2,36 +2,59 @@ require 'test/unit'
 require File.join(File.dirname(__FILE__), '../lib/permalink_fu')
 
 class MockModel
-  extend PermalinkFu
+  include PermalinkFu
+  attr_accessor :id
   attr_accessor :title
   attr_accessor :permalink
   
-  def self.before_validation(&amp;block)
-    @@validation = block
+  def self.count(whatever, options = {})
+    if options[:conditions][1] == 'foo' || options[:conditions][1] == 'bar' || 
+      (options[:conditions][1] == 'bar-2' &amp;&amp; options[:conditions][2] != 2)
+      1
+    else
+      0
+    end
+  end
+  
+  def self.before_validation(method)
+    @@validation = method
   end
   
   def validate
-    @@validation.call self
+    send @@validation
     permalink
   end
-  
+
+  def new_record?
+    @id.nil?
+  end
+
   has_permalink :title
 end
 
 class MockModelExtra
-  extend PermalinkFu
+  include PermalinkFu
+  attr_accessor :id
   attr_accessor :title
   attr_accessor :extra
   attr_accessor :permalink
 
-  def self.before_validation(&amp;block)
-    @@validation = block
+  def self.count(*args)
+    0
+  end
+
+  def self.before_validation(method)
+    @@validation = method
   end
 
   def validate
-    @@validation.call self
+    send @@validation
     permalink
   end
+  
+  def new_record?
+    !@id.nil?
+  end
 
   has_permalink [:title, :extra]
 end
@@ -68,4 +91,23 @@ class PermalinkFuTest &lt; Test::Unit::TestCase
       end
     end
   end
+  
+  def test_should_create_unique_permalink
+    @m = MockModel.new
+    @m.permalink = 'foo'
+    @m.validate
+    assert_equal 'foo-2', @m.permalink
+    
+    @m.permalink = 'bar'
+    @m.validate
+    assert_equal 'bar-3', @m.permalink
+  end
+  
+  def test_should_not_check_itself_for_unique_permalink
+    @m = MockModel.new
+    @m.id = 2
+    @m.permalink = 'bar-2'
+    @m.validate
+    assert_equal 'bar-2', @m.permalink
+  end
 end</diff>
      <filename>test/permalink_fu_test.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>8f215a0ec831685c124c784ad618559f4a6ae9ea</id>
    </parent>
  </parents>
  <author>
    <name>technoweenie</name>
    <email>technoweenie@567b1171-46fb-0310-a4c9-b4bef9110e78</email>
  </author>
  <url>http://github.com/henrik/permalink_fu/commit/dd229ee5509da7e3159075ce988a77156a316a8e</url>
  <id>dd229ee5509da7e3159075ce988a77156a316a8e</id>
  <committed-date>2007-10-15T14:19:36-07:00</committed-date>
  <authored-date>2007-10-15T14:19:36-07:00</authored-date>
  <message>append incrementing numbers for duplicate guids

git-svn-id: http://svn.techno-weenie.net/projects/plugins/permalink_fu@2993 567b1171-46fb-0310-a4c9-b4bef9110e78</message>
  <tree>040d110c031cf2aae62b9e4dbcb6448e20c7fee4</tree>
  <committer>
    <name>technoweenie</name>
    <email>technoweenie@567b1171-46fb-0310-a4c9-b4bef9110e78</email>
  </committer>
</commit>
