lib/mongoid/urls.rb in mongoid-urls-0.0.9 vs lib/mongoid/urls.rb in mongoid-urls-0.3.0
- old
+ new
@@ -1,7 +1,6 @@
require 'mongoid'
-require 'babosa'
module Mongoid
# Creates friendly urls for mongoid models!
module Urls
extend ActiveSupport::Concern
@@ -22,11 +21,11 @@
# :simple -> Only one url per instance
# :reserve -> Defaults to %w( new edit ) + I18n.locales
#
def url(*args)
options = args.extract_options!
- fail 'One #url per model!' if url_keys
+ raise 'Only one #url per model!' if url_keys
self.url_keys = args # .first.to_s
self.url_simple = options[:simple]
create_url_fields
create_url_validations(options)
end
@@ -43,19 +42,20 @@
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
+ alias find_by_url find_url
+
private
def create_url_fields
field :url, type: String
index({ url: 1 }, unique: true)
define_method('url=') do |val|
- self[:url] = val.to_slug.normalize.to_s
+ self[:url] = val.parameterize
end
return if url_simple
field :urls, type: Array, default: []
index(urls: 1)
end
@@ -67,10 +67,10 @@
def new_url
url_keys.each do |key|
val = send(key)
next if val.blank?
- url = val.to_slug.normalize.to_s
+ url = val.parameterize
next if self.class.find_url(url)
return url
end
nil
end