Sha256: 6210ce71be965aab864e91aad44017b8dae375924688363e0e43275bce90f6e2
Contents?: true
Size: 1.84 KB
Versions: 1
Compression:
Stored size: 1.84 KB
Contents
require 'mongoid' require 'babosa' module Mongoid # Creates friendly urls for mongoid models! module Urls extend ActiveSupport::Concern included do cattr_accessor :reserved_words, :url_simple, :url_scope, :url_key end # Methods avaiable at the model module ClassMethods # # The #url # # url :title # # :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_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 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 url end def new_url self[url_key].to_slug.normalize.to_s end protected def validate_urls(u) if self.class.find_by_url(u) errors.add(:title, :uniqueness) elsif reserved_words.include?(u) errors.add(:title, :reserved) else true end end def create_urls return unless changes.include?(url_key) validate_urls(new_url) self.url = new_url return if url_simple urls << new_url urls.uniq! end end # Urls end # Mongoid
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
mongoid-urls-0.0.7 | lib/mongoid/urls.rb |