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

Version Path
mvpkit-1.1.2 app/models/concerns/mvp/slugification.rb
mvpkit-1.1.1 app/models/concerns/mvp/slugification.rb
mvpkit-1.1.0 app/models/concerns/mvp/slugification.rb
mvpkit-1.0.4 app/models/concerns/mvp/slugification.rb
mvpkit-1.0.3 app/models/concerns/mvp/slugification.rb
mvpkit-1.0.2 app/models/concerns/mvp/slugification.rb
mvpkit-1.0.1 app/models/concerns/mvp/slugification.rb
mvpkit-1.0.0 app/models/concerns/mvp/slugification.rb
mvpkit-0.8.1 app/models/concerns/mvp/slugification.rb
mvpkit-0.8.0 app/models/concerns/mvp/slugification.rb