Skip to content

Commit 68296e6

Browse files
committed
Merge master into reload_all_records branch
2 parents 8930328 + a7df1dc commit 68296e6

File tree

5 files changed

+43
-9
lines changed

5 files changed

+43
-9
lines changed

lib/callsite.rb

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,50 @@ class Callsite
1515
def initialize( klass, signature )
1616
@klass = klass
1717
@signature = signature
18-
@columns = setup_columns
19-
@associations = setup_associations
2018
end
2119

2220
# Flag a column as seen
2321
#
2422
def column!( column )
2523
Mtx.synchronize do
26-
@columns << column
24+
columns << column
2725
end
2826
end
2927

3028
# Flag an association as seen
3129
#
3230
def association!( association )
3331
Mtx.synchronize do
34-
@associations << association if preloadable_association?( association )
32+
associations << association if preloadable_association?( association )
3533
end
3634
end
3735

3836
def inspect
39-
"<##{@klass.name} :select => '#{@klass.scrooge_select_sql( @columns )}', :include => [#{associations_for_inspect}]>"
37+
"<##{@klass.name} :select => '#{@klass.scrooge_select_sql( columns )}', :include => [#{associations_for_inspect}]>"
38+
end
39+
40+
# Lazy init default columns
41+
#
42+
def default_columns
43+
@default_columns ||= setup_columns()
44+
end
45+
46+
# Lazy init columns
47+
#
48+
def columns
49+
@columns ||= default_columns.dup
50+
end
51+
52+
# Lazy init associations
53+
#
54+
def associations
55+
@associations ||= setup_associations()
4056
end
4157

4258
private
4359

4460
def associations_for_inspect
45-
@associations.map{|a| ":#{a.to_s}" }.join(', ')
61+
associations.map{|a| ":#{a.to_s}" }.join(', ')
4662
end
4763

4864
# Only register associations that isn't polymorphic or a collection

lib/optimizations/associations/macro.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,10 @@ def scrooge_preloading_exclude
5050
# Let's not preload polymorphic associations or collections
5151
#
5252
def preloadable_associations
53-
@@preloadable_associations[self.name] ||= reflect_on_all_associations.reject{|a| a.options[:polymorphic] || a.macro == :has_many }.map{|a| a.name }
54-
end
55-
53+
@@preloadable_associations[self.name] ||=
54+
reflect_on_all_associations.reject{|a| a.options[:xxxpolymorphic] || a.macro == :xxxhas_many}.map(&:name)
55+
end
56+
5657
end
5758

5859
module InstanceMethods

test/callsite_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@ def setup
1616
assert_equal Scrooge::Callsite.new( MysqlUser, 123456 ).columns, Set["User","inheritance"]
1717
end
1818

19+
test "should be able to return all augmented columns" do
20+
assert_equal @callsite.augmented_columns, Set.new
21+
@callsite.column! :Db
22+
assert_equal @callsite.augmented_columns, Set[:Db]
23+
end
24+
25+
test "should be able to determine if any columns has been augmented" do
26+
assert !@callsite.augmented_columns?
27+
@callsite.column! :Db
28+
assert @callsite.augmented_columns?
29+
end
30+
1931
test "should be inspectable" do
2032
@callsite.association! :mysql_user
2133
@callsite.column! :db

test/helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require 'rubygems'
33
require 'mocha'
44
require 'active_support/test_case'
5+
ENV["BACKTRACE"] = "1"
56

67
module Scrooge
78
class Test

test/optimizations/associations/macro_test.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ class OptimizationsAssociationsMacroTest < ActiveSupport::TestCase
77
test "should flag a record as being scrooged when found through a supported SQL query" do
88
assert MysqlUser.find(:first).scrooged?
99
end
10+
11+
test "should always flag records via Model.find with a custom :select requirement as scrooged" do
12+
assert MysqlUser.find(:first, :select => 'user.Password' ).scrooged?
13+
end
1014

1115
test "should be able to flag any associations instantiated from a record" do
1216
@user = MysqlUser.find(:first)

0 commit comments

Comments
 (0)