require File.join(File.dirname(__FILE__), 'test_helper') class ActsAsJoinableTest < ActiveRecord::TestCase context "ActsAsJoinable" do context "modeling groups" do setup do @grandparent = Group.create!(:title => "Grandparent") @parent = Group.create!(:title => "Parent") @child = Group.create!(:title => "Child") @child_2 = Group.create!(:title => "Child 2") end should "allow self referential and sti associations" do @sub_class = SubGroup.create!(:title => "Subgroup!") @grandparent.nesteds << @sub_class @sub_class.reload @grandparent.reload association = Relationship.first assert @sub_class.valid? assert_equal "SubGroup", association.child_type assert_not_equal "Group", association.child_type assert_equal @grandparent, SubGroup.first.parents.first end should "have one instead of many" do parent = SubGroup.create!(:title => "subgroup with has_one :cover_image") child = Asset.create! another_child = Asset.create!(:title => "Not a cover_image!") parent.cover_image = child parent.galleries << another_child parent.save! parent.reload assert parent.valid? assert_equal child, parent.cover_image assert_equal 1, parent.galleries.length assert_equal 2, parent.assets.length assert_equal 0, parent.images.length assert_equal 0, parent.pictures.length # add another parent.cover_image = another_child parent.save! parent.reload assert_equal another_child, parent.cover_image another_child.reload assert_equal parent, another_child.groups.first end should "allow nested attributes" do assert_equal 0, Asset.count parent = SubGroup.create!( :title => "subgroup with has_one :cover_image", :cover_image_attributes => {:title => "an image"} ) assert_equal 1, Asset.count end should "have optimized sql calls" do Relationship.delete_all group = Group.create! group.posts << Post.create! group.posts << Post.create! assert_queries(1) { Group.all } assert_queries(1) { Group.first } assert_queries(2) do Group.first(:include => [:parent_relationships]) end # parent, child, and self assert_queries(3) do group.destroy end assert_equal 0, Relationship.count end context "follow validation conventions" do end context "find by join model" do end context "dirty join attributes" do setup do Group.delete_all @group = Group.create! end should "get dirty attributes" do assert !@group.join_attributes_changed? @group.posts << Post.create! assert @group.join_attributes_changed? puts @group.join_attribute_changes.inspect end end teardown do destroy_models end end end end