Permalink
Browse files

begin mongoid branch

  • Loading branch information...
1 parent 6944f6e commit dd3bf0ad8456ace29c6308f5ecdacde3805c8909 @rsl committed Dec 6, 2012
View
@@ -4,6 +4,7 @@ source "https://rubygems.org"
group :development do
gem "activerecord" # Not tying to a specific version of AR just yet
gem "jeweler", "1.8.4"
+ gem "mongoid"
gem "RedCloth" # Can I restate that I really dislike camelcased gem names?
gem "sqlite3" # Not tying to a specific version just yet
gem "travis-lint", "1.4.0"
View
@@ -43,6 +43,15 @@ namespace :test do
task :active_record => [:setup_active_record_suite, :test]
end
+task :setup_mongoid_suite do
+ ENV['ADAPTER'] = 'mongoid'
+end
+
+desc 'Run Stringex test suite using Mongoid as the ORM'
+namespace :test do
+ task :mongoid => [:setup_mongoid_suite, :test]
+end
+
desc 'Generate RDoc for Stringex'
Rake::RDocTask.new(:rdoc) do |rdoc|
version = File.read('VERSION')
@@ -1,5 +1,6 @@
require "stringex/acts_as_url/adapter/base"
require "stringex/acts_as_url/adapter/active_record"
+require "stringex/acts_as_url/adapter/mongoid"
module Stringex
module ActsAsUrl
@@ -0,0 +1,83 @@
+module Stringex
+ module ActsAsUrl
+ module Adapter
+ class Mongoid < Base
+ def create_callbacks!(klass)
+ if settings.sync_url
+ klass.before_validation :ensure_unique_url
+ else
+ if defined?(ActiveModel::Callbacks)
+ klass.before_validation :ensure_unique_url, :on => :create
+ else
+ klass.before_validation_on_create :ensure_unique_url
+ end
+ end
+
+ klass.class_eval <<-"END"
+ def #{settings.url_attribute}
+ acts_as_url_configuration.adapter.url_attribute self
+ end
+ END
+ end
+
+ def url_attribute(instance)
+ if !instance.new_record? && instance.errors[settings.attribute_to_urlify].present?
+ instance.class.find(instance.id).send settings.url_attribute
+ else
+ instance.read_attribute settings.url_attribute
+ end
+ end
+
+ def self.loadable?
+ defined?(::Mongoid) && defined?(::Mongoid::Document)
+ end
+
+ def self.load
+ ensure_loadable
+ ::Mongoid::Document.send :include, Stringex::ActsAsUrl
+ end
+
+ private
+
+ def add_new_record_url_owner_conditions
+ return if instance.new_record?
+ @url_owner_conditions.first << " and id != ?"
+ @url_owner_conditions << instance.id
+ end
+
+ def add_scoped_url_owner_conditions
+ return unless settings.scope_for_url
+ @url_owner_conditions.first << " and #{settings.scope_for_url} = ?"
+ @url_owner_conditions << instance.send(settings.scope_for_url)
+ end
+
+ # NOTE: The <tt>instance</tt> here is not the cached instance but a block variable
+ # passed from <tt>klass_previous_instances</tt>, just to be clear
+ def ensure_unique_url_for!(instance)
+ instance.send :ensure_unique_url
+ instance.save
+ end
+
+ def get_base_url_owner_conditions
+ @url_owner_conditions = ["#{settings.url_attribute} LIKE ?", base_url + '%']
+ end
+
+ def klass_previous_instances(&block)
+ klass.find_each(:conditions => {settings.url_attribute => nil}, &block)
+ end
+
+ def url_owner_conditions
+ get_base_url_owner_conditions
+ add_new_record_url_owner_conditions
+ add_scoped_url_owner_conditions
+
+ @url_owner_conditions
+ end
+
+ def url_owners
+ @url_owners ||= instance.class.unscoped.find(:all, :conditions => url_owner_conditions)
+ end
+ end
+ end
+ end
+end
@@ -16,6 +16,8 @@ def adapter
adapter_name.send :new, self
when :active_record
Stringex::ActsAsUrl::Adapter::ActiveRecord.new self
+ when :mongoid
+ Stringex::ActsAsUrl::Adapter::Mongoid.new self
else
raise ArgumentError, "#{adapter_name} is not a defined ActsAsUrl adapter. Please feel free to implement your own and submit it back upstream."
end
@@ -0,0 +1,19 @@
+require 'rubygems'
+gem 'mongoid'
+require 'mongoid'
+require 'stringex'
+
+puts "-------------------------------------------------"
+puts "Running ActsAsUrl tests with Mongoid adapter"
+puts "-------------------------------------------------"
+
+Mongoid.configure do |config|
+ config.connect_to('acts_as_url')
+end
+
+class Document
+ include Mongoid::Document
+ Stringex::ActsAsUrl.mix_into self
+
+ acts_as_url :title
+end

0 comments on commit dd3bf0a

Please sign in to comment.