Module: Gluttonberg::Content::SlugManagement::InstanceMethods
- Defined in:
- lib/gluttonberg/content/slug_management.rb
Instance Method Summary collapse
- #find_potential_duplicates(slug) ⇒ Object
- #fix_duplicated_slug ⇒ Object
- #get_slug_source ⇒ Object
- #slug=(new_slug) ⇒ Object
-
#slug_management ⇒ Object
protected Checks If slug is blank then tries to set slug using following logic if slug_field_name is set then use its value and make it slug otherwise checks for name column otherwise checks for title column else get id as slug.
- #slug_without_postfix(slug) ⇒ Object
- #unique_slug(slug) ⇒ Object
Instance Method Details
#find_potential_duplicates(slug) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/gluttonberg/content/slug_management.rb', line 116 def find_potential_duplicates(slug) unless self.class.where(["slug = ? ", slug]).first.blank? temp_slug = slug_without_postfix(slug) potential_duplicates = self.class.where(["slug = ? OR slug = ? OR slug like ? ", slug, temp_slug, "#{temp_slug}-%"]) unless self.class.slug_scope.blank? potential_duplicates = potential_duplicates.where(self.class.slug_scope => self.send(self.class.slug_scope) ) end potential_duplicates = potential_duplicates.all potential_duplicates = potential_duplicates.find_all{|obj| obj.id != self.id} potential_duplicates else [] end end |
#fix_duplicated_slug ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/gluttonberg/content/slug_management.rb', line 91 def fix_duplicated_slug # check duplication: add id at the end if its duplicated potential_duplicates = find_potential_duplicates(self.slug) unless potential_duplicates.blank? if potential_duplicates.length > 1 || (potential_duplicates.length == 1 && potential_duplicates.first.id != self.id ) number = potential_duplicates.length+1 begin self.slug = "#{slug_without_postfix(self.slug)}-#{number}" number += 1 end while find_potential_duplicates(slug).map{|o| o.slug}.include?(slug) end end end |
#get_slug_source ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/gluttonberg/content/slug_management.rb', line 51 def get_slug_source if self.class.slug_source_field_name.blank? if self.respond_to?(:name) self.class.slug_source_field_name= :name elsif self.respond_to?(:title) self.class.slug_source_field_name= :title else self.class.slug_source_field_name= :id end end self.class.slug_source_field_name end |
#slug=(new_slug) ⇒ Object
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/gluttonberg/content/slug_management.rb', line 64 def slug=(new_slug) current_slug = self.slug new_slug = new_slug.to_s.sluglize unless new_slug.blank? new_slug = unique_slug(new_slug) write_attribute(:slug, new_slug) if self.respond_to?(:previous_slug) && self.slug_changed? && self.slug != current_slug write_attribute(:previous_slug, current_slug) end new_slug end |
#slug_management ⇒ Object
protected Checks If slug is blank then tries to set slug using following logic if slug_field_name is set then use its value and make it slug otherwise checks for name column otherwise checks for title column else get id as slug
81 82 83 84 85 86 87 88 89 |
# File 'lib/gluttonberg/content/slug_management.rb', line 81 def slug_management if self.slug.blank? if self.class.slug_source_field_name.blank? self.get_slug_source end self.slug= self.send(self.class.slug_source_field_name) end #slug.blank self.fix_duplicated_slug end |
#slug_without_postfix(slug) ⇒ Object
112 113 114 |
# File 'lib/gluttonberg/content/slug_management.rb', line 112 def slug_without_postfix(slug) Content::SlugManagement::ClassMethods.slug_without_postfix(slug) end |
#unique_slug(slug) ⇒ Object
106 107 108 109 110 |
# File 'lib/gluttonberg/content/slug_management.rb', line 106 def unique_slug(slug) # check duplication: add id at the end if its duplicated potential_duplicates = find_potential_duplicates(slug) Content::SlugManagement::ClassMethods.check_for_duplication(slug, self, potential_duplicates) end |