Skip to content

Instantly share code, notes, and snippets.

@uzzz
Created January 23, 2011 21:53
Show Gist options
  • Save uzzz/792481 to your computer and use it in GitHub Desktop.
Save uzzz/792481 to your computer and use it in GitHub Desktop.
diff --git a/lib/friendly_id/active_record_adapter/configuration.rb b/lib/friendly_id/active_record_adapter/configuration.rb
index b1006ab..1992389 100644
--- a/lib/friendly_id/active_record_adapter/configuration.rb
+++ b/lib/friendly_id/active_record_adapter/configuration.rb
@@ -19,6 +19,12 @@ module FriendlyId
attr_reader :custom_cache_column
+ attr_accessor :update_timestamp
+
+ def update_timestamp?
+ update_timestamp.nil? || update_timestamp ? true : false
+ end
+
def cache_column
return @cache_column if defined?(@cache_column)
@cache_column = autodiscover_cache_column
diff --git a/lib/friendly_id/active_record_adapter/slugged_model.rb b/lib/friendly_id/active_record_adapter/slugged_model.rb
index ba0f3c3..9f53ca4 100644
--- a/lib/friendly_id/active_record_adapter/slugged_model.rb
+++ b/lib/friendly_id/active_record_adapter/slugged_model.rb
@@ -59,16 +59,28 @@ module FriendlyId
# Reset the cached friendly_id.
def set_slug_cache
if new_cache_needed?
- begin
- send "#{friendly_id_config.cache_column}=", slug.to_friendly_id
- update_without_callbacks
- rescue ActiveRecord::StaleObjectError
- reload
- retry
+ if friendly_id_config.update_timestamp?
+ regular_update
+ else
+ update_without_timespamping
end
end
end
+ def update_with_timestamping
+ begin
+ send "#{friendly_id_config.cache_column}=", slug.to_friendly_id
+ update_without_callbacks
+ rescue ActiveRecord::StaleObjectError
+ reload
+ retry
+ end
+ end
+
+ def update_without_timespamping
+ self.class.update_all({:cached_slug => slug.to_friendly_id}, {:id => self.id})
+ end
+
def update_scope
return unless slug && scope_changed?
self.class.transaction do
@@ -108,4 +120,4 @@ module FriendlyId
end
end
end
-end
\ No newline at end of file
+end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment