Class: Slug

Inherits:
ActiveRecord::Base show all
Defined in:
lib/friendly_id/active_record_adapter/slug.rb

Overview

A Slug is a unique, human-friendly identifier for an ActiveRecord.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from FriendlyId::ActiveRecordAdapter

#has_friendly_id

Methods included from FriendlyId::Base

#has_friendly_id, #uses_friendly_id?

Instance Attribute Details

#sluggableObject



18
19
20
21
22
23
24
25
26
# File 'lib/friendly_id/active_record_adapter/slug.rb', line 18

def sluggable
  sluggable_id && !@sluggable and begin
    klass = sluggable_type.constantize
    klass.send(:with_exclusive_scope) do
      @sluggable = klass.find(sluggable_id.to_i)
    end
  end
  @sluggable
end

Class Method Details

.named_scope(*args, &block) ⇒ Object



5
# File 'lib/friendly_id/active_record_adapter/slug.rb', line 5

def self.named_scope(*args, &block) scope(*args, &block) end

Instance Method Details

#current?Boolean

Whether this slug is the most recent of its owner’s slugs.

Returns:

  • (Boolean)


29
30
31
# File 'lib/friendly_id/active_record_adapter/slug.rb', line 29

def current?
  sluggable.slug == self
end

#outdated?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/friendly_id/active_record_adapter/slug.rb', line 33

def outdated?
  !current?
end

#to_friendly_idObject



37
38
39
# File 'lib/friendly_id/active_record_adapter/slug.rb', line 37

def to_friendly_id
  sequence > 1 ? friendly_id_with_sequence : name
end

#validate_nameObject

Raise a FriendlyId::SlugGenerationError if the slug name is blank.



42
43
44
45
46
# File 'lib/friendly_id/active_record_adapter/slug.rb', line 42

def validate_name
  if name.blank?
    raise FriendlyId::BlankError.new("slug.name can not be blank.")
  end
end