@@ -6,6 +6,10 @@ module ActiveRecord
6
6
describe Context do
7
7
subject { Context . new ( Person ) }
8
8
9
+ if ::ActiveRecord ::VERSION ::STRING >= "3.1"
10
+ its ( :alias_tracker ) { should be_a ::ActiveRecord ::Associations ::AliasTracker }
11
+ end
12
+
9
13
describe '#relation_for' do
10
14
it 'returns relation for given object' do
11
15
expect ( subject . object ) . to be_an ::ActiveRecord ::Relation
@@ -30,6 +34,45 @@ module ActiveRecord
30
34
end
31
35
end
32
36
37
+ describe "sharing context across searches" do
38
+ let ( :shared_context ) { Context . for ( Person ) }
39
+
40
+ before do
41
+ Search . new ( Person , { :parent_name_eq => 'A' } , context : shared_context )
42
+ Search . new ( Person , { :children_name_eq => 'B' } , context : shared_context )
43
+ end
44
+
45
+ describe '#join_associations' , :if => ::ActiveRecord ::VERSION ::STRING <= '4.0' do
46
+ it 'returns dependent join associations for all searches run against the context' do
47
+ parents , children = shared_context . join_associations
48
+
49
+ expect ( children . aliased_table_name ) . to eq "children_people"
50
+ expect ( parents . aliased_table_name ) . to eq "parents_people"
51
+ end
52
+
53
+ it 'can be rejoined to execute a valid query' do
54
+ parents , children = shared_context . join_associations
55
+
56
+ expect { Person . joins ( parents ) . joins ( children ) . to_a } . to_not raise_error
57
+ end
58
+ end
59
+
60
+ describe '#join_sources' , :if => ::ActiveRecord ::VERSION ::STRING >= '3.1' do
61
+ it 'returns dependent arel join nodes for all searches run against the context' do
62
+ parents , children = shared_context . join_sources
63
+
64
+ expect ( children . left . name ) . to eq "children_people"
65
+ expect ( parents . left . name ) . to eq "parents_people"
66
+ end
67
+
68
+ it 'can be rejoined to execute a valid query' do
69
+ parents , children = shared_context . join_sources
70
+
71
+ expect { Person . joins ( parents ) . joins ( children ) . to_a } . to_not raise_error
72
+ end
73
+ end
74
+ end
75
+
33
76
it 'contextualizes strings to attributes' do
34
77
attribute = subject . contextualize 'children_children_parent_name'
35
78
expect ( attribute ) . to be_a Arel ::Attributes ::Attribute
0 commit comments