Permalink
Browse files

Title slug in snippet url

  • Loading branch information...
1 parent d69b68a commit 5f812c35e58804647b0879a3c0982b33757f90ec @PavelTyk committed Mar 25, 2012
Showing with 85 additions and 2 deletions.
  1. +4 −1 app/models/snippet.rb
  2. +68 −0 lib/transliterator.rb
  3. +13 −1 spec/models/snippet_spec.rb
View
5 app/models/snippet.rb
@@ -19,6 +19,10 @@ def description_html
RDiscount.new(description, :filter_html, :safelink, :no_pseudo_protocols, :smart, :autolink).to_html
end
+ def to_param
+ "#{id} #{Transliterator.transliterate(title.to_s)}".parameterize
+ end
+
private
def self.conditions_array_for_description(args)
@@ -33,5 +37,4 @@ def self.match_operator
else 'LIKE'
end
end
-
end
View
68 lib/transliterator.rb
@@ -0,0 +1,68 @@
+# -*- encoding: utf-8 -*-
+# Borrowed from yaroslav / russian gem
+
+module Transliterator
+ extend self
+
+ # Transliteration heavily based on rutils gem by Julian "julik" Tarkhanov and Co.
+ # <http://rutils.rubyforge.org/>
+ # Cleaned up and optimized.
+
+ LOWER_SINGLE = {
+ "і"=>"i","ґ"=>"g","ё"=>"yo",""=>"#","є"=>"e",
+ "ї"=>"yi","а"=>"a","б"=>"b",
+ "в"=>"v","г"=>"g","д"=>"d","е"=>"e","ж"=>"zh",
+ "з"=>"z","и"=>"i","й"=>"y","к"=>"k","л"=>"l",
+ "м"=>"m","н"=>"n","о"=>"o","п"=>"p","р"=>"r",
+ "с"=>"s","т"=>"t","у"=>"u","ф"=>"f","х"=>"h",
+ "ц"=>"ts","ч"=>"ch","ш"=>"sh","щ"=>"sch","ъ"=>"'",
+ "ы"=>"y","ь"=>"","э"=>"e","ю"=>"yu","я"=>"ya",
+ }
+ LOWER_MULTI = {
+ "ье"=>"ie",
+ "ьё"=>"ie",
+ }
+
+ UPPER_SINGLE = {
+ "Ґ"=>"G","Ё"=>"YO","Є"=>"E","Ї"=>"YI","І"=>"I",
+ "А"=>"A","Б"=>"B","В"=>"V","Г"=>"G",
+ "Д"=>"D","Е"=>"E","Ж"=>"ZH","З"=>"Z","И"=>"I",
+ "Й"=>"Y","К"=>"K","Л"=>"L","М"=>"M","Н"=>"N",
+ "О"=>"O","П"=>"P","Р"=>"R","С"=>"S","Т"=>"T",
+ "У"=>"U","Ф"=>"F","Х"=>"H","Ц"=>"TS","Ч"=>"CH",
+ "Ш"=>"SH","Щ"=>"SCH","Ъ"=>"'","Ы"=>"Y","Ь"=>"",
+ "Э"=>"E","Ю"=>"YU","Я"=>"YA",
+ }
+ UPPER_MULTI = {
+ "ЬЕ"=>"IE",
+ "ЬЁ"=>"IE",
+ }
+
+ LOWER = (LOWER_SINGLE.merge(LOWER_MULTI)).freeze
+ UPPER = (UPPER_SINGLE.merge(UPPER_MULTI)).freeze
+ MULTI_KEYS = (LOWER_MULTI.merge(UPPER_MULTI)).keys.sort_by {|s| s.length}.reverse.freeze
+
+ # Transliterate a string with russian characters
+ #
+ # Возвращает строку, в которой все буквы русского алфавита заменены на похожую по звучанию латиницу
+ def transliterate(str)
+ chars = str.scan(%r{#{MULTI_KEYS.join '|'}|\w|.})
+
+ result = ""
+
+ chars.each_with_index do |char, index|
+ if UPPER.has_key?(char) && LOWER.has_key?(chars[index+1])
+ # combined case
+ result << UPPER[char].downcase.capitalize
+ elsif UPPER.has_key?(char)
+ result << UPPER[char]
+ elsif LOWER.has_key?(char)
+ result << LOWER[char]
+ else
+ result << char
+ end
+ end
+
+ result
+ end
+end
View
14 spec/models/snippet_spec.rb
@@ -39,4 +39,16 @@
snippet.description_html.should be_nil
end
end
-end
+
+ describe '#to_param' do
+ it 'returns id + title' do
+ snippet = Snippet.make(:description => 'This is a test')
+ snippet.to_param.should == "#{snippet.id}-this-is-a-test"
+ end
+
+ it 'works well with russian :)' do
+ snippet = Snippet.make(:description => 'Это тест')
+ snippet.to_param.to_s.should == "#{snippet.id}-eto-test"
+ end
+ end
+end

0 comments on commit 5f812c3

Please sign in to comment.