# ============================================================================= # # Ants::Slug provides a flexible way to define a custom slug on a top of # Mongoid::Slug module, a few cases are covered: # # 1. if _slug is not defined, slug is autogenerated based on specified # configuration # 2. if _slug is defined it's used as default # 3. if _slug is empty slug is autogenerated as in step 1 # 4. if slug configuration field has changed value and _slug is set, _slug # is used as default # # ----------------------------------------------------------------------------- module Ants module Slug extend ActiveSupport::Concern included do include Mongoid::Slug # attributes field :_slug, default: '' # callbacks before_save :_populate_custom_slug # helpers def _populate_custom_slug self.slugs = [] if ! self._slug.empty? append_slug(self._slug) end return true end # override Mongoid::Slug method to workaround slug change def slug_builder _cur_slug = nil # if custom slug is defined use that if ! self._slug.empty? append_slug(self._slug) end # user defined slug if new_with_slugs? or persisted_with_slug_changes? _cur_slug = _slugs.last end # generate slug if the slug is no custom slug defined or does not exist _cur_slug || pre_slug_string end end end end