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.
Class Method Summary collapse
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_a_slug? ⇒ Boolean
Does the record have (at least) one slug?.
-
#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.
33 34 35 |
# File 'lib/friendly_id/sluggable_instance_methods.rb', line 33 def finder_slug @finder_slug end |
#finder_slug_name ⇒ Object
Returns the value of attribute finder_slug_name.
34 35 36 |
# File 'lib/friendly_id/sluggable_instance_methods.rb', line 34 def finder_slug_name @finder_slug_name end |
Class Method Details
.included(base) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/friendly_id/sluggable_instance_methods.rb', line 3 def self.included(base) base.class_eval do has_many :slugs, :order => 'id DESC', :as => :sluggable, :dependent => :destroy before_save :set_slug after_save :set_slug_cache # only protect the column if the class is not already using attributes_accessible if !accessible_attributes if [:cache_column] attr_protected [:cache_column].to_sym end attr_protected :cached_slug end end def base.cache_column if defined?(@cache_column) return @cache_column elsif [:cache_column] @cache_column = [:cache_column].to_sym elsif columns.any? { |c| c.name == 'cached_slug' } @cache_column = :cached_slug else @cache_column = nil end end end |
Instance Method Details
#found_using_friendly_id? ⇒ Boolean
Was the record found using one of its friendly ids?
41 42 43 |
# File 'lib/friendly_id/sluggable_instance_methods.rb', line 41 def found_using_friendly_id? !!@finder_slug_name end |
#found_using_numeric_id? ⇒ Boolean
Was the record found using its numeric id?
46 47 48 |
# File 'lib/friendly_id/sluggable_instance_methods.rb', line 46 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?
51 52 53 54 |
# File 'lib/friendly_id/sluggable_instance_methods.rb', line 51 def found_using_outdated_friendly_id? return false if cache_column && send(cache_column) == @finder_slug_name finder_slug.id != slug.id end |
#friendly_id ⇒ Object Also known as: best_id
Returns the friendly id.
67 68 69 |
# File 'lib/friendly_id/sluggable_instance_methods.rb', line 67 def friendly_id slug(true).to_friendly_id end |
#has_a_slug? ⇒ Boolean
Does the record have (at least) one slug?
62 63 64 |
# File 'lib/friendly_id/sluggable_instance_methods.rb', line 62 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?
57 58 59 |
# File 'lib/friendly_id/sluggable_instance_methods.rb', line 57 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?
74 75 76 |
# File 'lib/friendly_id/sluggable_instance_methods.rb', line 74 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.
80 81 82 83 |
# File 'lib/friendly_id/sluggable_instance_methods.rb', line 80 def slug(reload = false) @most_recent_slug = nil if reload @most_recent_slug ||= slugs.first(:order => "id DESC") end |
#slug_text ⇒ Object
Get the processed string used as the basis of the friendly id.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/friendly_id/sluggable_instance_methods.rb', line 95 def slug_text base = send [:method] if self.slug_normalizer_block base = self.slug_normalizer_block.call(base) else if self.[:strip_diacritics] base = Slug::strip_diacritics(base) end if self.[:strip_non_ascii] base = Slug::strip_non_ascii(base) end base = Slug::normalize(base) end if base.mb_chars.length > [:max_length] base = base.mb_chars[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.
86 87 88 89 90 91 92 |
# File 'lib/friendly_id/sluggable_instance_methods.rb', line 86 def to_param if cache_column read_attribute(cache_column) || id.to_s else slug ? slug.to_friendly_id : id.to_s end end |