Sha256: b099e71c2242af3b452094c924cd20b582697a78e9cf9bda88ebbc088d345aee
Contents?: true
Size: 568 Bytes
Versions: 10
Compression:
Stored size: 568 Bytes
Contents
module MVP module Slugification extend ActiveSupport::Concern module ClassMethods def slugify(name, opts={}) before_validation -> { return true if self.slug.present? self.slug = slugify(self.send(name.to_sym)) self.slug += "-#{SecureRandom.uuid}" if self.class.find_by(slug: self.slug).present? }, opts validates :slug, uniqueness: true end end private def slugify(str) str.gsub(/[^a-zA-Z0-9]+/,'-') .gsub(/^[-]+|[-]+$/,'') .downcase end end end
Version data entries
10 entries across 10 versions & 1 rubygems