Permalink
Browse files

add a seed to permalinks, fix bug where setting permalink to nil woul…

…d set it to a random hash
  • Loading branch information...
1 parent dec57c9 commit f07bdca25dc6cf3aeb4b9bf0283a40a09f02c78d @technoweenie committed Oct 21, 2008
Showing with 18 additions and 21 deletions.
  1. +12 −15 lib/permalink_fu.rb
  2. +6 −6 test/permalink_fu_test.rb
View
@@ -12,22 +12,19 @@ class << self
# This method does the actual permalink escaping.
def escape(string)
- begin
- result = ((translation_to && translation_from) ? Iconv.iconv(translation_to, translation_from, string) : string).to_s
- result.gsub!(/[^\x00-\x7F]+/, '') # Remove anything non-ASCII entirely (e.g. diacritics).
- result.gsub!(/[^\w_ \-]+/i, '') # Remove unwanted chars.
- result.gsub!(/[ \-]+/i, '-') # No more than one of the separator in a row.
- result.gsub!(/^\-|\-$/i, '') # Remove leading/trailing separator.
- result.downcase!
-
- result.blank? ? random_permalink : result
- rescue
- random_permalink
- end
+ result = ((translation_to && translation_from) ? Iconv.iconv(translation_to, translation_from, string) : string).to_s
+ result.gsub!(/[^\x00-\x7F]+/, '') # Remove anything non-ASCII entirely (e.g. diacritics).
+ result.gsub!(/[^\w_ \-]+/i, '') # Remove unwanted chars.
+ result.gsub!(/[ \-]+/i, '-') # No more than one of the separator in a row.
+ result.gsub!(/^\-|\-$/i, '') # Remove leading/trailing separator.
+ result.downcase!
+ result.size.zero? ? random_permalink(string) : result
+ rescue
+ random_permalink(string)
end
- def random_permalink
- Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join )
+ def random_permalink(seed = nil)
+ Digest::SHA1.hexdigest("#{seed}#{Time.now.to_s.split(//).sort_by {rand}}")
end
end
@@ -96,7 +93,7 @@ class << base
def define_attribute_methods_with_permalinks
if value = define_attribute_methods_without_permalinks
- evaluate_attribute_method permalink_field, "def #{self.permalink_field}=(new_value);write_attribute(:#{self.permalink_field}, PermalinkFu.escape(new_value));end", "#{self.permalink_field}="
+ evaluate_attribute_method permalink_field, "def #{self.permalink_field}=(new_value);write_attribute(:#{self.permalink_field}, new_value ? PermalinkFu.escape(new_value) : nil);end", "#{self.permalink_field}="
end
value
end
View
@@ -192,14 +192,14 @@ class MockModelExtra < BaseModel
class PermalinkFuTest < Test::Unit::TestCase
@@samples = {
- 'This IS a Tripped out title!!.!1 (well/ not really)' => 'this-is-a-tripped-out-title1-well-not-really',
- '////// meph1sto r0x ! \\\\\\' => 'meph1sto-r0x',
- 'āčēģīķļņū' => 'acegiklnu',
- '中文測試 chinese text' => 'chinese-text',
- 'fööbär' => 'foobar'
+ 'This IS a Tripped out title!!.!1 (well/ not really)'.freeze => 'this-is-a-tripped-out-title1-well-not-really'.freeze,
+ '////// meph1sto r0x ! \\\\\\'.freeze => 'meph1sto-r0x'.freeze,
+ 'āčēģīķļņū'.freeze => 'acegiklnu'.freeze,
+ '中文測試 chinese text'.freeze => 'chinese-text'.freeze,
+ 'fööbär'.freeze => 'foobar'.freeze
}
- @@extra = { 'some-)()()-ExtRa!/// .data==?> to \/\/test' => 'some-extra-data-to-test' }
+ @@extra = { 'some-)()()-ExtRa!/// .data==?> to \/\/test'.freeze => 'some-extra-data-to-test'.freeze }
def test_should_escape_permalinks
@@samples.each do |from, to|

0 comments on commit f07bdca

Please sign in to comment.