Skip to content

Commit

Permalink
Fix for history slug creation. Resolves issue norman#206.
Browse files Browse the repository at this point in the history
  • Loading branch information
norman committed Jan 10, 2012
1 parent 98310bd commit 1fc14a5
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/friendly_id/history.rb
Expand Up @@ -65,20 +65,20 @@ def self.included(model_class)
@friendly_id_config.use :slugged
has_many :slugs, :as => :sluggable, :dependent => :destroy,
:class_name => Slug.to_s, :order => "#{Slug.quoted_table_name}.id DESC"
before_save :build_slug
after_save :create_slug
relation_class.send :include, FinderMethods
end
end

private

def build_slug
return unless should_generate_new_friendly_id?
def create_slug
return if slugs.first.try(:slug) == friendly_id
# Allow reversion back to a previously used slug
relation = slugs.where(:slug => friendly_id)
result = relation.select("id").lock(true).all
relation.delete_all unless result.empty?
slugs.build :slug => friendly_id
slugs.create! :slug => friendly_id
end

# Adds a finder that explictly uses slugs from the slug table.
Expand Down

0 comments on commit 1fc14a5

Please sign in to comment.