Skip to content

Instantly share code, notes, and snippets.

@sadjow
Forked from brennovich/slugable.rb
Created September 18, 2013 17:14
Show Gist options
  • Save sadjow/6612380 to your computer and use it in GitHub Desktop.
Save sadjow/6612380 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/
before_save :generate_slug
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
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment