Skip to content

Instantly share code, notes, and snippets.

@kzaitsev
Last active August 29, 2015 13:57
Show Gist options
  • Save kzaitsev/9703961 to your computer and use it in GitHub Desktop.
Save kzaitsev/9703961 to your computer and use it in GitHub Desktop.
module Slugeable
extend ActiveSupport::Concern
included do
extend FriendlyId
friendly_id :slug_candidates, use: :slugged
def slug_candidates
if defined?(self.posted_at)
"#{posted_at}-#{name}"
elsif defined?(self.date)
"#{date}-#{name}"
else
:name
end
end
def normalize_friendly_id(name)
if slug.blank?
name.to_slug.normalize! :transliterations => :russian
else
slug.to_slug.normalize! :transliterations => :russian
end
end
def should_generate_new_friendly_id?
new_record? || slug.blank? || self.class.find_by_slug(slug).nil?
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment