spec/integration/integration_spec.rb in mongoid-history-0.2.2 vs spec/integration/integration_spec.rb in mongoid-history-0.2.3

- old
+ new

@@ -15,10 +15,14 @@ field :body field :rating embeds_many :comments embeds_one :section + embeds_many :tags, :cascade_callbacks => true + + accepts_nested_attributes_for :tags, :allow_destroy => true + track_history :on => [:title, :body], :track_destroy => true end class Comment include Mongoid::Document @@ -48,10 +52,19 @@ field :email field :name track_history :except => [:email] end + + class Tag + include Mongoid::Document + include Mongoid::Timestamps + include Mongoid::History::Trackable + + field :title + track_history :on => [:title], :scope => :post, :track_create => true, :track_destroy => true + end end before :each do @user = User.create(:name => "Aaron", :email => "aaron@randomemail.com") @another_user = User.create(:name => "Another Guy", :email => "anotherguy@randomemail.com") @@ -112,11 +125,10 @@ it "should return affected attributes from track record" do @post.destroy @post.history_tracks.last.affected["title"].should == "Test" end - end describe "on update non-embedded" do it "should create a history track if changed attributes match tracked attributes" do lambda { @@ -175,11 +187,11 @@ end it "should exclude defined options" do @user.update_attributes(:name => "Aaron2", :email => "aaronsnewemail@randomemail.com") @user.history_tracks.first.modified.should == { - "name" => "Aaron2" + "name" => "Aaron2" } end end describe "on update non-embedded twice" do @@ -341,9 +353,35 @@ @track = @post.history_tracks.last @track.undo!(@user) @track.redo!(@user) @post.reload @post.comments.count.should == 2 + end + end + + describe "embedded with cascading callbacks" do + before(:each) do + @tag_foo = @post.tags.create(:title => "foo", :modifier => @user) + @tag_bar = @post.tags.create(:title => "bar", :modifier => @user) + end + + it "should have cascaded the creation callbacks and set timestamps" do + @tag_foo.created_at.should_not be_nil + @tag_foo.updated_at.should_not be_nil + end + + it "should allow an update through the parent model" do + update_hash = { "post" => { "tags_attributes" => { "1234" => { "id" => @tag_bar.id, "title" => "baz" } } } } + @post.update_attributes(update_hash["post"]) + @post.tags.last.title.should == "baz" + end + + it "should be possible to destroy through parent model using canoncial _destroy macro" do + @post.tags.count.should == 2 + update_hash = { "post" => { "tags_attributes" => { "1234" => { "id" => @tag_bar.id, "title" => "baz", "_destroy" => "true"} } } } + @post.update_attributes(update_hash["post"]) + @post.tags.count.should == 1 + @post.history_tracks.last.action.should == "destroy" end end describe "non-embedded" do it "should undo changes" do