ar_attr_lazy 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/LICENSE +20 -0
- data/README.md +35 -0
- data/Rakefile +62 -0
- data/init.rb +1 -0
- data/lib/mcmire/ar_attr_lazy.rb +32 -0
- data/lib/mcmire/ar_attr_lazy/association_preload_ext.rb +55 -0
- data/lib/mcmire/ar_attr_lazy/base_ext.rb +69 -0
- data/lib/mcmire/ar_attr_lazy/belongs_to_association_ext.rb +19 -0
- data/lib/mcmire/ar_attr_lazy/habtm_ext.rb +23 -0
- data/lib/mcmire/ar_attr_lazy/has_many_through_association_ext.rb +9 -0
- data/lib/mcmire/ar_attr_lazy/join_base_ext.rb +14 -0
- data/lib/mcmire/ar_attr_lazy/version.rb +5 -0
- data/test/factories.rb +96 -0
- data/test/helper.rb +107 -0
- data/test/matchers.rb +172 -0
- data/test/not_using_attr_lazy_test.rb +324 -0
- data/test/setup_migration.rb +67 -0
- data/test/setup_sti_models.rb +61 -0
- data/test/setup_tables_for_not_using.rb +91 -0
- data/test/setup_tables_for_using.rb +35 -0
- data/test/using_attr_lazy_sti_test.rb +357 -0
- data/test/using_attr_lazy_test.rb +359 -0
- metadata +136 -0
@@ -0,0 +1,67 @@
|
|
1
|
+
ActiveRecord::Base.establish_connection(
|
2
|
+
"adapter" => "sqlite3",
|
3
|
+
"database" => ":memory:"
|
4
|
+
)
|
5
|
+
|
6
|
+
ActiveRecord::Migration.suppress_messages do
|
7
|
+
ActiveRecord::Schema.define do
|
8
|
+
create_table :accounts, :force => true do |t|
|
9
|
+
t.string :name, :null => false
|
10
|
+
end
|
11
|
+
|
12
|
+
create_table :avatars, :force => true do |t|
|
13
|
+
t.string :type, :null => false, :default => "Account"
|
14
|
+
t.integer :user_id, :null => false
|
15
|
+
t.string :filename, :null => false
|
16
|
+
t.text :data
|
17
|
+
end
|
18
|
+
|
19
|
+
create_table :users, :force => true do |t|
|
20
|
+
t.string :type, :null => false, :default => "User"
|
21
|
+
t.integer :account_id, :null => false
|
22
|
+
t.string :login, :null => false
|
23
|
+
t.string :password, :null => false
|
24
|
+
t.string :email, :null => false
|
25
|
+
t.string :name, :null => false
|
26
|
+
t.text :bio, :null => false
|
27
|
+
end
|
28
|
+
|
29
|
+
create_table :posts, :force => true do |t|
|
30
|
+
t.string :type, :null => false, :default => "Post"
|
31
|
+
t.integer :author_id, :null => false
|
32
|
+
t.string :title, :null => false
|
33
|
+
t.string :permalink, :null => false
|
34
|
+
t.text :body, :null => false
|
35
|
+
t.text :summary, :null => false
|
36
|
+
end
|
37
|
+
|
38
|
+
create_table :comments, :force => true do |t|
|
39
|
+
t.string :type, :null => false, :default => "Comment"
|
40
|
+
t.integer :post_id, :null => false
|
41
|
+
t.string :name, :null => false
|
42
|
+
t.text :body, :null => false
|
43
|
+
end
|
44
|
+
|
45
|
+
create_table :tags, :force => true do |t|
|
46
|
+
t.string :type, :null => false, :default => "Tag"
|
47
|
+
t.string :name, :null => false
|
48
|
+
t.text :description
|
49
|
+
end
|
50
|
+
|
51
|
+
create_table :posts_tags, :id => false, :force => true do |t|
|
52
|
+
t.integer :post_id, :null => false
|
53
|
+
t.integer :tag_id, :null => false
|
54
|
+
end
|
55
|
+
|
56
|
+
create_table :categories, :force => true do |t|
|
57
|
+
t.string :type, :null => false, :default => "Category"
|
58
|
+
t.string :name, :null => false
|
59
|
+
t.text :description
|
60
|
+
end
|
61
|
+
|
62
|
+
create_table :post_categories, :force => true do |t|
|
63
|
+
t.integer :post_id, :null => false
|
64
|
+
t.integer :category_id, :null => false
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# Have to have a separate file for these so that we can load them
|
2
|
+
# after we put attr_lazy in our models.
|
3
|
+
|
4
|
+
class SpecialPost < Post
|
5
|
+
end
|
6
|
+
|
7
|
+
class SpecialPostWithDefaultScope < PostWithDefaultScope
|
8
|
+
end
|
9
|
+
|
10
|
+
=begin
|
11
|
+
class Account < ActiveRecord::Base
|
12
|
+
has_one :special_user
|
13
|
+
has_one :special_avatar, :through => :user
|
14
|
+
end
|
15
|
+
|
16
|
+
class SpecialAvatar < Avatar
|
17
|
+
end
|
18
|
+
|
19
|
+
class SpecialUser < User
|
20
|
+
end
|
21
|
+
|
22
|
+
class Post < ActiveRecord::Base
|
23
|
+
has_many :special_comments
|
24
|
+
has_many :special_comments_with_default_scope, :class_name => "SpecialCommentWithDefaultScope"
|
25
|
+
has_many :special_comments_with_select, :select => "name", :class_name => "SpecialComment"
|
26
|
+
has_many :special_categories, :through => :post_categories
|
27
|
+
has_many :special_categories_with_default_scope, :through => :post_categories,
|
28
|
+
:class_name => "SpecialCategoryWithDefaultScope",
|
29
|
+
:source => :post
|
30
|
+
has_many :special_categories_with_select, :through => :post_categories,
|
31
|
+
:select => "categories.name",
|
32
|
+
:class_name => "SpecialCategory",
|
33
|
+
:source => :post
|
34
|
+
belongs_to :special_author, :class_name => "SpecialUser"
|
35
|
+
has_and_belongs_to_many :special_tags, :join_table =>
|
36
|
+
has_and_belongs_to_many :special_tags_with_default_scope,
|
37
|
+
:class_name => "SpecialTagWithDefaultScope",
|
38
|
+
:join_table => "posts_tags",
|
39
|
+
:association_foreign_key => "tag_id"
|
40
|
+
has_and_belongs_to_many :special_tags_with_select,
|
41
|
+
:select => "tags.name",
|
42
|
+
:class_name => "SpecialTag",
|
43
|
+
:join_table => "posts_tags",
|
44
|
+
:association_foreign_key => "tag_id"
|
45
|
+
end
|
46
|
+
|
47
|
+
class SpecialComment < Comment
|
48
|
+
end
|
49
|
+
class SpecialCommentWithDefaultScope < CommentWithDefaultScope
|
50
|
+
end
|
51
|
+
|
52
|
+
class SpecialTag < Tag
|
53
|
+
end
|
54
|
+
class SpecialTagWithDefaultScope < TagWithDefaultScope
|
55
|
+
end
|
56
|
+
|
57
|
+
class SpecialCategory < Category
|
58
|
+
end
|
59
|
+
class SpecialCategoryWithDefaultScope < CategoryWithDefaultScope
|
60
|
+
end
|
61
|
+
=end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
class Account < ActiveRecord::Base
|
2
|
+
has_one :user
|
3
|
+
# has_one :through didn't work properly prior to 2.3.4 - see LH #2719
|
4
|
+
if Mcmire::ArAttrLazy.ar_version >= "2.3.4"
|
5
|
+
has_one :avatar, :through => :user
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
class Avatar < ActiveRecord::Base
|
10
|
+
belongs_to :user
|
11
|
+
end
|
12
|
+
|
13
|
+
class User < ActiveRecord::Base
|
14
|
+
has_one :avatar
|
15
|
+
has_many :posts, :foreign_key => "author_id"
|
16
|
+
belongs_to :account
|
17
|
+
end
|
18
|
+
|
19
|
+
class Post < ActiveRecord::Base
|
20
|
+
has_many :comments
|
21
|
+
has_many :comments_with_default_scope, :class_name => "CommentWithDefaultScope"
|
22
|
+
has_many :comments_with_select, :select => "name", :class_name => "Comment"
|
23
|
+
has_many :post_categories
|
24
|
+
has_many :categories, :through => :post_categories
|
25
|
+
has_many :categories_with_default_scope, :through => :post_categories,
|
26
|
+
:class_name => "CategoryWithDefaultScope",
|
27
|
+
:source => :post
|
28
|
+
has_many :categories_with_select, :through => :post_categories,
|
29
|
+
:select => "categories.name",
|
30
|
+
:class_name => "Category",
|
31
|
+
:source => :post
|
32
|
+
belongs_to :author, :class_name => "User"
|
33
|
+
has_and_belongs_to_many :tags
|
34
|
+
has_and_belongs_to_many :tags_with_default_scope,
|
35
|
+
:class_name => "TagWithDefaultScope",
|
36
|
+
:join_table => "posts_tags",
|
37
|
+
:association_foreign_key => "tag_id"
|
38
|
+
has_and_belongs_to_many :tags_with_select,
|
39
|
+
:select => "tags.name",
|
40
|
+
:class_name => "Tag",
|
41
|
+
:join_table => "posts_tags",
|
42
|
+
:association_foreign_key => "tag_id"
|
43
|
+
end
|
44
|
+
|
45
|
+
class Comment < ActiveRecord::Base
|
46
|
+
belongs_to :post
|
47
|
+
end
|
48
|
+
|
49
|
+
class Tag < ActiveRecord::Base
|
50
|
+
has_and_belongs_to_many :posts
|
51
|
+
end
|
52
|
+
|
53
|
+
class PostCategory < ActiveRecord::Base
|
54
|
+
belongs_to :post
|
55
|
+
belongs_to :category
|
56
|
+
end
|
57
|
+
class Category < ActiveRecord::Base
|
58
|
+
has_many :post_categories
|
59
|
+
has_many :posts, :through => :post_categories
|
60
|
+
end
|
61
|
+
|
62
|
+
if Mcmire::ArAttrLazy.ar_version >= 2.3
|
63
|
+
class AccountWithDefaultScope < ActiveRecord::Base
|
64
|
+
set_table_name :accounts
|
65
|
+
default_scope :select => "name"
|
66
|
+
end
|
67
|
+
|
68
|
+
class PostWithDefaultScope < ActiveRecord::Base
|
69
|
+
set_table_name :posts
|
70
|
+
default_scope :select => "title"
|
71
|
+
end
|
72
|
+
|
73
|
+
class CommentWithDefaultScope < ActiveRecord::Base
|
74
|
+
set_table_name :comments
|
75
|
+
belongs_to :post
|
76
|
+
default_scope :select => "name"
|
77
|
+
end
|
78
|
+
|
79
|
+
class TagWithDefaultScope < ActiveRecord::Base
|
80
|
+
set_table_name :tags
|
81
|
+
has_and_belongs_to_many :posts
|
82
|
+
default_scope :select => "name"
|
83
|
+
end
|
84
|
+
|
85
|
+
class CategoryWithDefaultScope < ActiveRecord::Base
|
86
|
+
set_table_name :categories
|
87
|
+
has_many :post_categories
|
88
|
+
has_many :posts, :through => :post_categories
|
89
|
+
default_scope :select => "categories.name"
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
class Avatar < ActiveRecord::Base
|
2
|
+
attr_lazy :data
|
3
|
+
end
|
4
|
+
|
5
|
+
class User < ActiveRecord::Base
|
6
|
+
attr_lazy :bio
|
7
|
+
end
|
8
|
+
|
9
|
+
class Post < ActiveRecord::Base
|
10
|
+
attr_lazy :body, :summary
|
11
|
+
end
|
12
|
+
class PostWithDefaultScope < ActiveRecord::Base
|
13
|
+
attr_lazy :body, :summary
|
14
|
+
end
|
15
|
+
|
16
|
+
class Comment < ActiveRecord::Base
|
17
|
+
attr_lazy :body
|
18
|
+
end
|
19
|
+
class CommentWithDefaultScope < ActiveRecord::Base
|
20
|
+
attr_lazy :body
|
21
|
+
end
|
22
|
+
|
23
|
+
class Tag < ActiveRecord::Base
|
24
|
+
attr_lazy :description
|
25
|
+
end
|
26
|
+
class TagWithDefaultScope < ActiveRecord::Base
|
27
|
+
attr_lazy :description
|
28
|
+
end
|
29
|
+
|
30
|
+
class Category < ActiveRecord::Base
|
31
|
+
attr_lazy :description
|
32
|
+
end
|
33
|
+
class CategoryWithDefaultScope < ActiveRecord::Base
|
34
|
+
attr_lazy :description
|
35
|
+
end
|
@@ -0,0 +1,357 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
# what about STI? (do the lazy attributes carry over?)
|
4
|
+
|
5
|
+
Protest.context "for an STI model whose superclass has lazy attributes" do
|
6
|
+
global_setup do
|
7
|
+
load File.dirname(__FILE__) + '/setup_migration.rb'
|
8
|
+
load File.dirname(__FILE__) + '/setup_tables_for_not_using.rb'
|
9
|
+
load File.dirname(__FILE__) + '/setup_tables_for_using.rb'
|
10
|
+
load File.dirname(__FILE__) + '/setup_sti_models.rb'
|
11
|
+
# likely some other Specials here...
|
12
|
+
Account.make! do |account|
|
13
|
+
User.make!(:account => account) do |user|
|
14
|
+
Avatar.make!(:user => user)
|
15
|
+
SpecialPost.make!(:post, :author => user) do |post|
|
16
|
+
#SpecialComment.make!(:post => post)
|
17
|
+
#post.tags << SpecialTag.make
|
18
|
+
#post.categories << SpecialCategory.make
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
global_teardown do
|
25
|
+
ObjectSpace.each_object(Class) do |klass|
|
26
|
+
Object.remove_class(klass) if klass < ActiveRecord::Base
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def regex(str)
|
31
|
+
Regexp.new(Regexp.escape(str))
|
32
|
+
end
|
33
|
+
|
34
|
+
context "with no associations involved" do
|
35
|
+
test "find selects non-lazy attributes only by default" do
|
36
|
+
lambda { SpecialPost.find(:first) }.should query(
|
37
|
+
regex(%|SELECT "posts"."id","posts"."type","posts"."author_id","posts"."title","posts"."permalink" FROM "posts"|)
|
38
|
+
)
|
39
|
+
end
|
40
|
+
test "accessing a lazy attribute selects that attribute only" do
|
41
|
+
post = SpecialPost.find(:first)
|
42
|
+
lambda { post.body }.should query(
|
43
|
+
regex(%|SELECT "posts"."id","posts"."body" FROM "posts"|)
|
44
|
+
)
|
45
|
+
end
|
46
|
+
test "find still honors an explicit select option" do
|
47
|
+
lambda { SpecialPost.find(:first, :select => "title, permalink") }.should query(
|
48
|
+
regex(%|SELECT title, permalink FROM "posts"|)
|
49
|
+
)
|
50
|
+
end
|
51
|
+
test "find still honors a select option in a parent scope" do
|
52
|
+
lambda {
|
53
|
+
SpecialPost.send(:with_scope, :find => {:select => "title, permalink"}) do
|
54
|
+
SpecialPost.find(:first)
|
55
|
+
end
|
56
|
+
}.should query(
|
57
|
+
regex(%|SELECT title, permalink FROM "posts"|)
|
58
|
+
)
|
59
|
+
end
|
60
|
+
if Mcmire::ArAttrLazy.ar_version >= 2.3
|
61
|
+
test "find still honors a select option in a default scope" do
|
62
|
+
lambda { SpecialPostWithDefaultScope.find(:first) }.should query(
|
63
|
+
regex(%|SELECT title FROM "posts"|)
|
64
|
+
)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
=begin
|
70
|
+
context "accessing a has_many association" do
|
71
|
+
before do
|
72
|
+
@post = Post.first
|
73
|
+
end
|
74
|
+
test "find selects non-lazy attributes by default" do
|
75
|
+
lambda { @post.special_comments.find(:first) }.should query(
|
76
|
+
regex(%|SELECT "comments"."id","comments"."post_id","comments"."name" FROM "comments"|)
|
77
|
+
)
|
78
|
+
end
|
79
|
+
test "find still honors an explicit select option" do
|
80
|
+
lambda { @post.special_comments.find(:first, :select => "name") }.should query(
|
81
|
+
regex(%|SELECT name FROM "comments"|)
|
82
|
+
)
|
83
|
+
end
|
84
|
+
test "find still honors a select option in a parent scope" do
|
85
|
+
lambda {
|
86
|
+
SpecialComment.send(:with_scope, :find => {:select => "name"}) do
|
87
|
+
@post.special_comments.find(:first)
|
88
|
+
end
|
89
|
+
}.should query(
|
90
|
+
regex(%|SELECT name FROM "comments"|)
|
91
|
+
)
|
92
|
+
end
|
93
|
+
test "find still honors a select option in a default scope" do
|
94
|
+
lambda { @post.special_comments_with_default_scope.find(:first) }.should query(
|
95
|
+
regex(%|SELECT name FROM "comments"|)
|
96
|
+
)
|
97
|
+
end
|
98
|
+
test "find still honors a select option in the association definition itself" do
|
99
|
+
lambda { @post.special_comments_with_select.find(:first) }.should query(
|
100
|
+
regex(%|SELECT name FROM "comments"|)
|
101
|
+
)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
context "accessing a belongs_to association" do
|
106
|
+
test "find selects non-lazy attributes by default" do
|
107
|
+
post = Post.first
|
108
|
+
lambda { post.special_author }.should query(
|
109
|
+
regex(%|SELECT "users"."id","users"."account_id","users"."login","users"."password","users"."email","users"."name" FROM "users"|)
|
110
|
+
)
|
111
|
+
end
|
112
|
+
# can't do a find on a belongs_to, so no testing needed for that
|
113
|
+
end
|
114
|
+
|
115
|
+
context "accessing a has_one association" do
|
116
|
+
test "find selects non-lazy attributes by default" do
|
117
|
+
account = Account.first
|
118
|
+
lambda { account.special_user }.should query(
|
119
|
+
regex(%|SELECT "users"."id","users"."account_id","users"."login","users"."password","users"."email","users"."name" FROM "users"|)
|
120
|
+
)
|
121
|
+
end
|
122
|
+
# can't do a find on a has_one, so no testing needed for that
|
123
|
+
end
|
124
|
+
|
125
|
+
context "accessing a has_and_belongs_to_many association" do
|
126
|
+
before do
|
127
|
+
@post = Post.first
|
128
|
+
end
|
129
|
+
test "find selects non-lazy attributes by default" do
|
130
|
+
lambda { @post.special_tags.find(:all) }.should query(
|
131
|
+
regex(%|SELECT "tags"."id","tags"."name" FROM "tags" INNER JOIN "posts_tags" ON "tags".id = "posts_tags".tag_id|)
|
132
|
+
)
|
133
|
+
end
|
134
|
+
test "find still honors an explicit select option" do
|
135
|
+
lambda { @post.special_tags.find(:all, :select => "tags.name") }.should query(
|
136
|
+
regex(%|SELECT tags.name FROM "tags"|)
|
137
|
+
)
|
138
|
+
end
|
139
|
+
test "find still honors a select option in a parent scope" do
|
140
|
+
pending "this fails on Rails 2.3.4"
|
141
|
+
lambda {
|
142
|
+
SpecialTag.send(:with_scope, :find => {:select => "tags.name"}) do
|
143
|
+
@post.special_tags.find(:all)
|
144
|
+
end
|
145
|
+
}.should query(
|
146
|
+
regex(%|SELECT tags.name FROM "tags"|)
|
147
|
+
)
|
148
|
+
end
|
149
|
+
test "find still honors a select option in a default scope" do
|
150
|
+
pending "this fails on Rails 2.3.4"
|
151
|
+
lambda {
|
152
|
+
@post.special_tags_with_default_scope.find(:all)
|
153
|
+
}.should query(
|
154
|
+
regex(%|SELECT tags.name FROM "tags"|)
|
155
|
+
)
|
156
|
+
end
|
157
|
+
test "find still honors a select option in the association definition itself" do
|
158
|
+
lambda {
|
159
|
+
@post.special_tags_with_select.find(:all)
|
160
|
+
}.should query(
|
161
|
+
regex(%|SELECT tags.name FROM "tags"|)
|
162
|
+
)
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
context "accessing a has_many :through association" do
|
167
|
+
before do
|
168
|
+
@post = Post.first
|
169
|
+
end
|
170
|
+
test "find selects non-lazy attributes by default" do
|
171
|
+
lambda { @post.special_categories.find(:all) }.should query(
|
172
|
+
regex(%|SELECT "categories"."id","categories"."name" FROM "categories"|)
|
173
|
+
)
|
174
|
+
end
|
175
|
+
test "find still honors an explicit select option" do
|
176
|
+
lambda { @post.special_categories.find(:all, :select => "categories.name") }.should query(
|
177
|
+
regex(%|SELECT categories.name FROM "categories"|)
|
178
|
+
)
|
179
|
+
end
|
180
|
+
test "find still honors a select option in a parent scope" do
|
181
|
+
pending "this fails on Rails 2.3.4"
|
182
|
+
lambda {
|
183
|
+
SpecialCategory.send(:with_scope, :find => {:select => "categories.name"}) do
|
184
|
+
@post.special_categories.find(:all)
|
185
|
+
end
|
186
|
+
}.should query(
|
187
|
+
regex(%|SELECT categories.name FROM "categories"|)
|
188
|
+
)
|
189
|
+
end
|
190
|
+
test "find still honors a select option in a default scope" do
|
191
|
+
pending "this fails on Rails 2.3.4"
|
192
|
+
lambda {
|
193
|
+
@post.special_categories_with_default_scope.find(:all)
|
194
|
+
}.should query(
|
195
|
+
regex(%|SELECT categories.name FROM "categories"|)
|
196
|
+
)
|
197
|
+
end
|
198
|
+
test "find still honors a select option in the association definition itself" do
|
199
|
+
lambda {
|
200
|
+
@post.special_categories_with_select.find(:all)
|
201
|
+
}.should query(
|
202
|
+
regex(%|SELECT categories.name FROM "categories"|)
|
203
|
+
)
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
# has_one :through didn't work properly prior to 2.3.4 - see LH #2719
|
208
|
+
if Mcmire::ArAttrLazy.ar_version >= "2.3.4"
|
209
|
+
context "accessing a has_one :through association" do
|
210
|
+
test "find selects non-lazy attributes by default" do
|
211
|
+
account = Account.first
|
212
|
+
lambda { account.special_avatar }.should query(
|
213
|
+
regex(%|SELECT "avatars"."id","avatars"."user_id","avatars"."filename" FROM "avatars"|)
|
214
|
+
)
|
215
|
+
end
|
216
|
+
# can't do a find on a has_one, so no testing needed for that
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
220
|
+
context "eager loading a has_many association (association preloading)" do
|
221
|
+
test "find selects non-lazy attributes by default" do
|
222
|
+
lambda {
|
223
|
+
Post.find(:first, :include => :special_comments)
|
224
|
+
}.should query(
|
225
|
+
regex(%|SELECT "comments"."id","comments"."post_id","comments"."name" FROM "comments"|)
|
226
|
+
)
|
227
|
+
end
|
228
|
+
# can't test for an explicit select since that will force a table join
|
229
|
+
# can't test for a scope select since association preloading doesn't honor those
|
230
|
+
end
|
231
|
+
context "eager loading a has_many association (table join)" do
|
232
|
+
test "find selects non-lazy attributes by default" do
|
233
|
+
lambda {
|
234
|
+
Post.find(:first, :include => :special_comments, :order => "comments.id")
|
235
|
+
}.should query(
|
236
|
+
regex(%|SELECT "posts"."id" AS t0_r0, "posts"."author_id" AS t0_r1, "posts"."title" AS t0_r2, "posts"."permalink" AS t0_r3, "comments"."id" AS t1_r0, "comments"."post_id" AS t1_r1, "comments"."name" AS t1_r2 FROM "posts"|)
|
237
|
+
)
|
238
|
+
end
|
239
|
+
# can't test for an explicit select since that clashes with the table join anyway
|
240
|
+
# can't test for a scope for the same reason
|
241
|
+
end
|
242
|
+
|
243
|
+
context "eager loading a has_one association (association preloading)" do
|
244
|
+
test "find selects non-lazy attributes by default" do
|
245
|
+
lambda { Account.find(:first, :include => :special_user) }.should query(
|
246
|
+
regex(%|SELECT "users"."id","users"."account_id","users"."login","users"."password","users"."email","users"."name" FROM "users"|)
|
247
|
+
)
|
248
|
+
end
|
249
|
+
# can't test for an explicit select since that will force a table join
|
250
|
+
# can't test for a scope select since association preloading doesn't honor those
|
251
|
+
end
|
252
|
+
context "eager loading a has_one association (table join)" do
|
253
|
+
test "find selects non-lazy attributes by default" do
|
254
|
+
lambda {
|
255
|
+
Account.find(:first, :include => :special_user, :order => "users.id")
|
256
|
+
}.should query(
|
257
|
+
regex(%|SELECT "accounts"."id" AS t0_r0, "accounts"."name" AS t0_r1, "users"."id" AS t1_r0, "users"."account_id" AS t1_r1, "users"."login" AS t1_r2, "users"."password" AS t1_r3, "users"."email" AS t1_r4, "users"."name" AS t1_r5 FROM "accounts"|)
|
258
|
+
)
|
259
|
+
end
|
260
|
+
# can't test for an explicit select since that clashes with the table join anyway
|
261
|
+
# can't test for a scope for the same reason
|
262
|
+
end
|
263
|
+
|
264
|
+
context "eager loading a belongs_to association (association preloading)" do
|
265
|
+
test "find selects non-lazy attributes by default" do
|
266
|
+
lambda {
|
267
|
+
Post.find(:first, :include => :special_author)
|
268
|
+
}.should query(
|
269
|
+
regex(%|SELECT "users"."id","users"."account_id","users"."login","users"."password","users"."email","users"."name" FROM "users"|)
|
270
|
+
)
|
271
|
+
end
|
272
|
+
# can't test for an explicit select since that will force a table join
|
273
|
+
# can't test for a scope select since association preloading doesn't honor those
|
274
|
+
end
|
275
|
+
context "eager loading a belongs_to association (table join)" do
|
276
|
+
test "find selects non-lazy attributes by default" do
|
277
|
+
lambda {
|
278
|
+
Post.find(:first, :include => :special_author, :order => "users.id")
|
279
|
+
}.should query(
|
280
|
+
regex(%|SELECT "posts"."id" AS t0_r0, "posts"."author_id" AS t0_r1, "posts"."title" AS t0_r2, "posts"."permalink" AS t0_r3, "users"."id" AS t1_r0, "users"."account_id" AS t1_r1, "users"."login" AS t1_r2, "users"."password" AS t1_r3, "users"."email" AS t1_r4, "users"."name" AS t1_r5 FROM "posts"|)
|
281
|
+
)
|
282
|
+
end
|
283
|
+
# can't test for an explicit select since that clashes with the table join anyway
|
284
|
+
# can't test for a scope for the same reason
|
285
|
+
end
|
286
|
+
|
287
|
+
context "eager loading a has_and_belongs_to_many association (association preloading)" do
|
288
|
+
test "find selects non-lazy attributes by default" do
|
289
|
+
lambda {
|
290
|
+
Post.find(:first, :include => :special_tags)
|
291
|
+
}.should query(
|
292
|
+
regex(%|SELECT "tags"."id","tags"."name", t0.post_id as the_parent_record_id FROM "tags"|)
|
293
|
+
)
|
294
|
+
end
|
295
|
+
# can't test for an explicit select since that will force a table join
|
296
|
+
# can't test for a scope select since association preloading doesn't honor those
|
297
|
+
end
|
298
|
+
context "eager loading a has_and_belongs_to_many association (table join)" do
|
299
|
+
test "find selects non-lazy attributes by default" do
|
300
|
+
lambda {
|
301
|
+
Post.find(:first, :include => :special_tags, :order => "tags.id")
|
302
|
+
}.should query(
|
303
|
+
regex(%|SELECT "posts"."id" AS t0_r0, "posts"."author_id" AS t0_r1, "posts"."title" AS t0_r2, "posts"."permalink" AS t0_r3, "tags"."id" AS t1_r0, "tags"."name" AS t1_r1 FROM "posts"|)
|
304
|
+
)
|
305
|
+
end
|
306
|
+
# can't test for an explicit select since that clashes with the table join anyway
|
307
|
+
# can't test for a scope for the same reason
|
308
|
+
end
|
309
|
+
|
310
|
+
context "eager loading a has_many :through association (association preloading)" do
|
311
|
+
test "find selects non-lazy attributes by default" do
|
312
|
+
lambda {
|
313
|
+
Post.find(:first, :include => :special_categories)
|
314
|
+
}.should query(
|
315
|
+
regex(%|SELECT "categories"."id","categories"."name" FROM "categories"|)
|
316
|
+
)
|
317
|
+
end
|
318
|
+
# can't test for an explicit select since that will force a table join
|
319
|
+
# can't test for a scope select since association preloading doesn't honor those
|
320
|
+
end
|
321
|
+
context "eager loading a has_many :through association (table join)" do
|
322
|
+
test "find selects non-lazy attributes by default" do
|
323
|
+
lambda {
|
324
|
+
Post.find(:first, :include => :special_categories, :order => "categories.id")
|
325
|
+
}.should query(
|
326
|
+
regex(%|SELECT "posts"."id" AS t0_r0, "posts"."author_id" AS t0_r1, "posts"."title" AS t0_r2, "posts"."permalink" AS t0_r3, "categories"."id" AS t1_r0, "categories"."name" AS t1_r1 FROM "posts"|)
|
327
|
+
)
|
328
|
+
end
|
329
|
+
# can't test for an explicit select since that clashes with the table join anyway
|
330
|
+
# can't test for a scope for the same reason
|
331
|
+
end
|
332
|
+
|
333
|
+
# has_one :through didn't work properly prior to 2.3.4 - see LH #2719
|
334
|
+
if Mcmire::ArAttrLazy.ar_version >= "2.3.4"
|
335
|
+
context "eager loading a has_one :through association (association preloading)" do
|
336
|
+
test "find selects non-lazy attributes by default" do
|
337
|
+
lambda { Account.find(:first, :include => :special_avatar) }.should query(
|
338
|
+
regex(%|SELECT "avatars"."id","avatars"."user_id","avatars"."filename" FROM "avatars"|)
|
339
|
+
)
|
340
|
+
end
|
341
|
+
# can't test for an explicit select since that will force a table join
|
342
|
+
# can't test for a scope select since association preloading doesn't honor those
|
343
|
+
end
|
344
|
+
context "eager loading a has_one :through association (table join)" do
|
345
|
+
test "find selects non-lazy attributes by default" do
|
346
|
+
pending "this is failing for some reason!"
|
347
|
+
lambda {
|
348
|
+
Account.find(:first, :include => :special_avatar, :order => "avatars.id")
|
349
|
+
}.should query(%|SELECT "accounts"."id" AS t0_r0, "accounts"."name" AS t0_r1, "avatars"."id" AS t1_r0, "avatars"."user_id" AS t1_r1, "avatars"."filename" AS t1_r2 FROM "accounts"|)
|
350
|
+
end
|
351
|
+
# can't test for an explicit select since that clashes with the table join anyway
|
352
|
+
# can't test for a scope for the same reason
|
353
|
+
end
|
354
|
+
end
|
355
|
+
=end
|
356
|
+
|
357
|
+
end
|