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.

Examples:

Basic example of a slugged model

class Widget < ActiveRecord::Base
  include Slugalicious
  slugged :title
end

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

MAX_SLUG_LENGTH =

The maximum length of a slug.

126

Instance Method Summary collapse

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).

Parameters:

  • slug (String)

    A slug for this object.

Returns:

  • (true, false, nil)

    ‘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

#slugString?

Returns The slug for this object, or ‘nil` if none has been assigned.

Returns:

  • (String, nil)

    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_pathString?

Returns The full slug and path for this object, with scope included, or ‘nil` if none has been assigned.

Returns:

  • (String, nil)

    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