Module: Slugalicious::ClassMethods

Defined in:
lib/slugalicious.rb

Overview

Methods added to the class when this module is included.

Instance Method Summary collapse

Instance Method Details

#find_from_slug(slug, scope = nil) ⇒ ActiveRecord::Base?

Locates a record matching a given slug.

Parameters:

  • slug (String)

    The slug to locate.

  • scope (String) (defaults to: nil)

    The scope to search in (for use with scoped-unique slugs). This should be a string equal to the portion of the URL path preceding the slug.

Returns:

  • (ActiveRecord::Base, nil)

    The object with that slug, or ‘nil` if not found.



49
50
51
# File 'lib/slugalicious.rb', line 49

def find_from_slug(slug, scope=nil)
  Slug.from_slug(self, scope, slug).first.try!(:sluggable)
end

#find_from_slug!(*args) ⇒ Object

Identical to #find_from_slug, but raises an exception if the object is not found.

Raises:

  • (ActiveRecord::RecordNotFound)

    If no object with that slug is found.

See Also:



36
37
38
# File 'lib/slugalicious.rb', line 36

def find_from_slug!(*args)
  find_from_slug(*args) || raise(ActiveRecord::RecordNotFound)
end

#find_from_slug_path(path) ⇒ ActiveRecord::Base

Locates a record from a given path, that consists of a slug and its scope, as would appear in a URL path component.

Parameters:

  • path (String)

    The scope and slug concatenated together.

Returns:

  • (ActiveRecord::Base)

    The object with that slug.

Raises:

  • (ActiveRecord::RecordNotFound)

    If no object with that slug is found.



61
62
63
64
65
# File 'lib/slugalicious.rb', line 61

def find_from_slug_path(path)
  slug = path.split('/').last
  scope = path[0..(-(slug.size + 1))]
  find_from_slug slug, scope
end