Module: Mongoid::Slug::ClassMethods
- Defined in:
- lib/mongoid/slug.rb
Instance Method Summary collapse
-
#find_by_slug!(*args) ⇒ Array<Document>, Document
Find documents by slugs.
-
#is_paranoid_doc? ⇒ Array<Document>, Document
Indicates whether or not the document includes Mongoid::Paranoia.
- #look_like_slugs?(*args) ⇒ Boolean
- #queryable ⇒ Object
- #slug(*fields, &block) ⇒ Object
-
#slug_scope_key ⇒ Array<Document>, Document
Returns the scope key for indexing, considering associations.
Instance Method Details
#find_by_slug!(*args) ⇒ Array<Document>, Document
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.
133 134 135 |
# File 'lib/mongoid/slug.rb', line 133 def find_by_slug!(*args) with_default_scope.find_by_slug!(*args) end |
#is_paranoid_doc? ⇒ Array<Document>, Document
Indicates whether or not the document includes Mongoid::Paranoia
This can be replaced with .paranoid? method once the following PRs are merged:
148 149 150 |
# File 'lib/mongoid/slug.rb', line 148 def is_paranoid_doc? !!(defined?(::Mongoid::Paranoia) && self < ::Mongoid::Paranoia) end |
#look_like_slugs?(*args) ⇒ Boolean
104 105 106 |
# File 'lib/mongoid/slug.rb', line 104 def look_like_slugs?(*args) with_default_scope.look_like_slugs?(*args) end |
#queryable ⇒ Object
137 138 139 |
# File 'lib/mongoid/slug.rb', line 137 def queryable scope_stack.last || Criteria.new(self) # Use Mongoid::Slug::Criteria for slugged documents. end |
#slug(*fields) { ... } ⇒ Object #slug(*fields, options) { ... } ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/mongoid/slug.rb', line 58 def slug(*fields, &block) = fields. self.slug_scope = [:scope] self.reserved_words = [:reserve] || Set.new(["new", "edit"]) self.slugged_attributes = fields.map &:to_s self.history = [:history] self.by_model_type = [:by_model_type] field :_slugs, type: Array, default: [], localize: [:localize] alias_attribute :slugs, :_slugs # Set index unless 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 [: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 |
#slug_scope_key ⇒ Array<Document>, Document
Returns the scope key for indexing, considering associations
111 112 113 114 |
# 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 |