Permalink
Browse files

Added support for aliased associations

  • Loading branch information...
1 parent 1417b58 commit ec93f2ad273574425df16b06ca10ea7864bf30bd @ErwinM committed Oct 9, 2011
Showing with 28 additions and 4 deletions.
  1. +8 −0 CHANGELOG.md
  2. +4 −1 lib/acts_as_tenant/model_extensions.rb
  3. +1 −1 lib/acts_as_tenant/version.rb
  4. +14 −1 spec/model_extensions_spec.rb
  5. +1 −1 spec/spec_helper.rb
View
@@ -0,0 +1,8 @@
+0.2.2
+-----
+* Enhancements
+ * Added support for aliased associations ( belongs_to :something, :class_name => 'SomethingElse'). In previous version these would raise an 'uninitialized constant' error.
+
+0.2.1
+-----
+* Initial release
@@ -70,8 +70,11 @@ def self.is_scoped_by_tenant?
# exempt them
reflect_on_all_associations.each do |a|
unless a == reflection || a.macro == :has_many || a.options[:polymorphic]
+ # check if the association is aliasing another class, if so
+ # find the unaliased class name
+ association_class = a.options[:class_name].nil? ? a.name.to_s.classify.constantize : a.options[:class_name].constantize
validates_each a.foreign_key.to_sym do |record, attr, value|
- record.errors.add attr, "is invalid" unless a.name.to_s.classify.constantize.where(:id => value).present?
+ record.errors.add attr, "is invalid" unless association_class.where(:id => value).present?
end
end
end
@@ -1,3 +1,3 @@
module ActsAsTenant
- VERSION = "0.2.1"
+ VERSION = "0.2.2"
end
@@ -26,6 +26,11 @@
t.column :name, :string
end
+ create_table :sub_tasks, :force => true do |t|
+ t.column :name, :string
+ t.column :something_else, :integer
+ end
+
end
# Setup the models
@@ -48,11 +53,13 @@ class Task < ActiveRecord::Base
validates_uniqueness_of :name
end
-
class City < ActiveRecord::Base
validates_uniqueness_of :name
end
+class SubTask < ActiveRecord::Base
+ belongs_to :something_else, :class_name => "Project"
+end
# Start testing!
describe ActsAsTenant do
@@ -190,4 +197,10 @@ class City < ActiveRecord::Base
@city2 = City.create(:name => 'foo').valid?.should == false
end
end
+
+ describe "It should be possible to use aliased associations" do
+ it { @sub_task = SubTask.create(:name => 'foo').valid?.should == true }
+ end
+
+
end
View
@@ -23,7 +23,7 @@
#RSpec.configure do |config|
#end
-Spec::Runner.configure do |config|
+RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.strategy = :transaction

0 comments on commit ec93f2a

Please sign in to comment.