squeel 0.5.0 → 0.5.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +115 -39
- data/lib/squeel/adapters/active_record.rb +22 -5
- data/lib/squeel/adapters/active_record/3.0/association_preload.rb +15 -0
- data/lib/squeel/adapters/active_record/3.0/compat.rb +143 -0
- data/lib/squeel/adapters/active_record/3.0/context.rb +67 -0
- data/lib/squeel/adapters/active_record/3.0/join_association.rb +54 -0
- data/lib/squeel/adapters/active_record/3.0/join_dependency.rb +84 -0
- data/lib/squeel/adapters/active_record/3.0/relation.rb +327 -0
- data/lib/squeel/adapters/active_record/context.rb +67 -0
- data/lib/squeel/adapters/active_record/join_association.rb +10 -56
- data/lib/squeel/adapters/active_record/join_dependency.rb +22 -7
- data/lib/squeel/adapters/active_record/preloader.rb +21 -0
- data/lib/squeel/adapters/active_record/relation.rb +84 -38
- data/lib/squeel/context.rb +38 -0
- data/lib/squeel/dsl.rb +1 -1
- data/lib/squeel/nodes/join.rb +18 -0
- data/lib/squeel/nodes/key_path.rb +2 -2
- data/lib/squeel/nodes/stub.rb +5 -1
- data/lib/squeel/version.rb +1 -1
- data/lib/squeel/visitors.rb +2 -2
- data/lib/squeel/visitors/{order_visitor.rb → attribute_visitor.rb} +1 -2
- data/lib/squeel/visitors/predicate_visitor.rb +13 -11
- data/lib/squeel/visitors/symbol_visitor.rb +48 -0
- data/spec/helpers/squeel_helper.rb +17 -1
- data/spec/spec_helper.rb +31 -0
- data/spec/squeel/adapters/active_record/context_spec.rb +50 -0
- data/spec/squeel/adapters/active_record/join_association_spec.rb +1 -1
- data/spec/squeel/adapters/active_record/join_depdendency_spec.rb +1 -1
- data/spec/squeel/adapters/active_record/relation_spec.rb +166 -25
- data/spec/squeel/dsl_spec.rb +6 -6
- data/spec/squeel/nodes/join_spec.rb +16 -3
- data/spec/squeel/nodes/stub_spec.rb +12 -0
- data/spec/squeel/visitors/{order_visitor_spec.rb → attribute_visitor_spec.rb} +4 -5
- data/spec/squeel/visitors/predicate_visitor_spec.rb +18 -6
- data/spec/squeel/visitors/symbol_visitor_spec.rb +42 -0
- data/squeel.gemspec +2 -2
- metadata +21 -13
- data/lib/squeel/contexts/join_dependency_context.rb +0 -74
- data/lib/squeel/visitors/select_visitor.rb +0 -103
- data/spec/squeel/contexts/join_dependency_context_spec.rb +0 -43
- data/spec/squeel/visitors/select_visitor_spec.rb +0 -115
@@ -1,43 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module Squeel
|
4
|
-
module Contexts
|
5
|
-
describe JoinDependencyContext do
|
6
|
-
before do
|
7
|
-
@jd = ActiveRecord::Associations::JoinDependency.
|
8
|
-
new(Person, {
|
9
|
-
:children => {
|
10
|
-
:children => {
|
11
|
-
:parent => :parent
|
12
|
-
}
|
13
|
-
}
|
14
|
-
}, [])
|
15
|
-
@c = JoinDependencyContext.new(@jd)
|
16
|
-
end
|
17
|
-
|
18
|
-
it 'finds associations' do
|
19
|
-
last_association = @jd.join_parts.last
|
20
|
-
next_to_last_association = @jd.join_parts[-2]
|
21
|
-
|
22
|
-
@c.find(:parent, next_to_last_association).should eq last_association
|
23
|
-
end
|
24
|
-
|
25
|
-
it 'contextualizes join parts with the proper alias' do
|
26
|
-
table = @c.contextualize @jd.join_parts.last
|
27
|
-
table.table_alias.should eq 'parents_people_2'
|
28
|
-
end
|
29
|
-
|
30
|
-
it 'contextualizes symbols as a generic table' do
|
31
|
-
table = @c.contextualize :table
|
32
|
-
table.name.should eq 'table'
|
33
|
-
table.table_alias.should be_nil
|
34
|
-
end
|
35
|
-
|
36
|
-
it 'contextualizes non-JoinPart/Symbols to the default join base table' do
|
37
|
-
table = @c.contextualize nil
|
38
|
-
table.name.should eq 'people'
|
39
|
-
table.table_alias.should be_nil
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
@@ -1,115 +0,0 @@
|
|
1
|
-
module Squeel
|
2
|
-
module Visitors
|
3
|
-
describe SelectVisitor do
|
4
|
-
|
5
|
-
before do
|
6
|
-
@jd = ActiveRecord::Associations::JoinDependency.
|
7
|
-
new(Person, {
|
8
|
-
:children => {
|
9
|
-
:children => {
|
10
|
-
:parent => :parent
|
11
|
-
}
|
12
|
-
}
|
13
|
-
}, [])
|
14
|
-
@c = Squeel::Contexts::JoinDependencyContext.new(@jd)
|
15
|
-
@v = SelectVisitor.new(@c)
|
16
|
-
end
|
17
|
-
|
18
|
-
it 'creates a bare ARel attribute given a symbol with no asc/desc' do
|
19
|
-
attribute = @v.accept(:name)
|
20
|
-
attribute.should be_a Arel::Attribute
|
21
|
-
attribute.name.should eq :name
|
22
|
-
attribute.relation.name.should eq 'people'
|
23
|
-
end
|
24
|
-
|
25
|
-
it 'creates the select against the proper table for nested hashes' do
|
26
|
-
selects = @v.accept({
|
27
|
-
:children => {
|
28
|
-
:children => {
|
29
|
-
:parent => {
|
30
|
-
:parent => :name
|
31
|
-
}
|
32
|
-
}
|
33
|
-
}
|
34
|
-
})
|
35
|
-
selects.should be_a Array
|
36
|
-
select = selects.first
|
37
|
-
select.should be_a Arel::Attribute
|
38
|
-
select.relation.table_alias.should eq 'parents_people_2'
|
39
|
-
end
|
40
|
-
|
41
|
-
it 'will not alter values it is unable to accept' do
|
42
|
-
select = @v.accept(['THIS PARAMETER', 'WHAT DOES IT MEAN???'])
|
43
|
-
select.should eq ['THIS PARAMETER', 'WHAT DOES IT MEAN???']
|
44
|
-
end
|
45
|
-
|
46
|
-
it 'treats keypath keys like nested hashes' do
|
47
|
-
select = @v.accept(Nodes::Stub.new(:children).children.parent.parent.name)
|
48
|
-
select.should be_a Arel::Attribute
|
49
|
-
select.relation.table_alias.should eq 'parents_people_2'
|
50
|
-
end
|
51
|
-
|
52
|
-
it 'honors absolute keypaths' do
|
53
|
-
selects = @v.accept(dsl{{children => {children => ~children.children.name}}})
|
54
|
-
selects.should be_a Array
|
55
|
-
select = selects.first
|
56
|
-
select.should be_a Arel::Attribute
|
57
|
-
select.relation.table_alias.should eq 'children_people_2'
|
58
|
-
end
|
59
|
-
|
60
|
-
it 'allows hashes with keypath keys' do
|
61
|
-
selects = @v.accept(Nodes::Stub.new(:children).children.parent.parent => :name)
|
62
|
-
selects.should be_a Array
|
63
|
-
select = selects.first
|
64
|
-
select.should be_a Arel::Attribute
|
65
|
-
select.relation.table_alias.should eq 'parents_people_2'
|
66
|
-
end
|
67
|
-
|
68
|
-
it 'creates an ARel NamedFunction node for a Function node' do
|
69
|
-
function = @v.accept(:find_in_set.func())
|
70
|
-
function.should be_a Arel::Nodes::NamedFunction
|
71
|
-
end
|
72
|
-
|
73
|
-
it 'maps symbols in Function args to ARel attributes' do
|
74
|
-
function = @v.accept(:find_in_set.func(:id, '1,2,3'))
|
75
|
-
function.to_sql.should match /find_in_set\("people"."id", '1,2,3'\)/
|
76
|
-
end
|
77
|
-
|
78
|
-
it 'accepts keypaths as function args' do
|
79
|
-
function = @v.accept(dsl{find_in_set(children.children.id, '1,2,3')})
|
80
|
-
function.to_sql.should match /find_in_set\("children_people_2"."id", '1,2,3'\)/
|
81
|
-
end
|
82
|
-
|
83
|
-
it 'sets the alias on the ARel NamedFunction from the Function alias' do
|
84
|
-
function = @v.accept(:find_in_set.func(:id, '1,2,3').as('newname'))
|
85
|
-
function.to_sql.should match /newname/
|
86
|
-
end
|
87
|
-
|
88
|
-
it 'creates an ARel Addition node for an Operation node with + as operator' do
|
89
|
-
operation = @v.accept(dsl{id + 1})
|
90
|
-
operation.should be_a Arel::Nodes::Addition
|
91
|
-
end
|
92
|
-
|
93
|
-
it 'creates an ARel Subtraction node for an Operation node with - as operator' do
|
94
|
-
operation = @v.accept(dsl{id - 1})
|
95
|
-
operation.should be_a Arel::Nodes::Subtraction
|
96
|
-
end
|
97
|
-
|
98
|
-
it 'creates an ARel Multiplication node for an Operation node with * as operator' do
|
99
|
-
operation = @v.accept(dsl{id * 1})
|
100
|
-
operation.should be_a Arel::Nodes::Multiplication
|
101
|
-
end
|
102
|
-
|
103
|
-
it 'creates an ARel Division node for an Operation node with / as operator' do
|
104
|
-
operation = @v.accept(dsl{id / 1})
|
105
|
-
operation.should be_a Arel::Nodes::Division
|
106
|
-
end
|
107
|
-
|
108
|
-
it 'sets the alias on an InfixOperation from the Operation alias' do
|
109
|
-
operation = @v.accept(dsl{(id + 1).as(:incremented_id)})
|
110
|
-
operation.to_sql.should match /incremented_id/
|
111
|
-
end
|
112
|
-
|
113
|
-
end
|
114
|
-
end
|
115
|
-
end
|