2008-05-18 06:22:34 +02:00
|
|
|
require 'cases/helper'
|
2009-03-16 15:55:30 +01:00
|
|
|
require 'models/post'
|
2009-09-05 09:01:46 +02:00
|
|
|
require 'models/author'
|
2009-03-16 15:55:30 +01:00
|
|
|
require 'models/comment'
|
|
|
|
require 'models/category'
|
|
|
|
require 'models/categorization'
|
2010-05-25 19:45:45 +02:00
|
|
|
require 'active_support/core_ext/array/random_access'
|
2008-05-18 06:22:34 +02:00
|
|
|
|
2008-10-27 07:47:01 +01:00
|
|
|
module Remembered
|
|
|
|
def self.included(base)
|
|
|
|
base.extend ClassMethods
|
|
|
|
base.class_eval do
|
|
|
|
after_create :remember
|
|
|
|
protected
|
|
|
|
def remember; self.class.remembered << self; end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module ClassMethods
|
|
|
|
def remembered; @@remembered ||= []; end
|
2010-05-25 19:45:45 +02:00
|
|
|
def random_element; @@remembered.random_element; end
|
2008-10-27 07:47:01 +01:00
|
|
|
end
|
|
|
|
end
|
2008-05-18 06:22:34 +02:00
|
|
|
|
|
|
|
class ShapeExpression < ActiveRecord::Base
|
|
|
|
belongs_to :shape, :polymorphic => true
|
|
|
|
belongs_to :paint, :polymorphic => true
|
|
|
|
end
|
|
|
|
|
|
|
|
class Circle < ActiveRecord::Base
|
|
|
|
has_many :shape_expressions, :as => :shape
|
2008-10-27 07:47:01 +01:00
|
|
|
include Remembered
|
2008-05-18 06:22:34 +02:00
|
|
|
end
|
|
|
|
class Square < ActiveRecord::Base
|
|
|
|
has_many :shape_expressions, :as => :shape
|
2008-10-27 07:47:01 +01:00
|
|
|
include Remembered
|
2008-05-18 06:22:34 +02:00
|
|
|
end
|
|
|
|
class Triangle < ActiveRecord::Base
|
|
|
|
has_many :shape_expressions, :as => :shape
|
2008-10-27 07:47:01 +01:00
|
|
|
include Remembered
|
2008-05-18 06:22:34 +02:00
|
|
|
end
|
|
|
|
class PaintColor < ActiveRecord::Base
|
|
|
|
has_many :shape_expressions, :as => :paint
|
|
|
|
belongs_to :non_poly, :foreign_key => "non_poly_one_id", :class_name => "NonPolyOne"
|
2008-10-27 07:47:01 +01:00
|
|
|
include Remembered
|
2008-05-18 06:22:34 +02:00
|
|
|
end
|
|
|
|
class PaintTexture < ActiveRecord::Base
|
|
|
|
has_many :shape_expressions, :as => :paint
|
|
|
|
belongs_to :non_poly, :foreign_key => "non_poly_two_id", :class_name => "NonPolyTwo"
|
2008-10-27 07:47:01 +01:00
|
|
|
include Remembered
|
2008-05-18 06:22:34 +02:00
|
|
|
end
|
|
|
|
class NonPolyOne < ActiveRecord::Base
|
|
|
|
has_many :paint_colors
|
2008-10-27 07:47:01 +01:00
|
|
|
include Remembered
|
2008-05-18 06:22:34 +02:00
|
|
|
end
|
|
|
|
class NonPolyTwo < ActiveRecord::Base
|
|
|
|
has_many :paint_textures
|
2008-10-27 07:47:01 +01:00
|
|
|
include Remembered
|
2008-05-18 06:22:34 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class EagerLoadPolyAssocsTest < ActiveRecord::TestCase
|
|
|
|
NUM_SIMPLE_OBJS = 50
|
|
|
|
NUM_SHAPE_EXPRESSIONS = 100
|
|
|
|
|
|
|
|
def setup
|
|
|
|
generate_test_object_graphs
|
|
|
|
end
|
2009-09-05 09:01:46 +02:00
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
def teardown
|
2009-09-05 09:01:46 +02:00
|
|
|
[Circle, Square, Triangle, PaintColor, PaintTexture,
|
2008-05-18 06:22:34 +02:00
|
|
|
ShapeExpression, NonPolyOne, NonPolyTwo].each do |c|
|
|
|
|
c.delete_all
|
|
|
|
end
|
2009-09-05 09:01:46 +02:00
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def generate_test_object_graphs
|
|
|
|
1.upto(NUM_SIMPLE_OBJS) do
|
|
|
|
[Circle, Square, Triangle, NonPolyOne, NonPolyTwo].map(&:create!)
|
|
|
|
end
|
2008-10-27 07:47:01 +01:00
|
|
|
1.upto(NUM_SIMPLE_OBJS) do
|
2010-05-25 19:45:45 +02:00
|
|
|
PaintColor.create!(:non_poly_one_id => NonPolyOne.random_element.id)
|
|
|
|
PaintTexture.create!(:non_poly_two_id => NonPolyTwo.random_element.id)
|
2008-05-18 06:22:34 +02:00
|
|
|
end
|
2008-10-27 07:47:01 +01:00
|
|
|
1.upto(NUM_SHAPE_EXPRESSIONS) do
|
2010-05-25 19:45:45 +02:00
|
|
|
shape_type = [Circle, Square, Triangle].random_element
|
|
|
|
paint_type = [PaintColor, PaintTexture].random_element
|
|
|
|
ShapeExpression.create!(:shape_type => shape_type.to_s, :shape_id => shape_type.random_element.id,
|
|
|
|
:paint_type => paint_type.to_s, :paint_id => paint_type.random_element.id)
|
2008-05-18 06:22:34 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_include_query
|
|
|
|
res = 0
|
|
|
|
res = ShapeExpression.find :all, :include => [ :shape, { :paint => :non_poly } ]
|
|
|
|
assert_equal NUM_SHAPE_EXPRESSIONS, res.size
|
|
|
|
assert_queries(0) do
|
|
|
|
res.each do |se|
|
|
|
|
assert_not_nil se.paint.non_poly, "this is the association that was loading incorrectly before the change"
|
|
|
|
assert_not_nil se.shape, "just making sure other associations still work"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-03-16 15:55:30 +01:00
|
|
|
|
|
|
|
class EagerLoadNestedIncludeWithMissingDataTest < ActiveRecord::TestCase
|
|
|
|
def setup
|
|
|
|
@davey_mcdave = Author.create(:name => 'Davey McDave')
|
|
|
|
@first_post = @davey_mcdave.posts.create(:title => 'Davey Speaks', :body => 'Expressive wordage')
|
|
|
|
@first_comment = @first_post.comments.create(:body => 'Inflamatory doublespeak')
|
|
|
|
@first_categorization = @davey_mcdave.categorizations.create(:category => Category.first, :post => @first_post)
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
@davey_mcdave.destroy
|
|
|
|
@first_post.destroy
|
|
|
|
@first_comment.destroy
|
|
|
|
@first_categorization.destroy
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_missing_data_in_a_nested_include_should_not_cause_errors_when_constructing_objects
|
|
|
|
assert_nothing_raised do
|
|
|
|
# @davey_mcdave doesn't have any author_favorites
|
|
|
|
includes = {:posts => :comments, :categorizations => :category, :author_favorites => :favorite_author }
|
|
|
|
Author.all :include => includes, :conditions => {:authors => {:name => @davey_mcdave.name}}, :order => 'categories.name'
|
|
|
|
end
|
|
|
|
end
|
2009-09-05 09:01:46 +02:00
|
|
|
end
|