Permalink
Browse files

finished data mapper adapter

  • Loading branch information...
1 parent 6e53ebb commit 018d68d939de41e5782ca6bcfcb27b42f7d980d4 @rsl committed Jan 23, 2013
View
@@ -34,13 +34,12 @@ desc 'Default: Run Stringex test suite using ActiveRecord as the ORM'
task :default => [:setup_active_record_suite, :test]
task :setup_active_record_suite do
- `rm -f #{File.dirname(__FILE__)}/test/acts_as_url.sqlite3`
ENV['ADAPTER'] = 'active_record'
end
desc 'Run Stringex test suite using ActiveRecord as the ORM'
namespace :test do
- task :active_record => [:setup_active_record_suite, :test]
+ task :active_record => [:setup_active_record_suite]
end
task :setup_mongoid_suite do
@@ -52,6 +51,15 @@ namespace :test do
task :mongoid => [:setup_mongoid_suite, :test]
end
+task :setup_data_mapper_suite do
+ ENV['ADAPTER'] = 'data_mapper'
+end
+
+desc 'Run Stringex test suite using DataMapper as the ORM'
+namespace :test do
+ task :data_mapper => [:setup_data_mapper_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/data_mapper"
require "stringex/acts_as_url/adapter/mongoid"
module Stringex
@@ -0,0 +1,104 @@
+module Stringex
+ module ActsAsUrl
+ module Adapter
+ class DataMapper < Base
+ def self.load
+ ensure_loadable
+ orm_class.send :include, Stringex::ActsAsUrl::ActsAsUrlInstanceMethods
+ ::DataMapper::Model.send :include, Stringex::ActsAsUrl::ActsAsUrlClassMethods
+ end
+
+ private
+
+ def add_new_record_url_owner_conditions
+ return if instance.new?
+ @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
+
+ def orm_class
+ self.class.orm_class
+ end
+
+ def create_callback
+ if settings.sync_url
+ klass.class_eval do
+ before :save, :ensure_unique_url
+ end
+ else
+ klass.class_eval do
+ before :create, :ensure_unique_url
+ end
+ end
+ 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 instance_from_db
+ instance.class.get(instance.id)
+ end
+
+ def is_blank?(object)
+ object.nil? || object == '' || object == []
+ end
+
+ def is_new?(object)
+ object.new?
+ end
+
+ def is_present?(object)
+ !object.nil? && object != '' && object != []
+ end
+
+ def klass_previous_instances(&block)
+ klass.all(:conditions => {settings.url_attribute => nil}).each do |instance|
+ yield instance
+ end
+ end
+
+ def read_attribute(instance, attribute)
+ instance.attributes[attribute]
+ 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 ||= url_owners_class.all(:conditions => url_owner_conditions)
+ end
+
+ def read_attribute(instance, name)
+ instance.attribute_get name
+ end
+
+ def write_attribute(instance, name, value)
+ instance.attribute_set name, value
+ end
+
+ def self.orm_class
+ ::DataMapper::Resource
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,82 @@
+require 'rubygems'
+gem 'dm-core'
+gem 'dm-migrations'
+gem 'dm-validations'
+require 'dm-core'
+require 'dm-migrations'
+require 'dm-validations'
+require 'stringex'
+# Reload adapters to make sure ActsAsUrl sees the ORM
+Stringex::ActsAsUrl::Adapter.load_available
+
+puts "-------------------------------------------------"
+puts "Running ActsAsUrl tests with DataMapper adapter"
+puts "-------------------------------------------------"
+
+DataMapper.setup :default, 'sqlite::memory:'
+
+# What the tests do in constant redefining the same classes doesn't quite work with DataMapper.
+# This proc allows us to reset the class definitions on each test. This might be more expensive
+# but it definitely allows the class definitions to be correct. If someone more familiar with
+# DataMapper than I am wants to refactor this, I'd be more than happy to take a look.
+DefineTestClasses = proc do
+ class Document
+ include DataMapper::Resource
+ property :id, Serial
+ property :title, String
+ property :other, String
+ property :url, String, :lazy => false
+
+ acts_as_url :title
+ end
+
+ class STIBaseDocument
+ include DataMapper::Resource
+ property :id, Serial
+ property :title, String
+ property :other, String
+ property :url, String, :lazy => false
+ property :type, String
+
+ # This gets redefined in the only test that uses it but I want to be uniform
+ # in setting configuration details in the tests themselves
+ acts_as_url :title
+ end
+
+ class STIChildDocument < STIBaseDocument
+ end
+
+ class AnotherSTIChildDocument < STIBaseDocument
+ end
+
+ DataMapper.finalize
+ Document.auto_migrate!
+ STIBaseDocument.auto_migrate!
+end
+
+module AdapterSpecificTestBehaviors
+ def setup
+ DefineTestClasses.call
+ end
+
+ def teardown
+ [Document, STIBaseDocument, STIChildDocument, AnotherSTIChildDocument].each do |klass|
+ klass.destroy
+ Object.send :remove_const, klass.name.intern
+ end
+ end
+
+ def add_validation_on_document_title
+ Document.class_eval do
+ validates_presence_of :title
+ end
+ end
+
+ def remove_validation_on_document_title
+ # Do nothing. The class is going to be reloaded on the next test.
+ end
+
+ def adapter_specific_update(instance, hash)
+ response = instance.send :update, hash
+ end
+end
@@ -50,8 +50,9 @@ def test_should_not_increment_count_on_repeated_saves
acts_as_url :title, :sync_url => true
end
- @doc = Document.new(:title => "Continuous or Constant")
- 5.times do
+ @doc = Document.create(:title => "Continuous or Constant")
+ assert_equal "continuous-or-constant", @doc.url
+ 5.times do |n|
@doc.save!
assert_equal "continuous-or-constant", @doc.url
end

0 comments on commit 018d68d

Please sign in to comment.