Module: Slugalicious
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/slugalicious.rb
Overview
Adds the ‘slugged` method to an `ActiveRecord::Base` subclass. You can then call this method to add slugging support to your model. See the ClassMethods#slugged method for more details.
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- MAX_SLUG_LENGTH =
The maximum length of a slug.
126
Instance Method Summary collapse
-
#active_slug?(slug) ⇒ true, ...
‘true` if the slug is the currently active one (should not redirect), `false` if it’s inactive (should redirect), and ‘nil` if it’s not a known slug for the object (should 404).
-
#slug ⇒ String?
The slug for this object, or ‘nil` if none has been assigned.
-
#slug_with_path ⇒ String?
The full slug and path for this object, with scope included, or ‘nil` if none has been assigned.
Instance Method Details
#active_slug?(slug) ⇒ true, ...
Returns ‘true` if the slug is the currently active one (should not redirect), `false` if it’s inactive (should redirect), and ‘nil` if it’s not a known slug for the object (should 404).
182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/slugalicious.rb', line 182 def active_slug?(slug) @active_slug ||= begin slug = if slugs.loaded? then slugs.detect { |s| s.slug.downcase == slug.downcase } else slugs.where(slug: slug).first end if slug then slug.active? else nil end end end |
#slug ⇒ String?
Returns The slug for this object, or ‘nil` if none has been assigned.
162 163 164 165 166 |
# File 'lib/slugalicious.rb', line 162 def slug Rails.cache.fetch("Slug/#{self.class.to_s}/#{id}/slug") do slug_object.try!(:slug) end end |
#slug_with_path ⇒ String?
Returns The full slug and path for this object, with scope included, or ‘nil` if none has been assigned.
171 172 173 174 175 |
# File 'lib/slugalicious.rb', line 171 def slug_with_path Rails.cache.fetch("Slug/#{self.class.to_s}/#{id}/slug_with_path") do slug_object ? (slug_object.scope.to_s + slug_object.slug) : nil end end |