Sha256: fc1d012b888e7037c38a2acb30b25aba31f8199835dfce20d6d99a62a541b0a2
Contents?: true
Size: 1.35 KB
Versions: 21
Compression:
Stored size: 1.35 KB
Contents
module Slug extend ActiveSupport::Concern included do after_save :sluggify_slug def self.find(input) input.to_i == 0 ? find_by_slug(input) : super end 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
21 entries across 21 versions & 2 rubygems
Version | Path |
---|---|
cambium-0.3.0 | lib/generators/cambium/templates/app/models/concerns/slug.rb |