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
-
#finder_slug ⇒ Object
readonly
Returns the value of attribute finder_slug.
-
#finder_slug_name ⇒ Object
Returns the value of attribute finder_slug_name.
Instance Method Summary collapse
-
#found_using_friendly_id? ⇒ Boolean
Was the record found using one of its friendly ids?.
-
#found_using_numeric_id? ⇒ Boolean
Was the record found using its numeric id?.
-
#found_using_outdated_friendly_id? ⇒ Boolean
Was the record found using an old friendly id?.
-
#friendly_id ⇒ Object
(also: #best_id)
Returns the friendly id.
-
#has_better_id? ⇒ Boolean
Was the record found using an old friendly id, or its numeric id?.
-
#new_slug_needed? ⇒ Boolean
Has the basis of our friendly id changed, requiring the generation of a new slug?.
-
#slug(reload = false) ⇒ Object
Returns the most recent slug, which is used to determine the friendly id.
-
#slug_text ⇒ Object
Get the processed string used as the basis of the friendly id.
-
#to_param ⇒ Object
Returns the friendly id, or if none is available, the numeric id.
Instance Attribute Details
#finder_slug ⇒ Object
Returns the value of attribute finder_slug.
5 6 7 |
# File 'lib/friendly_id/sluggable_instance_methods.rb', line 5 def finder_slug @finder_slug end |
#finder_slug_name ⇒ Object
Returns the value of attribute finder_slug_name.
6 7 8 |
# File 'lib/friendly_id/sluggable_instance_methods.rb', line 6 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?
13 14 15 |
# File 'lib/friendly_id/sluggable_instance_methods.rb', line 13 def found_using_friendly_id? finder_slug end |
#found_using_numeric_id? ⇒ Boolean
Was the record found using its numeric id?
18 19 20 |
# File 'lib/friendly_id/sluggable_instance_methods.rb', line 18 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?
23 24 25 |
# File 'lib/friendly_id/sluggable_instance_methods.rb', line 23 def found_using_outdated_friendly_id? finder_slug.id != slug.id end |
#friendly_id ⇒ Object Also known as: best_id
Returns the friendly id.
33 34 35 |
# File 'lib/friendly_id/sluggable_instance_methods.rb', line 33 def friendly_id slug(true).to_friendly_id end |
#has_better_id? ⇒ Boolean
Was the record found using an old friendly id, or its numeric id?
28 29 30 |
# File 'lib/friendly_id/sluggable_instance_methods.rb', line 28 def has_better_id? 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?
40 41 42 |
# File 'lib/friendly_id/sluggable_instance_methods.rb', line 40 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.
46 47 48 49 |
# File 'lib/friendly_id/sluggable_instance_methods.rb', line 46 def slug(reload = false) @most_recent_slug = nil if reload @most_recent_slug ||= slugs.first end |
#slug_text ⇒ Object
Get the processed string used as the basis of the friendly id.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/friendly_id/sluggable_instance_methods.rb', line 57 def slug_text base = send [:column] if self.[:strip_diacritics] base = Slug::normalize(Slug::strip_diacritics(base)) else base = Slug::normalize(base) end if base.length > [:max_length] base = base[0...[:max_length]] end if [:reserved].include?(base) raise FriendlyId::SlugGenerationError.new("The slug text is a reserved value") end return base end |
#to_param ⇒ Object
Returns the friendly id, or if none is available, the numeric id.
52 53 54 |
# File 'lib/friendly_id/sluggable_instance_methods.rb', line 52 def to_param slug ? slug.to_friendly_id : id.to_s end |