<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -1,3 +1,9 @@
+== 2.2.1 2009-10-23
+
+* 2 minor enhancements:
+  * slug cache now properly caches the slug sequence (closes GH issue #10)
+  * attr_protected is now only invoked on the cached_slug column if attr_accessible has not already been invoked. (closes GH issue #11)
+
 == 2.2.0 2009-10-19
 
 * 1 major enhancement:</diff>
      <filename>History.txt</filename>
    </modified>
    <modified>
      <diff>@@ -172,12 +172,19 @@ Then, redo the slugs:
 
   rake friendly_id:redo_slugs MODEL=User
 
+This feature exists largely to improve the performance of URL
+generation, the part of Rails where FriendlyId has the biggest
+performance impact. FriendlyId never queries against this column, so
+it's not necessary to add an index on it unless your application does.
+
+Two warnings when using this feature:
+
 *DO NOT* forget to redo the slugs, or else this feature will not work!
 
-Also note that this feature exists largely to improve the performance of URL
-generation, the part of Rails where FriendlyId has the biggest performance
-impact. FriendlyId never queries against this column, so it's not necessary to
-add an index on it unless your application does.
+Also, this feature uses +attr_protected+ to protect the cached_slug
+column, unless you have already invoked +attr_accessible+. So if you
+wish to use +attr_accessible+, you must invoke it BEFORE you invoke
++has_friendly_id+ in your class.
 
 ==== Using a custom column name:
 </diff>
      <filename>README.rdoc</filename>
    </modified>
    <modified>
      <diff>@@ -42,7 +42,7 @@ module FriendlyId
     # Options:
     # * &lt;tt&gt;:use_slug&lt;/tt&gt; - Defaults to false. Use slugs when you want to use a non-unique text field for friendly ids.
     # * &lt;tt&gt;:max_length&lt;/tt&gt; - Defaults to 255. The maximum allowed length for a slug.
-    # * &lt;tt&gt;:cache_column&lt;/tt&gt; - Defaults to nil. Use this column as a cache for generating to_param (experimental).
+    # * &lt;tt&gt;:cache_column&lt;/tt&gt; - Defaults to nil. Use this column as a cache for generating to_param (experimental) Note that if you use this option, any calls to +attr_accessible+ must be made BEFORE any calls to has_friendly_id in your class.
     # * &lt;tt&gt;:strip_diacritics&lt;/tt&gt; - Defaults to false. If true, it will remove accents, umlauts, etc. from western characters.
     # * &lt;tt&gt;:strip_non_ascii&lt;/tt&gt; - Defaults to false. If true, it will all non-ascii ([^a-z0-9]) characters.
     # * &lt;tt&gt;:reserved&lt;/tt&gt; - Array of words that are reserved and can't be used as friendly_id's. For sluggable models, if such a word is used, it will raise a FriendlyId::SlugGenerationError. Defaults to [&quot;new&quot;, &quot;index&quot;].
@@ -79,11 +79,13 @@ module FriendlyId
         extend SluggableClassMethods
         include SluggableInstanceMethods
         before_save :set_slug
+        after_save :set_slug_cache
         if block_given?
           write_inheritable_attribute :slug_normalizer_block, block
         end
         if options[:cache_column]
-          attr_protected options[:cache_column].to_sym
+          # only protect the column if the class is not already using attributes_accessible
+          attr_protected options[:cache_column].to_sym unless accessible_attributes
         end
       else
         require 'friendly_id/non_sluggable_class_methods'</diff>
      <filename>lib/friendly_id.rb</filename>
    </modified>
    <modified>
      <diff>@@ -118,12 +118,14 @@ private
       # If we're renaming back to a previously used friendly_id, delete the
       # slug so that we can recycle the name without having to use a sequence.
       slugs.find(:all, :conditions =&gt; {:name =&gt; slug_text, :scope =&gt; scope}).each { |s| s.destroy }
-      slug = slugs.build slug_attributes
-      if cache = friendly_id_options[:cache_column]
-        new_slug = slug.to_friendly_id
-        send(&quot;#{cache}=&quot;, new_slug) unless send(cache) == new_slug
-      end
-      slug
+      slugs.build slug_attributes
+    end
+  end
+
+  def set_slug_cache
+    if friendly_id_options[:cache_column] &amp;&amp; send(friendly_id_options[:cache_column]) != slug.to_friendly_id
+      send &quot;#{friendly_id_options[:cache_column]}=&quot;, slug.to_friendly_id
+      send :update_without_callbacks
     end
   end
 </diff>
      <filename>lib/friendly_id/sluggable_instance_methods.rb</filename>
    </modified>
    <modified>
      <diff>@@ -4,7 +4,7 @@ module FriendlyId #:nodoc:
   module Version #:nodoc:
     MAJOR = 2
     MINOR = 2
-    TINY = 0
+    TINY = 1
     STRING = [MAJOR, MINOR, TINY].join('.')
   end
 end</diff>
      <filename>lib/friendly_id/version.rb</filename>
    </modified>
    <modified>
      <diff>@@ -1,5 +1,5 @@
 # encoding: utf-8!
-# @paris.reload
+require &quot;mocha&quot;
 
 require File.dirname(__FILE__) + '/test_helper'
 
@@ -29,9 +29,22 @@ class CachedSlugModelTest &lt; Test::Unit::TestCase
     should &quot;protect the cached slug value&quot; do
       @paris.update_attributes(:my_slug =&gt; &quot;Madrid&quot;)
       @paris.reload
-      assert_equal(&quot;paris&quot;, @paris.my_slug)
+      assert_equal &quot;paris&quot;, @paris.my_slug
     end
 
+    should &quot;cache the incremented sequence for duplicate slug names&quot; do
+      @paris2 = City.create!(:name =&gt; &quot;Paris&quot;)
+      assert_equal 2, @paris2.slug.sequence
+      assert_equal &quot;paris--2&quot;, @paris2.my_slug
+    end
+
+    should &quot;not update the cached slug column if it has not changed&quot; do
+      @paris.population = 10_000_000
+      @paris.expects(:my_slug=).never
+      @paris.save
+    end
+
+
     context &quot;found by its friendly id&quot; do
 
       setup do</diff>
      <filename>test/cached_slug_test.rb</filename>
    </modified>
    <modified>
      <diff>@@ -1,3 +1,4 @@
 class City &lt; ActiveRecord::Base
+  attr_accessible :name
   has_friendly_id :name, :use_slug =&gt; true, :cache_column =&gt; 'my_slug'
 end</diff>
      <filename>test/models/city.rb</filename>
    </modified>
    <modified>
      <diff>@@ -42,6 +42,7 @@ ActiveRecord::Schema.define(:version =&gt; 1) do
 
   create_table &quot;cities&quot;, :force =&gt; true do |t|
     t.column &quot;name&quot;, &quot;string&quot;
+    t.column &quot;population&quot;, &quot;integer&quot;
     t.column &quot;my_slug&quot;, &quot;string&quot;
   end
 </diff>
      <filename>test/schema.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>f68f7c92f4b3416a96cdee5f69308b0b7fc70079</id>
    </parent>
  </parents>
  <author>
    <name>Norman Clarke</name>
    <email>norman@njclarke.com</email>
  </author>
  <url>http://github.com/norman/friendly_id/commit/6ec718bb9d90e2041aac0a2ddb8da9139cdb8808</url>
  <id>6ec718bb9d90e2041aac0a2ddb8da9139cdb8808</id>
  <committed-date>2009-10-23T09:22:52-07:00</committed-date>
  <authored-date>2009-10-23T09:18:53-07:00</authored-date>
  <message>* Resolved GH issues #10 and #11 (cached_slug column protection and slug sequence caching).</message>
  <tree>952aa13001a4309480b30f6b2ee19b98d0560fb9</tree>
  <committer>
    <name>Norman Clarke</name>
    <email>norman@njclarke.com</email>
  </committer>
</commit>
