Module: Mongoid::Slug
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/mongoid/slug.rb,
lib/mongoid/slug/index.rb,
lib/mongoid/slug/railtie.rb,
lib/mongoid/slug/version.rb,
lib/mongoid/slug/criteria.rb,
lib/mongoid/slug/paranoia.rb,
lib/mongoid/slug/unique_slug.rb
Overview
Slugs your Mongoid model.
Defined Under Namespace
Modules: ClassMethods, Index, Paranoia Classes: Criteria, Railtie, UniqueSlug
Constant Summary collapse
- MONGO_INDEX_KEY_LIMIT_BYTES =
1024- VERSION =
'5.3.0'.freeze
Class Attribute Summary collapse
-
.default_slug ⇒ Object
Returns the value of attribute default_slug.
Class Method Summary collapse
Instance Method Summary collapse
- #apply_slug ⇒ Object
-
#build_slug ⇒ true
Builds a new slug.
-
#clear_slug! ⇒ Object
Sets the slug to its default value.
-
#find_unique_slug ⇒ String
Finds a unique slug, were specified string used to generate a slug.
-
#paranoid_deleted? ⇒ Boolean
Indicates whether or not the document has been deleted in paranoid fashion Always returns false if the document is not paranoid.
-
#reset_slug! ⇒ Object
Rolls back the slug value from the Mongoid changeset.
-
#set_slug! ⇒ Object
Builds slug then atomically sets it in the database.
-
#slug ⇒ String
The slug, or nil if the document does not have a slug.
- #slug_builder ⇒ Object
-
#slug_should_be_rebuilt? ⇒ Boolean
Whether the slug requires to be rebuilt.
- #slugged_attributes_changed? ⇒ Boolean
-
#to_param ⇒ String
to this record.
-
#unset_slug! ⇒ Object
Atomically unsets the slug field in the database.
Class Attribute Details
.default_slug ⇒ Object
Returns the value of attribute default_slug.
32 33 34 |
# File 'lib/mongoid/slug.rb', line 32 def default_slug @default_slug end |
Class Method Details
.configure(&block) ⇒ Object
33 34 35 |
# File 'lib/mongoid/slug.rb', line 33 def configure(&block) instance_eval(&block) end |
.slug(&block) ⇒ Object
37 38 39 |
# File 'lib/mongoid/slug.rb', line 37 def slug(&block) @default_slug = block if block_given? end |
Instance Method Details
#apply_slug ⇒ Object
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/mongoid/slug.rb', line 208 def apply_slug new_slug = find_unique_slug # skip slug generation and use Mongoid id # to find document instead return true if new_slug.size.zero? # avoid duplicate slugs _slugs.delete(new_slug) if _slugs if !!slug_history && _slugs.is_a?(Array) append_slug(new_slug) else self._slugs = [new_slug] end end |
#build_slug ⇒ true
Builds a new slug.
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/mongoid/slug.rb', line 191 def build_slug if localized? begin orig_locale = I18n.locale all_locales.each do |target_locale| I18n.locale = target_locale apply_slug end ensure I18n.locale = orig_locale end else apply_slug end true end |
#clear_slug! ⇒ Object
Sets the slug to its default value.
250 251 252 |
# File 'lib/mongoid/slug.rb', line 250 def clear_slug! self._slugs = [] end |
#find_unique_slug ⇒ String
Finds a unique slug, were specified string used to generate a slug.
Returned slug will the same as the specified string when there are no duplicates.
260 261 262 |
# File 'lib/mongoid/slug.rb', line 260 def find_unique_slug UniqueSlug.new(self).find_unique end |
#paranoid_deleted? ⇒ Boolean
Indicates whether or not the document has been deleted in paranoid fashion Always returns false if the document is not paranoid
273 274 275 |
# File 'lib/mongoid/slug.rb', line 273 def paranoid_deleted? !!(self.class.is_paranoid_doc? && !deleted_at.nil?) end |
#reset_slug! ⇒ Object
Rolls back the slug value from the Mongoid changeset.
245 246 247 |
# File 'lib/mongoid/slug.rb', line 245 def reset_slug! reset__slugs! end |
#set_slug! ⇒ Object
Builds slug then atomically sets it in the database. This is used when working with the Mongoid::Paranoia restore callback.
This method is adapted to use the :set method variants from both Mongoid 3 (two args) and Mongoid 4 (hash arg)
230 231 232 233 |
# File 'lib/mongoid/slug.rb', line 230 def set_slug! build_slug method(:set).arity == 1 ? set(_slugs: _slugs) : set(:_slugs, _slugs) end |
#slug ⇒ String
Returns the slug, or nil if the document does not have a slug.
288 289 290 291 |
# File 'lib/mongoid/slug.rb', line 288 def slug return _slugs.last if _slugs _id.to_s end |
#slug_builder ⇒ Object
293 294 295 296 297 298 299 300 301 |
# File 'lib/mongoid/slug.rb', line 293 def slug_builder cur_slug = nil if new_with_slugs? || persisted_with_slug_changes? # user defined slug cur_slug = _slugs.last end # generate slug if the slug is not user defined or does not exist cur_slug || pre_slug_string end |
#slug_should_be_rebuilt? ⇒ Boolean
Returns Whether the slug requires to be rebuilt.
265 266 267 |
# File 'lib/mongoid/slug.rb', line 265 def slug_should_be_rebuilt? (new_record? || _slugs_changed? || slugged_attributes_changed?) && !paranoid_deleted? end |
#slugged_attributes_changed? ⇒ Boolean
277 278 279 |
# File 'lib/mongoid/slug.rb', line 277 def slugged_attributes_changed? slugged_attributes.any? { |f| attribute_changed? f.to_s } end |
#to_param ⇒ String
to this record.
283 284 285 |
# File 'lib/mongoid/slug.rb', line 283 def to_param slug || super end |
#unset_slug! ⇒ Object
Atomically unsets the slug field in the database. It is important to unset the field for the sparse index on slugs.
This also resets the in-memory value of the slug field to its default (empty array)
239 240 241 242 |
# File 'lib/mongoid/slug.rb', line 239 def unset_slug! unset(:_slugs) clear_slug! end |