lib/superslug/has_superslug.rb in superslug-1.2.0 vs lib/superslug/has_superslug.rb in superslug-1.3.0

- old
+ new

@@ -37,22 +37,28 @@ superslug = self.send(source) else superslug = send(dest).blank? ? self.send(source) : self.send(dest) end # make lower case - superslug = superslug.downcase + superslug = superslug.downcase.strip # replace ampersands with 'and' superslug.gsub!(/\&/, ' and ') # remove all bad characters # (we allow hyphens, underscores, and plus marks) - superslug.gsub!(/[^a-zA-Z0-9 \-\_\+]/, "") + superslug.gsub!(/[^a-zA-Z0-9 \-\_]/, "") # replace spaces with underscores superslug.gsub!(/\ /, separator) # replace repeating underscores superslug.gsub!(/#{separator}+/, separator) # Find all records with the same slug value for the # slug column - duplicates = self.class.name.constantize.where(dest.to_sym => superslug) + if options[:context] + duplicates = self.send(options[:context].to_s) + .send(self.class.table_name.to_s) + .where(dest.to_sym => superslug) + else + duplicates = self.class.name.constantize.where(dest.to_sym => superslug) + end # Append the ID to the end of the slug if the slug # column is already taken if (duplicates - [self]).size > 0 superslug = "#{superslug}#{separator}#{self.id}" end