Permalink
Browse files

allow enforcing uniqueness at the sti base class level

  • Loading branch information...
1 parent b242aa1 commit c946ccc5eef71663e6867fec78c49d149345c466 @rsl committed Jan 3, 2013
@@ -2,14 +2,10 @@ module Stringex
module ActsAsUrl
module Adapter
class ActiveRecord < Base
- def self.loadable?
- defined?(::ActiveRecord) && defined?(::ActiveRecord::Base)
- end
-
def self.load
ensure_loadable
- ::ActiveRecord::Base.send :include, ActsAsUrlInstanceMethods
- ::ActiveRecord::Base.send :extend, ActsAsUrlClassMethods
+ orm_class.send :include, ActsAsUrlInstanceMethods
+ orm_class.send :extend, ActsAsUrlClassMethods
end
private
@@ -26,6 +22,10 @@ def add_scoped_url_owner_conditions
@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.before_validation :ensure_unique_url
@@ -58,7 +58,11 @@ def url_owner_conditions
end
def url_owners
- @url_owners ||= instance.class.unscoped.find(:all, :conditions => url_owner_conditions)
+ @url_owners ||= url_owners_class.unscoped.find(:all, :conditions => url_owner_conditions)
+ end
+
+ def self.orm_class
+ ::ActiveRecord::Base
end
end
end
@@ -46,6 +46,8 @@ def self.ensure_loadable
end
def self.loadable?
+ orm_class
+ rescue NameError
false
end
@@ -95,6 +97,16 @@ def url_attribute_for(object)
object.send settings.url_attribute
end
+ def url_owners_class
+ return instance.class unless settings.enforce_uniqueness_on_sti_base_class
+
+ klass = instance.class
+ while klass.superclass < orm_class
+ klass = klass.superclass
+ end
+ klass
+ end
+
def write_url_attribute(value)
instance.send :write_attribute, settings.url_attribute, value
end
@@ -2,14 +2,9 @@ module Stringex
module ActsAsUrl
module Adapter
class Mongoid < Base
-
- def self.loadable?
- defined?(::Mongoid) && defined?(::Mongoid::Document)
- end
-
def self.load
ensure_loadable
- ::Mongoid::Document.send :extend, Stringex::ActsAsUrl::ActsAsUrlClassMethods
+ orm_class.send :extend, Stringex::ActsAsUrl::ActsAsUrlClassMethods
end
private
@@ -24,6 +19,10 @@ def add_scoped_url_owner_conditions
@url_owner_conditions.merge! settings.scope_for_url => instance.send(settings.scope_for_url)
end
+ def orm_class
+ self.class.orm_class
+ end
+
def create_callback
if settings.sync_url
klass.before_validation :ensure_unique_url
@@ -56,7 +55,11 @@ def url_owner_conditions
end
def url_owners
- @url_owners ||= instance.class.unscoped.where(url_owner_conditions).to_a
+ @url_owners ||= url_owners_class.unscoped.where(url_owner_conditions).to_a
+ end
+
+ def self.orm_class
+ ::Mongoid::Document
end
end
end
@@ -38,6 +38,7 @@ def self.default_settings
@default_settings ||= {
:allow_duplicates => false,
:duplicate_count_separator => "-",
+ :enforce_uniqueness_on_sti_class => false,
:only_when_blank => false,
:scope_for_url => nil,
:sync_url => false,
@@ -17,60 +17,27 @@
t.string :title, :other, :url
end
- # create_table :permuments, :force => true do |t|
- # t.string :title, :permalink
- # end
+ create_table :sti_base_documents, :force => true do |t|
+ t.string :title, :other, :url, :type
+ end
end
ActiveRecord::Migration.verbose = true
class Document < ActiveRecord::Base
acts_as_url :title
end
-# class Updatument < ActiveRecord::Base
-# acts_as_url :title, :sync_url => true
-# end
-
-# class Mocument < ActiveRecord::Base
-# acts_as_url :title, :scope => :other, :sync_url => true
-# end
-
-# class Permument < ActiveRecord::Base
-# acts_as_url :title, :url_attribute => :permalink
-# end
-
-# class Procument < ActiveRecord::Base
-# acts_as_url :non_attribute_method
-
-# def non_attribute_method
-# "#{title} got massaged"
-# end
-# end
-
-# class Blankument < ActiveRecord::Base
-# acts_as_url :title, :only_when_blank => true
-# end
-
-# class Duplicatument < ActiveRecord::Base
-# acts_as_url :title, :duplicate_count_separator => "---"
-# end
-
-# class Validatument < ActiveRecord::Base
-# acts_as_url :title, :sync_url => true
-# validates_presence_of :title
-# end
-
-# class Ununiqument < ActiveRecord::Base
-# acts_as_url :title, :allow_duplicates => true
-# end
+class STIBaseDocument < ActiveRecord::Base
+ # 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 Limitument < ActiveRecord::Base
-# acts_as_url :title, :limit => 13
-# end
+class STIChildDocument < STIBaseDocument
+end
-# class Skipument < ActiveRecord::Base
-# acts_as_url :title, :exclude => ["_So_Fucking_Special"]
-# end
+class AnotherSTIChildDocument < STIBaseDocument
+end
module AdapterSpecificTestBehaviors
def setup
@@ -22,6 +22,24 @@ class Document
acts_as_url :title
end
+class STIBaseDocument
+ include Mongoid::Document
+ field :title, :type => String
+ field :other, :type => String
+ field :url, :type => String
+ field :type, :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
+
module AdapterSpecificTestBehaviors
def setup
# No setup tasks at present
@@ -255,4 +255,15 @@ def test_should_allow_alternate_whitespace_replacements
@doc = Document.create!(:title => "now with tildes")
assert_equal "now~with~tildes", @doc.url
end
+
+ def test_should_allow_enforcing_uniqueness_on_sti_base_class
+ STIBaseDocument.class_eval do
+ acts_as_url :title, :enforce_uniqueness_on_sti_base_class => true
+ end
+
+ @doc = STIChildDocument.create!(:title => "Unique")
+ assert_equal "unique", @doc.url
+ @doc_2 = AnotherSTIChildDocument.create!(:title => "Unique")
+ assert_equal "unique-1", @doc_2.url
+ end
end

0 comments on commit c946ccc

Please sign in to comment.