Module: Mongoid::Slug
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/mongoid/slug.rb,
lib/mongoid/slug/index.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, UniqueSlug
Constant Summary collapse
- VERSION =
'4.0.0'
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 Method Details
.mongoid3? ⇒ Boolean
268 269 270 |
# File 'lib/mongoid/slug.rb', line 268 def self.mongoid3? ::Mongoid.const_defined? :Observer end |
Instance Method Details
#apply_slug ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/mongoid/slug.rb', line 173 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 == 0 # avoid duplicate slugs self._slugs.delete(_new_slug) if self._slugs if !!self.history && self._slugs.is_a?(Array) append_slug(_new_slug) else self._slugs = [_new_slug] end end |
#build_slug ⇒ true
Builds a new slug.
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/mongoid/slug.rb', line 156 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.
215 216 217 |
# File 'lib/mongoid/slug.rb', line 215 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.
225 226 227 |
# File 'lib/mongoid/slug.rb', line 225 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
238 239 240 |
# File 'lib/mongoid/slug.rb', line 238 def paranoid_deleted? !!(self.class.is_paranoid_doc? and self.deleted_at != nil) end |
#reset_slug! ⇒ Object
Rolls back the slug value from the Mongoid changeset.
210 211 212 |
# File 'lib/mongoid/slug.rb', line 210 def reset_slug! self.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)
195 196 197 198 |
# File 'lib/mongoid/slug.rb', line 195 def set_slug! build_slug self.method(:set).arity == 1 ? set({_slugs: self._slugs}) : set(:_slugs, self._slugs) end |
#slug ⇒ String
253 254 255 256 |
# File 'lib/mongoid/slug.rb', line 253 def slug return _slugs.last if _slugs return _id.to_s end |
#slug_builder ⇒ Object
258 259 260 261 262 263 264 265 266 |
# File 'lib/mongoid/slug.rb', line 258 def slug_builder _cur_slug = nil if new_with_slugs? or 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
230 231 232 |
# File 'lib/mongoid/slug.rb', line 230 def slug_should_be_rebuilt? (new_record? or _slugs_changed? or slugged_attributes_changed?) and !paranoid_deleted? end |
#slugged_attributes_changed? ⇒ Boolean
242 243 244 |
# File 'lib/mongoid/slug.rb', line 242 def slugged_attributes_changed? slugged_attributes.any? { |f| attribute_changed? f.to_s } end |
#to_param ⇒ String
to this record.
248 249 250 |
# File 'lib/mongoid/slug.rb', line 248 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)
204 205 206 207 |
# File 'lib/mongoid/slug.rb', line 204 def unset_slug! unset(:_slugs) clear_slug! end |