Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added support for child models with no parent set

  • Loading branch information...
commit c1f3ea794918011d2c3ed5f5be2880d193d670ce 1 parent 4155a90
@ErwinM authored
View
5 CHANGELOG.md
@@ -1,3 +1,8 @@
+0.2.4
+-----
+* Added correct handling of child models that do not have their parent set (foreign key == nil)
+
+
0.2.3
-----
* Added support for models that declare a has_one relationships, these would error out in the previous versions.
View
4 lib/acts_as_tenant/model_extensions.rb
@@ -74,7 +74,9 @@ def self.is_scoped_by_tenant?
# 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 association_class.where(:id => value).present?
+ # Invalidate the association unless the parent is known to the tenant or no association has
+ # been set.
+ record.errors.add attr, "is invalid" unless association_class.where(:id => value).present? || value.nil?
end
end
end
View
2  lib/acts_as_tenant/version.rb
@@ -1,3 +1,3 @@
module ActsAsTenant
- VERSION = "0.2.3"
+ VERSION = "0.2.4"
end
View
7 spec/model_extensions_spec.rb
@@ -216,9 +216,8 @@ class SubTask < ActiveRecord::Base
end
describe "It should be possible to create and save an AaT-enabled child without it having a parent" do
- @account = Account.create!(:name => 'baz')
- ActsAsTenant.current_tenant = @account
- @task = Task.new(:name => 'bar')
- lambda { @task.save }.should_not raise_exception
+ @account = Account.create!(:name => 'baz')
+ ActsAsTenant.current_tenant = @account
+ Task.create(:name => 'bar').valid?.should == true
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.