Sha256: 96ba0797d378172d9d1d9a5b2a78f8c3f6436cc8efab60554ffd7dcb8f0d0c4c

Contents?: true

Size: 544 Bytes

Versions: 4

Compression:

Stored size: 544 Bytes

Contents

module MinimumViableProduct
  module Slugification
    extend ActiveSupport::Concern

    module ClassMethods
      def slugify(name, opts={})
        _opts = opts.reverse_merge(:if => lambda{ |obj| obj.send("#{name}_changed?".to_sym) })
        before_validation -> {
          self.slug = slugify(self.send(name.to_sym))
          self.slug += SecureRandom.uuid if self.class.find_by(slug: self.slug).present?
        }, _opts
      end
    end

    private

    def slugify(str)
      str.gsub(/[^a-zA-Z0-9]/,'-').downcase
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
minimum_viable_product-0.4.2 app/models/concerns/minimum_viable_product/slugification.rb
minimum_viable_product-0.4.1 app/models/concerns/minimum_viable_product/slugification.rb
minimum_viable_product-0.4.0 app/models/concerns/minimum_viable_product/slugification.rb
minimum_viable_product-0.3.4 app/models/concerns/minimum_viable_product/slugification.rb