Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added support for aliased associations

  • Loading branch information...
commit ec93f2ad273574425df16b06ca10ea7864bf30bd 1 parent 1417b58
@ErwinM authored
View
8 CHANGELOG.md
@@ -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
View
5 lib/acts_as_tenant/model_extensions.rb
@@ -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
View
2  lib/acts_as_tenant/version.rb
@@ -1,3 +1,3 @@
module ActsAsTenant
- VERSION = "0.2.1"
+ VERSION = "0.2.2"
end
View
15 spec/model_extensions_spec.rb
@@ -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
2  spec/spec_helper.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.