Module: Sluggable
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/sluggable_rickpeyton.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
- #add_digit_to_end_of_slug ⇒ Object
- #check_for_slug_uniqueness ⇒ Object
- #create_slug ⇒ Object
- #to_param ⇒ Object
- #trim_leading_and_trailing_hyphen ⇒ Object
- #trim_non_url_characters ⇒ Object
- #trim_repeat_hyphens ⇒ Object
Instance Method Details
#add_digit_to_end_of_slug ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/sluggable_rickpeyton.rb', line 41 def add_digit_to_end_of_slug counter = 2 while self.class.exists?(slug: "#{self.slug}-#{counter}") counter += 1 end self.slug = self.slug + "-#{counter}" end |
#check_for_slug_uniqueness ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/sluggable_rickpeyton.rb', line 33 def check_for_slug_uniqueness if self.class.exists?(slug: self.slug) self.add_digit_to_end_of_slug else return self end end |
#create_slug ⇒ Object
13 14 15 16 17 18 19 |
# File 'lib/sluggable_rickpeyton.rb', line 13 def create_slug self.slug = self.send(self.class.slug_column.to_sym).downcase trim_non_url_characters trim_repeat_hyphens trim_leading_and_trailing_hyphen check_for_slug_uniqueness end |
#to_param ⇒ Object
9 10 11 |
# File 'lib/sluggable_rickpeyton.rb', line 9 def to_param "#{self.slug}" end |
#trim_leading_and_trailing_hyphen ⇒ Object
29 30 31 |
# File 'lib/sluggable_rickpeyton.rb', line 29 def trim_leading_and_trailing_hyphen self.slug.gsub!(/(^-)|(-$)/, '') end |
#trim_non_url_characters ⇒ Object
21 22 23 |
# File 'lib/sluggable_rickpeyton.rb', line 21 def trim_non_url_characters self.slug.gsub!(/[^a-z0-9\-]/, '-') end |
#trim_repeat_hyphens ⇒ Object
25 26 27 |
# File 'lib/sluggable_rickpeyton.rb', line 25 def trim_repeat_hyphens self.slug.gsub!(/-{2,}/, '-') end |