Skip to content
Browse files

Add failing specs for association_matcher autosave

has_one, has_many, belongs_to, and has_and_belongs_to_many relationships can
have an `:autosave` option, which causes the relevant associated records to be
automatically saved when their parent is saved.

I've added failing specs for passing and failing cases for each of these
association types.

https://github.com/rails/rails/blob/master/activerecord/lib/active_record/autosave_association.rb
  • Loading branch information...
1 parent 35b65c2 commit 7c582ba9c81dbeeb70421aee6d7233073f2b28a1 @calebthompson committed with
Showing with 68 additions and 0 deletions.
  1. +68 −0 spec/shoulda/active_record/association_matcher_spec.rb
View
68 spec/shoulda/active_record/association_matcher_spec.rb
@@ -98,6 +98,23 @@
end
Child.new.should_not @matcher.class_name('TreeChild')
end
+
+ it "should accept an association with a valid :autosave option" do
+ define_model :parent, :adopter => :boolean
+ define_model :child, :parent_id => :integer do
+ belongs_to :parent, :autosave => true
+ end
+ Child.new.should @matcher.autosave(:true)
+ end
+
+ it "should reject an association with a valid :autosave option" do
+ define_model :parent, :adopter => :boolean
+ define_model :child, :parent_id => :integer do
+ belongs_to :parent, :autosave => false
+ end
+ Child.new.should_not @matcher.autosave(:true)
+ end
+
end
context "have_many" do
@@ -240,6 +257,22 @@
Parent.new.should_not @matcher.class_name('Node')
end
+ it "should accept an association with a valid :autosave option" do
+ define_model :child, :parent_id => :integer
+ define_model :parent do
+ has_many :children, :autosave => true
+ end
+ Parent.new.should @matcher.autosave(:true)
+ end
+
+ it "should reject an association with a valid :autosave option" do
+ define_model :child, :parent_id => :integer
+ define_model :parent do
+ has_many :children, :autosave => false
+ end
+ Parent.new.should_not @matcher.autosave(:true)
+ end
+
it "should accept an association with a nonstandard reverse foreign key, using :inverse_of" do
define_model :child, :ancestor_id => :integer do
belongs_to :ancestor, :inverse_of => :children, :class_name => :Parent
@@ -364,6 +397,22 @@
Person.new.should_not @matcher.class_name('PersonDetail')
end
+ it "should accept an association with a valid :autosave option" do
+ define_model :detail, :person_id => :integer, :disabled => :boolean
+ define_model :person do
+ has_one :detail, :autosave => true
+ end
+ Person.new.should @matcher.autosave(:true)
+ end
+
+ it "should reject an association with a valid :autosave option" do
+ define_model :detail, :person_id => :integer, :disabled => :boolean
+ define_model :person do
+ has_one :detail, :autosave => false
+ end
+ Person.new.should_not @matcher.autosave(:true)
+ end
+
end
context "have_and_belong_to_many" do
@@ -445,5 +494,24 @@
Person.new.should_not @matcher.class_name('PersonRelatives')
end
+ it "should accept an association with a valid :autosave option" do
+ define_model :relatives, :adopted => :boolean
+ define_model :person do
+ has_and_belongs_to_many :relatives, :autosave => true
+ end
+ define_model :people_relative, :person_id => :integer,
+ :relative_id => :integer
+ Person.new.should @matcher.autosave(:true)
+ end
+
+ it "should reject an association with a bad :autosave option" do
+ define_model :relatives, :adopted => :boolean
+ define_model :person do
+ has_and_belongs_to_many :relatives
+ end
+ define_model :people_relative, :person_id => :integer,
+ :relative_id => :integer
+ Person.new.should_not @matcher.autosave(:true)
+ end
end
end

0 comments on commit 7c582ba

Please sign in to comment.
Something went wrong with that request. Please try again.