Skip to content

Instantly share code, notes, and snippets.

@davidcelis
Last active December 23, 2015 02:39
Show Gist options
  • Save davidcelis/6568183 to your computer and use it in GitHub Desktop.
Save davidcelis/6568183 to your computer and use it in GitHub Desktop.
an concern
module Sluggable
extend ActiveSupport::Concern
included do
before_create :set_slug
validates :slug, uniqueness: { case_sensitive: false }
scope :from_param, -> param { find_by(slug: param) }
end
def to_param() slug end
private
def set_slug
number = 1
begin
slug = name.parameterize
slug << "-#{number}" if number > 1
number += 1
end while self.class.where(slug: slug).exists?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment