lib/mongoid/urls.rb in mongoid-urls-0.0.3 vs lib/mongoid/urls.rb in mongoid-urls-0.0.7

- old
+ new

@@ -22,41 +22,41 @@ # :simple -> Only one url per instance # :reserve -> Defaults to %w( new edit ) # def url(*args) options = args.extract_options! + fail 'One #url per model!' if url_key self.url_key = args.first.to_s self.url_simple = options[:simple] self.reserved_words = options[:reserve] || Set.new(%w(new edit)) create_url_fields before_validation :create_urls end - def find_by_url(u) - url_simple ? find_by(url: u) : find_by(urls: u) + def find_url(u) + find_by(url: u) || (!url_simple && find_by(urls: u)) rescue Mongoid::Errors::DocumentNotFound nil end + alias_method :find_by_url, :find_url private def create_url_fields - if url_simple - field :url, type: String - index({ url: 1 }, unique: true) - else - field :urls, type: Array, default: [] - index(urls: 1) - end + field :url, type: String + index({ url: 1 }, unique: true) + return if url_simple + field :urls, type: Array, default: [] + index(urls: 1) end end # ClassMethods def to_param - urls.last + url end - def slug + def new_url self[url_key].to_slug.normalize.to_s end protected @@ -70,12 +70,13 @@ end end def create_urls return unless changes.include?(url_key) - validate_urls(new_url = slug) + validate_urls(new_url) - return self.url = new_url if url_simple + self.url = new_url + return if url_simple urls << new_url urls.uniq! end end # Urls end # Mongoid