Module: Sluggable
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/sluggable_ts.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
- #append_suffix(str, count) ⇒ Object
- #generate_slug! ⇒ Object
- #to_param ⇒ Object
- #to_slug(name) ⇒ Object
Instance Method Details
#append_suffix(str, count) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/sluggable_ts.rb', line 25 def append_suffix(str, count) if str.split('-').last.to_i != 0 #if the last is not a string(0), but an integer, i.e. the "1" in Apple Pie 1" return str.split('-').slice(0...-1).join('-') + "-" + count.to_s #remove the last string of "str.split('-'). And then join all the strings with '-', and add an count(number) at last. else return str + "-" + count.to_s end end |
#generate_slug! ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/sluggable_ts.rb', line 13 def generate_slug! the_slug = to_slug(self.send(self.class.slug_column.to_sym)) obj = self.class.find_by slug: the_slug #check if the title/slug already exists count = 2 while obj && obj != self #while obj exists && obj is not the one we're currently handling with the_slug = append_suffix(the_slug, count) #add suffix(count/number) to the_slug obj = self.class.find_by slug: the_slug #check again if the_slug is already taken count += 1 #when it's ture(the_slug is taken), count accumulates end self.slug = the_slug.downcase end |
#to_param ⇒ Object
9 10 11 |
# File 'lib/sluggable_ts.rb', line 9 def to_param self.slug end |
#to_slug(name) ⇒ Object
33 34 35 36 37 38 |
# File 'lib/sluggable_ts.rb', line 33 def to_slug(name) str = name.strip #remove all the whitespace leading and trailing the string str.gsub! /\s*[^A-Za-z0-9]\s*/, "-" # replace all symbols and numbers with "-" str.gsub! /-+/, "-" #replace multiple "-", with only 1 "-" str.downcase end |