Sha256: 000c18c8215b46b96264a44ac8dce9ef402559aa28328a4709d0c0bf496c1bee

Contents?: true

Size: 790 Bytes

Versions: 1

Compression:

Stored size: 790 Bytes

Contents

module ManualSlug::Mongoid
  extend ActiveSupport::Concern
  include Mongoid::Slug

  def text_slug
    self._slugs.empty? ? '' : self._slugs.last
  end
  def text_slug=(slug)
    if slug.blank?
      self._slugs = []
    else
      self._slugs.delete(slug)
      self._slugs << slug
    end
  end

  module ClassMethods
    def manual_slug(field, options = {}, callback = true)
      options.merge!(permanent: true, history: true)
      slug field, options

      # we will create slugs manually when needed
      skip_callback :create, :before, :build_slug

      before_validation do
        self._slugs = self._slugs.map{ |s| s.strip }.select {|s| !s.blank? }

        if self._slugs.empty?
          self.build_slug
        end

        true
      end if callback
    end
  end

end


Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rocket_cms-0.5.14 app/models/concerns/manual_slug/mongoid.rb