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)

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

- (Object) sluggable



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

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

Instance Method Details

- (Boolean) current?

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

Returns:

  • (Boolean)


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

def current?
  sluggable.slug == self
end

- (Boolean) outdated?

Returns:

  • (Boolean)


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

def outdated?
  !current?
end

- (Object) save(*args)



47
48
49
# File 'lib/friendly_id/active_record_adapter/slug.rb', line 47

def save(*args)
  persisted? && !scope_changed? ? true : super
end

- (Object) save!(*args)



51
52
53
# File 'lib/friendly_id/active_record_adapter/slug.rb', line 51

def save!(*args)
  persisted? && !scope_changed? ? true : super
end

- (Object) to_friendly_id



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

def to_friendly_id
  sequence > 1 ? friendly_id_with_sequence : name
end

- (Object) validate_name

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



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

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