Skip to content

Instantly share code, notes, and snippets.

@brennovich
Last active December 23, 2015 09:09
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save brennovich/6612361 to your computer and use it in GitHub Desktop.
# app/models/concerns/slugable.rb
module Slugable
extend ActiveSupport::Concern
included do
validates_format_of :slug, :without => /^\d/
end
module ClassMethods
def find input
input.to_i == 0 ? find_by_slug(input) : super
end
end
def generate_slug
self.slug = title.parameterize
end
def to_param
slug
end
def save
generate_slug
super
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment