Module: FriendlyId::SluggableInstanceMethods

Defined in:
lib/friendly_id/sluggable_instance_methods.rb

Constant Summary collapse

NUM_CHARS_RESERVED_FOR_FRIENDLY_ID_EXTENSION =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#finder_slugObject

Returns the value of attribute finder_slug.



7
8
9
# File 'lib/friendly_id/sluggable_instance_methods.rb', line 7

def finder_slug
  @finder_slug
end

#finder_slug_nameObject

Returns the value of attribute finder_slug_name.



8
9
10
# File 'lib/friendly_id/sluggable_instance_methods.rb', line 8

def finder_slug_name
  @finder_slug_name
end

Instance Method Details

#found_using_friendly_id?Boolean

Was the record found using one of its friendly ids?

Returns:

  • (Boolean)


15
16
17
# File 'lib/friendly_id/sluggable_instance_methods.rb', line 15

def found_using_friendly_id?
  !!@finder_slug_name
end

#found_using_numeric_id?Boolean

Was the record found using its numeric id?

Returns:

  • (Boolean)


20
21
22
# File 'lib/friendly_id/sluggable_instance_methods.rb', line 20

def found_using_numeric_id?
  !found_using_friendly_id?
end

#found_using_outdated_friendly_id?Boolean

Was the record found using an old friendly id?

Returns:

  • (Boolean)


25
26
27
28
29
30
# File 'lib/friendly_id/sluggable_instance_methods.rb', line 25

def found_using_outdated_friendly_id?
  if cache = friendly_id_options[:cache_column]
    return false if send(cache) == @finder_slug_name
  end
  finder_slug.id != slug.id
end

#friendly_idObject Also known as: best_id

Returns the friendly id.



43
44
45
# File 'lib/friendly_id/sluggable_instance_methods.rb', line 43

def friendly_id
  slug(true).to_friendly_id
end

#has_a_slug?Boolean

Does the record have (at least) one slug?

Returns:

  • (Boolean)


38
39
40
# File 'lib/friendly_id/sluggable_instance_methods.rb', line 38

def has_a_slug?
  @finder_slug_name || slug
end

#has_better_id?Boolean

Was the record found using an old friendly id, or its numeric id?

Returns:

  • (Boolean)


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

def has_better_id?
  has_a_slug? and found_using_numeric_id? || found_using_outdated_friendly_id?
end

#new_slug_needed?Boolean

Has the basis of our friendly id changed, requiring the generation of a new slug?

Returns:

  • (Boolean)


50
51
52
# File 'lib/friendly_id/sluggable_instance_methods.rb', line 50

def new_slug_needed?
  !slug || slug_text != slug.name
end

#slug(reload = false) ⇒ Object

Returns the most recent slug, which is used to determine the friendly id.



56
57
58
59
# File 'lib/friendly_id/sluggable_instance_methods.rb', line 56

def slug(reload = false)
  @most_recent_slug = nil if reload
  @most_recent_slug ||= slugs.first(:order => "id DESC")
end

#slug_textObject

Get the processed string used as the basis of the friendly id.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/friendly_id/sluggable_instance_methods.rb', line 70

def slug_text
  base = send friendly_id_options[:column]
  if self.slug_normalizer_block
    base = self.slug_normalizer_block.call(base)
  else
    if self.friendly_id_options[:strip_diacritics]
      base = Slug::strip_diacritics(base)
    end
    if self.friendly_id_options[:strip_non_ascii]
      base = Slug::strip_non_ascii(base)
    end
    base = Slug::normalize(base)
  end

  if base.mb_chars.length > friendly_id_options[:max_length]
    base = base.mb_chars[0...friendly_id_options[:max_length]]
  end
  if friendly_id_options[:reserved].include?(base)
    raise FriendlyId::SlugGenerationError.new("The slug text is a reserved value")
  end
  return base
end

#to_paramObject

Returns the friendly id, or if none is available, the numeric id.



62
63
64
65
66
67
# File 'lib/friendly_id/sluggable_instance_methods.rb', line 62

def to_param
  if cache = friendly_id_options[:cache_column]
    return read_attribute(cache) || id.to_s
  end
  slug ? slug.to_friendly_id : id.to_s
end