squeel 0.8.7 → 0.8.8
Sign up to get free protection for your applications and to get access to all the features.
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Squeel [![Build Status](
|
1
|
+
# Squeel [![Build Status](https://secure.travis-ci.org/ernie/squeel.png)](http://travis-ci.org/ernie/squeel)
|
2
2
|
|
3
3
|
Squeel lets you write your ActiveRecord queries with with fewer strings, and more Ruby,
|
4
4
|
by making the ARel awesomeness that lies beneath ActiveRecord more accessible.
|
@@ -11,7 +11,7 @@ module Squeel
|
|
11
11
|
super
|
12
12
|
@base = object.join_base
|
13
13
|
@engine = @base.arel_engine
|
14
|
-
@arel_visitor =
|
14
|
+
@arel_visitor = @engine.connection.visitor
|
15
15
|
@default_table = Arel::Table.new(@base.table_name, :as => @base.aliased_table_name, :engine => @engine)
|
16
16
|
end
|
17
17
|
|
data/lib/squeel/version.rb
CHANGED
@@ -92,8 +92,14 @@ module Squeel
|
|
92
92
|
def visit_Squeel_Nodes_Predicate(o, parent)
|
93
93
|
value = o.value
|
94
94
|
|
95
|
-
|
95
|
+
case value
|
96
|
+
when Nodes::KeyPath
|
96
97
|
value = can_visit?(value.endpoint) ? visit(value, parent) : contextualize(traverse(value, parent))[value.endpoint.to_sym]
|
98
|
+
when ActiveRecord::Relation
|
99
|
+
value = visit(
|
100
|
+
value.select_values.empty? ? value.select(value.klass.arel_table[value.klass.primary_key]) : value,
|
101
|
+
parent
|
102
|
+
)
|
97
103
|
else
|
98
104
|
value = visit(value, parent) if can_visit?(value)
|
99
105
|
end
|
@@ -295,7 +301,12 @@ module Squeel
|
|
295
301
|
# @param value The value to be compared against
|
296
302
|
# @return [Arel::Nodes::Node] An ARel predicate node
|
297
303
|
def arel_predicate_for(attribute, value, parent)
|
298
|
-
|
304
|
+
if ActiveRecord::Relation === value && value.select_values.empty?
|
305
|
+
value = visit(value.select(value.klass.arel_table[value.klass.primary_key]), parent)
|
306
|
+
else
|
307
|
+
value = can_visit?(value) ? visit(value, parent) : value
|
308
|
+
end
|
309
|
+
|
299
310
|
case value
|
300
311
|
when Array
|
301
312
|
attribute_in_array(attribute, value)
|
@@ -99,6 +99,30 @@ module Squeel
|
|
99
99
|
predicate.right.should be_a Arel::Nodes::SelectStatement
|
100
100
|
end
|
101
101
|
|
102
|
+
it 'selects the primary key of a relation with no select_values with an explicit predicate' do
|
103
|
+
predicate = @v.accept dsl{name.in(PersonWithNamePrimaryKey.where{name.in(['Aric Smith', 'Gladyce Kulas'])})}
|
104
|
+
predicate.right.should be_a Arel::Nodes::SelectStatement
|
105
|
+
predicate.right.to_sql.should match /SELECT "people"."name"/
|
106
|
+
end
|
107
|
+
|
108
|
+
it 'selects the primary key of a relation with no select_values with an implicit predicate' do
|
109
|
+
predicate = @v.accept(:name => PersonWithNamePrimaryKey.where{name.in(['Aric Smith', 'Gladyce Kulas'])})
|
110
|
+
predicate.right.should be_a Arel::Nodes::SelectStatement
|
111
|
+
predicate.right.to_sql.should match /SELECT "people"."name"/
|
112
|
+
end
|
113
|
+
|
114
|
+
it "doesn't clobber a relation value's existing select_values if present with an explicit predicate" do
|
115
|
+
predicate = @v.accept dsl{name.in(Person.select{name})}
|
116
|
+
predicate.right.should be_a Arel::Nodes::SelectStatement
|
117
|
+
predicate.right.to_sql.should match /SELECT "people"."name"/
|
118
|
+
end
|
119
|
+
|
120
|
+
it "doesn't clobber a relation value's existing select_values if present with an implicit predicate" do
|
121
|
+
predicate = @v.accept(:name => Person.select{name})
|
122
|
+
predicate.right.should be_a Arel::Nodes::SelectStatement
|
123
|
+
predicate.right.to_sql.should match /SELECT "people"."name"/
|
124
|
+
end
|
125
|
+
|
102
126
|
it 'converts ActiveRecord::Base objects to their id' do
|
103
127
|
predicate = @v.accept(:id => Person.first)
|
104
128
|
predicate.should be_a Arel::Nodes::Equality
|
data/spec/support/schema.rb
CHANGED
@@ -19,7 +19,13 @@ class Person < ActiveRecord::Base
|
|
19
19
|
|
20
20
|
has_many :outgoing_messages, :class_name => 'Message', :foreign_key => :author_id
|
21
21
|
has_many :incoming_messages, :class_name => 'Message', :foreign_key => :recipient_id
|
22
|
+
end
|
22
23
|
|
24
|
+
class PersonWithNamePrimaryKey < ActiveRecord::Base
|
25
|
+
set_primary_key 'name'
|
26
|
+
# Set this second, because I'm lazy and don't want to populate another table,
|
27
|
+
# and also don't want to clobber the AR connection's primary_key cache.
|
28
|
+
set_table_name 'people'
|
23
29
|
end
|
24
30
|
|
25
31
|
class Message < ActiveRecord::Base
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: squeel
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.8.
|
4
|
+
version: 0.8.8
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-08-
|
12
|
+
date: 2011-08-23 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
16
|
-
requirement: &
|
16
|
+
requirement: &70108702043340 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '3.0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70108702043340
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: activesupport
|
27
|
-
requirement: &
|
27
|
+
requirement: &70108702042780 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '3.0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70108702042780
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rspec
|
38
|
-
requirement: &
|
38
|
+
requirement: &70108702042220 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: 2.6.0
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70108702042220
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: machinist
|
49
|
-
requirement: &
|
49
|
+
requirement: &70108702041580 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ~>
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: 1.0.6
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70108702041580
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: faker
|
60
|
-
requirement: &
|
60
|
+
requirement: &70108702040920 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ~>
|
@@ -65,10 +65,10 @@ dependencies:
|
|
65
65
|
version: 0.9.5
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *70108702040920
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: sqlite3
|
71
|
-
requirement: &
|
71
|
+
requirement: &70108702040300 !ruby/object:Gem::Requirement
|
72
72
|
none: false
|
73
73
|
requirements:
|
74
74
|
- - ~>
|
@@ -76,7 +76,7 @@ dependencies:
|
|
76
76
|
version: 1.3.3
|
77
77
|
type: :development
|
78
78
|
prerelease: false
|
79
|
-
version_requirements: *
|
79
|
+
version_requirements: *70108702040300
|
80
80
|
description: ! "\n Squeel unlocks the power of ARel in your Rails 3 application
|
81
81
|
with\n a handy block-based syntax. You can write subqueries, access named\n
|
82
82
|
\ functions provided by your RDBMS, and more, all without writing\n SQL
|