Sha256: d29e64887a273fec1f717a4abba927215d498b88c0d089fb709956468a532f98

Contents?: true

Size: 1.48 KB

Versions: 24

Compression:

Stored size: 1.48 KB

Contents

# =============================================================================
#
# 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




Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
ants-0.3.14 lib/concerns/ants/slug.rb
ants-0.3.13 lib/concerns/ants/slug.rb
ants-0.3.12 lib/concerns/ants/slug.rb
ants-0.3.11 lib/concerns/ants/slug.rb
ants-0.3.10 lib/concerns/ants/slug.rb
ants-0.3.9 lib/concerns/ants/slug.rb
ants-0.3.5 lib/concerns/ants/slug.rb
ants-0.3.4 lib/concerns/ants/slug.rb
ants-0.3.3 lib/concerns/ants/slug.rb
ants-0.3.2 lib/concerns/ants/slug.rb
ants-0.3.1 lib/concerns/ants/slug.rb
ants-0.3.0 lib/concerns/ants/slug.rb
ants-0.2.8 lib/concerns/ants/slug.rb
ants-0.2.7 lib/concerns/ants/slug.rb
ants-0.2.6 lib/concerns/ants/slug.rb
ants-0.2.5 lib/concerns/ants/slug.rb
ants-0.2.4 lib/concerns/ants/slug.rb
ants-0.2.3 lib/concerns/ants/slug.rb
ants-0.2.2 lib/concerns/ants/slug.rb
ants-0.2.1 lib/concerns/ants/slug.rb