Sha256: 13e9ae843d36e54c97042b98812779f270068d47b4a29e4770cf686f3cf5fc26

Contents?: true

Size: 585 Bytes

Versions: 6

Compression:

Stored size: 585 Bytes

Contents

module MinimumViableProduct
  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

6 entries across 6 versions & 1 rubygems

Version Path
minimum_viable_product-0.7.15 app/models/concerns/minimum_viable_product/slugification.rb
minimum_viable_product-0.7.14 app/models/concerns/minimum_viable_product/slugification.rb
minimum_viable_product-0.7.13 app/models/concerns/minimum_viable_product/slugification.rb
minimum_viable_product-0.7.12 app/models/concerns/minimum_viable_product/slugification.rb
minimum_viable_product-0.7.11 app/models/concerns/minimum_viable_product/slugification.rb
minimum_viable_product-0.7.10 app/models/concerns/minimum_viable_product/slugification.rb