0
@@ -172,6 +172,7 @@ module FriendlyId
0
raise ActiveRecord::RecordNotFound, "Couldn't find all #{ name.pluralize } with IDs (#{ ids_and_names * ', ' }) AND #{ sanitize_sql options[:conditions] } (found #{ results.size } results, but was looking for #{ expected_size })" if results.size != expected_size
0
+ # FIXME set the actual slugs, not the name to "lazy load" later, because that's not lazy!
0
result = results.find { |r| r.id == slug.sluggable_id } and
0
result.finder_slug_name = slug.name
0
@@ -183,6 +184,8 @@ module FriendlyId
0
module SluggableInstanceMethods
0
+ NUM_CHARS_RESERVED_FOR_FRIENDLY_ID_EXTENSION = 2
0
attr_accessor :finder_slug_name
0
@@ -192,7 +195,7 @@ module FriendlyId
0
# Was the record found using one of its friendly ids?
0
def found_using_friendly_id?
0
# Was the record found using its numeric id?
0
@@ -212,7 +215,7 @@ module FriendlyId
0
# Returns the friendly id.
0
-
finder_slug_name or slug.name
0
alias best_id friendly_id
0
@@ -230,10 +233,15 @@ module FriendlyId
0
# Generate the text for the friendly id, ensuring no duplication.
0
def generate_friendly_id
0
- slug_text = truncated_friendly_id_base
0
- count = Slug.count_matches slug_text, self.class.name, :all, :conditions => "sluggable_id <> #{ id.to_i }"
0
- count += 1 if self.class.friendly_id_options[:reserved].include?(slug_text)
0
- count == 0 ? slug_text : generate_friendly_id_with_extension(slug_text, count)
0
+ base = friendly_id_base
0
+ opts = self.class.friendly_id_options
0
+ if base.length > opts[:max_length]
0
+ base = base[0...opts[:max_length] - NUM_CHARS_RESERVED_FOR_FRIENDLY_ID_EXTENSION]
0
+ if opts[:reserved].include?(base)
0
+ Slug.get_best_name(base, self.class)
0
# Set the slug using the generated friendly id.
0
@@ -241,12 +249,13 @@ module FriendlyId
0
if self.class.friendly_id_options[:use_slug]
0
@most_recent_slug = nil
0
slug_text = generate_friendly_id
0
+ # Avoids regenerating slug over and over again.
0
+ # FIXME This could perform pretty badly if a model has tons of similarly-named slugs
0
+ return if slug && slug.succ == slug_text
0
if slugs.empty? || slugs.first.name != slug_text
0
- previous_slug = slugs.find_by_name
slug_text0
+ previous_slug = slugs.find_by_name
friendly_id_base0
previous_slug.destroy if previous_slug
0
- slugs.build :name => slug_text
0
+ slugs.build :name => generate_friendly_id
0
@@ -267,33 +276,13 @@ module FriendlyId
0
- NUM_CHARS_RESERVED_FOR_FRIENDLY_ID_EXTENSION = 2
0
- r
aise RuntimeError, 'No slug name is set' if !@finder_slug_name
0
+ r
eturn false if !@finder_slug_name
0
slug = Slug.find(:first, :conditions => {:sluggable_id => id, :name => @finder_slug_name})
0
- def truncated_friendly_id_base
0
- max_length = friendly_id_options[:max_length]
0
- slug_text = friendly_id_base[0, max_length - NUM_CHARS_RESERVED_FOR_FRIENDLY_ID_EXTENSION]
0
- # Reserve a few spaces at the end of the slug for the counter extension.
0
- # This is to avoid generating slugs longer than the maxlength when an
0
- POSSIBILITIES = 10 ** NUM_CHARS_RESERVED_FOR_FRIENDLY_ID_EXTENSION - 1
0
- def generate_friendly_id_with_extension(slug_text, count)
0
- count <= POSSIBILITIES or
0
- raise FriendlyId::SlugGenerationError.new("slug text #{slug_text} goes over limit for similarly named slugs")
0
- slug_text = "#{ truncated_friendly_id_base }-#{ count + 1 }"
0
- count = Slug.count_matches slug_text, self.class.name, :all, :conditions => "sluggable_id <> #{ id.to_i }"
0
- count > 0 ? "#{ truncated_friendly_id_base }-#{ count + 1 }" : slug_text
0
\ No newline at end of file