Module: Sluggable
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/sluggable_tim.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
29 30 31 32 33 34 35 |
# File 'lib/sluggable_tim.rb', line 29 def append_suffix(str, count) if str.split('-').last.to_i != 0 str.split('-').slice(0...-1).join('-') + "-" + count.to_s else str + "-" + count.to_s #will always be "-2" end end |
#generate_slug! ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/sluggable_tim.rb', line 15 def generate_slug! the_slug = to_slug(self.send(self.class.slug_col_name.to_sym)) obj = self.class.find_by(:slug => the_slug) count = 2 while obj && obj != self the_slug = append_suffix(the_slug, count) obj = self.class.find_by(:slug => the_slug) count += 1 end self.slug = the_slug.downcase end |
#to_param ⇒ Object
11 12 13 |
# File 'lib/sluggable_tim.rb', line 11 def to_param self.slug end |
#to_slug(name) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/sluggable_tim.rb', line 37 def to_slug(name) str = name.strip str.gsub! /\s*[^A-Za-z0-9]\s*/, '-' str.gsub! /-+/,"-" str.downcase end |