Skip to content

Commit

Permalink
Add failing specs for association_matcher autosave
Browse files Browse the repository at this point in the history
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
calebhearth committed Sep 21, 2012
1 parent 35b65c2 commit 7c582ba
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions spec/shoulda/active_record/association_matcher_spec.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.