Module: SluggableDoug
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/sluggable_doug.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
22 23 24 25 26 27 28 29 |
# File 'lib/sluggable_doug.rb', line 22 def append_suffix(str, count) if str.split('-').last.to_i != 0 #if the last item in the string is not a number #split the string on the dashes, slice off the last item, rejoin on the dashes, return str.split('-').slice(0...-1).join('-') + "-" + count.to_s else return str + "-" + count.to_s #just add -count to the end end end |
#generate_slug! ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/sluggable_doug.rb', line 9 def generate_slug! the_slug = to_slug(self.send(self.class.slug_column.to_sym)) #turn the current post title into a slug object = self.class.find_by slug: the_slug #try to find the object with the slug we just created count = 2 while object && object != self #while there is an object and it's not equal to the current object the_slug = append_suffix(the_slug, count) #detirmine the proper suffix and add it to slug object = self.class.find_by slug: the_slug #see if there is a post with the new slug count += 1 end self.slug = the_slug.downcase # assign the new slug to the object in lowercase end |
#to_param ⇒ Object
38 39 40 |
# File 'lib/sluggable_doug.rb', line 38 def to_param self.slug end |
#to_slug(name) ⇒ Object
31 32 33 34 35 36 |
# File 'lib/sluggable_doug.rb', line 31 def to_slug(name) str = name.strip str.gsub!(/\s*[^A-Za-z0-9]\s*/ , '-') str.gsub!(/-+/, '-') str.downcase end |