module Mongoid::Slug::ClassMethods
Public Instance Methods
Find documents by slugs.
A document matches if any of its slugs match one of the supplied params.
A document matching multiple supplied params will be returned only once.
If any supplied param does not match a document a Mongoid::Errors::DocumentNotFound will be raised.
@example Find by a slug.
Model.find_by_slug!('some-slug')
@example Find by multiple slugs.
Model.find_by_slug!('some-slug', 'some-other-slug')
@param [ Array<Object> ] args The slugs to search for.
@return [ Array<Document>, Document ] The matching document(s).
# File lib/mongoid/slug.rb, line 133 def find_by_slug!(*args) with_default_scope.find_by_slug!(*args) end
Indicates whether or not the document includes Mongoid::Paranoia
This can be replaced with .paranoid? method once the following PRs are merged:
@return [ Array<Document>, Document ] Whether the document is paranoid
# File lib/mongoid/slug.rb, line 148 def is_paranoid_doc? !!(defined?(::Mongoid::Paranoia) && self < ::Mongoid::Paranoia) end
# File lib/mongoid/slug.rb, line 104 def look_like_slugs?(*args) with_default_scope.look_like_slugs?(*args) end
# File lib/mongoid/slug.rb, line 137 def queryable scope_stack.last || Criteria.new(self) # Use Mongoid::Slug::Criteria for slugged documents. end
@overload slug(*fields)
Sets one ore more fields as source of slug. @param [Array] fields One or more fields the slug should be based on. @yield If given, the block is used to build a custom slug.
@overload slug(*fields, options)
Sets one ore more fields as source of slug. @param [Array] fields One or more fields the slug should be based on. @param [Hash] options @param options [Boolean] :history Whether a history of changes to the slug should be retained. When searched by slug, the document now matches both past and present slugs. @param options [Boolean] :permanent Whether the slug should be immutable. Defaults to `false`. @param options [Array] :reserve` A list of reserved slugs @param options :scope [Symbol] a reference association or field to scope the slug by. Embedded documents are, by default, scoped by their parent. @yield If given, a block is used to build a slug.
@example A custom builder
class Person include Mongoid::Document include Mongoid::Slug field :names, :type => Array slug :names do |doc| doc.names.join(' ') end end
# File lib/mongoid/slug.rb, line 58 def slug(*fields, &block) options = fields.extract_options! self.slug_scope = options[:scope] self.reserved_words = options[:reserve] || Set.new(["new", "edit"]) self.slugged_attributes = fields.map &:to_s self.history = options[:history] self.by_model_type = options[:by_model_type] field :_slugs, type: Array, default: [], localize: options[:localize] alias_attribute :slugs, :_slugs # Set index unless embedded? index(*Mongoid::Slug::Index.build_index(self.slug_scope_key, self.by_model_type)) end #-- Why is it necessary to customize the slug builder? default_url_builder = lambda do |cur_object| cur_object.slug_builder.to_url end self.url_builder = block_given? ? block : default_url_builder #-- always create slug on create #-- do not create new slug on update if the slug is permanent if options[:permanent] set_callback :create, :before, :build_slug else set_callback :save, :before, :build_slug, :if => :slug_should_be_rebuilt? end # If paranoid document: # - include shim to add callbacks for restore method # - unset the slugs on destroy # - recreate the slug on restore # - force reset the slug when saving a destroyed paranoid document, to ensure it stays unset in the database if is_paranoid_doc? self.send(:include, Mongoid::Slug::Paranoia) unless self.respond_to?(:before_restore) set_callback :destroy, :after, :unset_slug! set_callback :restore, :before, :set_slug! set_callback :save, :before, :reset_slug!, :if => :paranoid_deleted? set_callback :save, :after, :clear_slug!, :if => :paranoid_deleted? end end
Returns the scope key for indexing, considering associations
@return [ Array<Document>, Document ] Whether the document is paranoid
# File lib/mongoid/slug.rb, line 111 def slug_scope_key return nil unless self.slug_scope self.reflect_on_association(self.slug_scope).try(:key) || self.slug_scope end