Skip to content

Commit

Permalink
remove id from permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
flyerhzm committed Jan 24, 2010
1 parent fd6dede commit 996b6f7
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.textile
Expand Up @@ -62,7 +62,7 @@ Or
<pre><code>
class Post < ActiveRecord::Base
def to_param
permalink
"#{id}-#{permalink}"
end
end
</code></pre>
Expand Down
1 change: 1 addition & 0 deletions init.rb
@@ -0,0 +1 @@
ActiveRecord::Base.send :include, ChinesePermalink
3 changes: 1 addition & 2 deletions lib/chinese_permalink.rb
Expand Up @@ -30,8 +30,7 @@ def create_permalink
end

english_permalink = remove_duplicate_dash(remove_tailing_dash(remove_non_ascii(remove_space(remove_punctuation(english_permalink))))).downcase
permalink = id.to_s + '-' + english_permalink
self.update_attribute(self.class.permalink_field, permalink)
self.update_attribute(self.class.permalink_field, english_permalink)
end
end

Expand Down
26 changes: 13 additions & 13 deletions test/chines_permalink_test.rb
Expand Up @@ -64,50 +64,50 @@ def teardown

def test_simple_chinese_title
post = Post.create(:title => '中国人')
assert_equal "#{post.id}-chinese", post.permalink
assert_equal "chinese", post.permalink

post = Post.create(:title => '我是中国人')
assert_equal "#{post.id}-i-am-a-chinese", post.permalink
assert_equal "i-am-a-chinese", post.permalink
end

def test_chinese_title_with_dash
post = Post.create(:title => '我是中国人——上海')
assert_equal "#{post.id}-i-am-a-chinese-shanghai", post.permalink
assert_equal "i-am-a-chinese-shanghai", post.permalink

post = Post.create(:title => '我是中国人──上海')
assert_equal "#{post.id}-i-am-a-chinese-shanghai", post.permalink
assert_equal "i-am-a-chinese-shanghai", post.permalink

post = Post.create(:title => '上海+中国')
assert_equal "#{post.id}-shanghai-china", post.permalink
assert_equal "shanghai-china", post.permalink

post = Post.create(:title => '上海/中国')
assert_equal "#{post.id}-shanghai-china", post.permalink
assert_equal "shanghai-china", post.permalink

post = Post.create(:title => '“工作”')
assert_equal "#{post.id}-work", post.permalink
assert_equal "work", post.permalink

post = Post.create(:title => '妈妈的礼物')
assert_equal "#{post.id}-moms-gift", post.permalink
assert_equal "moms-gift", post.permalink

post = Post.create(:title => '宝洁')
assert_equal "#{post.id}-procter-gamble", post.permalink
assert_equal "procter-gamble", post.permalink

post = Post.create(:title => '自我介绍')
assert_equal "#{post.id}-self-introduction", post.permalink
assert_equal "self-introduction", post.permalink
end

def test_chinese_category_and_title
post = CategoryPost.create(:title => '我是中国人', :category => '介绍')
assert_equal "#{post.id}-introduction-i-am-a-chinese", post.permalink
assert_equal "introduction-i-am-a-chinese", post.permalink
end

def test_complicated_title_with_before_methods
post = ComplicatedBeforePost.create(:title => 'C#语言')
assert_equal "#{post.id}-c-sharp-language", post.permalink
assert_equal "c-sharp-language", post.permalink
end

def test_complicated_title_with_after_methods
post = ComplicatedAfterPost.create(:title => '宝洁')
assert_equal "#{post.id}-pg", post.permalink
assert_equal "pg", post.permalink
end
end

0 comments on commit 996b6f7

Please sign in to comment.