lib/stringex/acts_as_url.rb in stringex-1.3.2 vs lib/stringex/acts_as_url.rb in stringex-1.3.3
- old
+ new
@@ -31,10 +31,11 @@
cattr_accessor :scope_for_url
cattr_accessor :url_attribute # The attribute on the DB
cattr_accessor :only_when_blank
cattr_accessor :duplicate_count_separator
cattr_accessor :allow_slash
+ cattr_accessor :allow_duplicates
if options[:sync_url]
before_validation(:ensure_unique_url)
else
if defined?(ActiveModel::Callbacks)
@@ -48,10 +49,11 @@
self.scope_for_url = options[:scope]
self.url_attribute = options[:url_attribute] || "url"
self.only_when_blank = options[:only_when_blank] || false
self.duplicate_count_separator = options[:duplicate_count_separator] || "-"
self.allow_slash = options[:allow_slash] || false
+ self.allow_duplicates = options[:allow_duplicates] || false
class_eval <<-"END"
def #{url_attribute}
if !new_record? && errors[attribute_to_urlify].present?
self.class.find(id).send(url_attribute)
@@ -93,15 +95,17 @@
conditions.first << " and #{self.class.scope_for_url} = ?"
conditions << send(self.class.scope_for_url)
end
url_owners = self.class.find(:all, :conditions => conditions)
write_attribute url_attribute, base_url
- if url_owners.any?{|owner| owner.send(url_attribute) == base_url}
- n = 1
- while url_owners.any?{|owner| owner.send(url_attribute) == "#{base_url}#{separator}#{n}"}
- n = n.succ
+ unless self.class.allow_duplicates
+ if url_owners.any?{|owner| owner.send(url_attribute) == base_url}
+ n = 1
+ while url_owners.any?{|owner| owner.send(url_attribute) == "#{base_url}#{separator}#{n}"}
+ n = n.succ
+ end
+ write_attribute url_attribute, "#{base_url}#{separator}#{n}"
end
- write_attribute url_attribute, "#{base_url}#{separator}#{n}"
end
end
end
end