ambition 0.1.1 → 0.1.2
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 +5 -1
- data/Rakefile +1 -1
- data/lib/ambition/order.rb +5 -4
- data/lib/ambition/processor.rb +14 -1
- data/lib/ambition/where.rb +1 -17
- data/test/join_test.rb +16 -0
- metadata +1 -1
data/README
CHANGED
@@ -158,6 +158,10 @@ still query through ActiveRecord just fine.
|
|
158
158
|
User.select { |m| m.name == 'jon' }.sort_by { |m| -m.age }
|
159
159
|
"SELECT * FROM users WHERE users.`name` = 'jon' ORDER BY users.age DESC"
|
160
160
|
|
161
|
+
User.select { |m| m.name == 'jon' }.sort_by { |m| -m.profiles.title }
|
162
|
+
"SELECT users.`id` AS t0_r0, ... FROM users LEFT OUTER JOIN profiles ON profiles.user_id = users.id
|
163
|
+
WHERE (users.`name` = 'jon') ORDER BY profiles.title DESC"
|
164
|
+
|
161
165
|
User.select { |m| m.name == 'jon' }.sort_by { rand }
|
162
166
|
"SELECT * FROM users WHERE users.`name` = 'jon' ORDER BY RAND()"
|
163
167
|
|
@@ -175,4 +179,4 @@ Found a bug? Sweet. Add it at the Lighthouse: http://err.lighthouseapp.com/pro
|
|
175
179
|
|
176
180
|
Feature requests are welcome.
|
177
181
|
|
178
|
-
* Chris Wanstrath [ chris@ozmm.org ]
|
182
|
+
* Chris Wanstrath [ chris@ozmm.org ]
|
data/Rakefile
CHANGED
@@ -8,7 +8,7 @@ begin
|
|
8
8
|
ENV['RUBY_FLAGS'] = ""
|
9
9
|
require 'echoe'
|
10
10
|
|
11
|
-
Echoe.new('ambition', '0.1.
|
11
|
+
Echoe.new('ambition', '0.1.2') do |p|
|
12
12
|
p.rubyforge_name = 'err'
|
13
13
|
p.summary = "Ambition builds SQL from plain jane Ruby."
|
14
14
|
p.description = "Ambition builds SQL from plain jane Ruby."
|
data/lib/ambition/order.rb
CHANGED
@@ -1,15 +1,16 @@
|
|
1
1
|
module Ambition
|
2
2
|
module Order
|
3
3
|
def sort_by(&block)
|
4
|
-
query_context.add OrderProcessor.new(
|
4
|
+
query_context.add OrderProcessor.new(self, block)
|
5
5
|
end
|
6
6
|
end
|
7
7
|
|
8
8
|
class OrderProcessor < Processor
|
9
|
-
def initialize(
|
9
|
+
def initialize(owner, block)
|
10
10
|
super()
|
11
11
|
@receiver = nil
|
12
|
-
@
|
12
|
+
@owner = owner
|
13
|
+
@table_name = owner.table_name
|
13
14
|
@block = block
|
14
15
|
@key = :order
|
15
16
|
end
|
@@ -45,7 +46,7 @@ module Ambition
|
|
45
46
|
when :__send__
|
46
47
|
"#{@table_name}.#{eval('to_s', @block)}"
|
47
48
|
else
|
48
|
-
"#{@table_name}.#{method}"
|
49
|
+
extract_includes(receiver, method) || "#{@table_name}.#{method}"
|
49
50
|
end
|
50
51
|
end
|
51
52
|
end
|
data/lib/ambition/processor.rb
CHANGED
@@ -4,7 +4,8 @@ module Ambition
|
|
4
4
|
class Processor < SexpProcessor
|
5
5
|
include ActiveRecord::ConnectionAdapters::Quoting
|
6
6
|
|
7
|
-
attr_reader :key, :join_string, :prefix
|
7
|
+
attr_reader :key, :join_string, :prefix, :includes
|
8
|
+
|
8
9
|
|
9
10
|
def initialize
|
10
11
|
super()
|
@@ -13,6 +14,7 @@ module Ambition
|
|
13
14
|
@auto_shift_type = true
|
14
15
|
@warn_on_default = false
|
15
16
|
@default_method = :process_error
|
17
|
+
@includes = []
|
16
18
|
end
|
17
19
|
|
18
20
|
##
|
@@ -50,5 +52,16 @@ module Ambition
|
|
50
52
|
else ActiveRecord::Base.connection.quote(value) rescue quote(value)
|
51
53
|
end
|
52
54
|
end
|
55
|
+
|
56
|
+
def extract_includes(receiver, method)
|
57
|
+
return unless receiver.first == :call && receiver[1].last == @receiver
|
58
|
+
|
59
|
+
if reflection = @owner.reflections[receiver.last]
|
60
|
+
@includes << reflection.name unless @includes.include? reflection.name
|
61
|
+
"#{reflection.table_name}.#{method}"
|
62
|
+
else
|
63
|
+
raise "No reflection `#{receiver.last}' found on #{@owner}"
|
64
|
+
end
|
65
|
+
end
|
53
66
|
end
|
54
67
|
end
|
data/lib/ambition/where.rb
CHANGED
@@ -10,8 +10,6 @@ module Ambition
|
|
10
10
|
end
|
11
11
|
|
12
12
|
class WhereProcessor < Processor
|
13
|
-
attr_reader :includes
|
14
|
-
|
15
13
|
def initialize(owner, block)
|
16
14
|
super()
|
17
15
|
@receiver = nil
|
@@ -19,7 +17,6 @@ module Ambition
|
|
19
17
|
@table_name = owner.table_name
|
20
18
|
@block = block
|
21
19
|
@key = :conditions
|
22
|
-
@includes = []
|
23
20
|
end
|
24
21
|
|
25
22
|
##
|
@@ -138,20 +135,7 @@ module Ambition
|
|
138
135
|
when '!~'
|
139
136
|
"#{process(receiver)} NOT LIKE #{process(other)}"
|
140
137
|
else
|
141
|
-
|
142
|
-
end
|
143
|
-
end
|
144
|
-
|
145
|
-
def build_condition(receiver, method, other)
|
146
|
-
if receiver.first == :call && receiver[1].last == @receiver
|
147
|
-
if reflection = @owner.reflections[receiver.last]
|
148
|
-
@includes << reflection.name unless @includes.include? reflection.name
|
149
|
-
"#{reflection.table_name}.#{method}"
|
150
|
-
else
|
151
|
-
raise "No reflection `#{receiver.last}' found on #{@owner}"
|
152
|
-
end
|
153
|
-
else
|
154
|
-
"#{process(receiver)}.`#{method}` #{process(other)}"
|
138
|
+
extract_includes(receiver, method) || "#{process(receiver)}.`#{method}` #{process(other)}"
|
155
139
|
end
|
156
140
|
end
|
157
141
|
end
|
data/test/join_test.rb
CHANGED
@@ -29,4 +29,20 @@ context "Joins" do
|
|
29
29
|
sql = User.select { |m| m.liquor.brand == 'Jack' }
|
30
30
|
should.raise { sql.to_hash }
|
31
31
|
end
|
32
|
+
|
33
|
+
specify "in order" do
|
34
|
+
sql = User.sort_by { |m| m.ideas.title }
|
35
|
+
sql.to_hash.should == {
|
36
|
+
:order => "ideas.title",
|
37
|
+
:include => [:ideas]
|
38
|
+
}
|
39
|
+
end
|
40
|
+
|
41
|
+
specify "in a more complex order" do
|
42
|
+
sql = User.sort_by { |m| [ m.ideas.title, -m.invites.email ] }
|
43
|
+
sql.to_hash.should == {
|
44
|
+
:order => "ideas.title, invites.email DESC",
|
45
|
+
:include => [:ideas, :invites]
|
46
|
+
}
|
47
|
+
end
|
32
48
|
end
|