Sha256: a2ec8bf35c523489ced268b18b909cd63f604dfe8572e6656bf417ba8075ed13
Contents?: true
Size: 1.27 KB
Versions: 5
Compression:
Stored size: 1.27 KB
Contents
module Slug extend ActiveSupport::Concern included do after_save :sluggify_slug end def sluggify_slug if slug.blank? update_column(:slug, create_slug) else new_slug = slug.gsub(/[^a-zA-Z0-9 \-]/, "") # remove all bad characters new_slug.gsub!(/\ /, "-") # replace spaces with underscores new_slug.gsub!(/\-+/, "-") # replace repeating underscores update_column(:slug, new_slug) unless slug == new_slug end end def create_slug slug = self.title.downcase.gsub(/\&/, ' and ') # & -> and slug.gsub!(/[^a-zA-Z0-9 \-]/, "") # remove all bad characters slug.gsub!(/\ /, "-") # replace spaces with underscores slug.gsub!(/\-+/, "-") # replace repeating underscores dups = self.class.name.constantize.where(:slug => slug) if dups.count == 1 and dups.first != self if self.idx.present? slug = "#{slug}-#{self.idx}" else slug = "#{slug}-#{self.id}" end end slug end def make_slug_unique(slug) dups = self.class.name.constantize.where(:slug => slug) if dups.count == 1 and dups.first != self if self.idx.present? slug = "#{slug}-#{self.idx}" else slug = "#{slug}-#{self.id}" end end slug end def to_param slug end end
Version data entries
5 entries across 5 versions & 1 rubygems