fake_arel 0.8.1 → 0.9
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +16 -0
- data/fake_arel.gemspec +1 -1
- data/lib/fake_arel.rb +1 -1
- data/lib/fake_arel/rails_3_finders.rb +25 -0
- data/spec/fake_arel_spec.rb +18 -10
- data/spec/fixtures/reply.rb +1 -1
- data/spec/test.db +0 -0
- metadata +7 -8
data/README.rdoc
CHANGED
@@ -35,6 +35,22 @@ http://github.com/gammons/fake_arel
|
|
35
35
|
named_scope :recent_by_john, recent(15).by_john
|
36
36
|
end
|
37
37
|
|
38
|
+
=== Recently Added!
|
39
|
+
|
40
|
+
* <tt>or</tt> syntax. Because named scopes load lazily, we're able to pass the scope to another scope, in this case, <tr>or</tr>.
|
41
|
+
|
42
|
+
q1 = Reply.where(:id => 1)
|
43
|
+
q2 = Reply.where(:id => 2)
|
44
|
+
q3 = Reply.where(:id => 3)
|
45
|
+
q4 = Reply.where(:id => 4)
|
46
|
+
|
47
|
+
Reply.or(q1,q2).all.map(&:id) # equals [1,2]
|
48
|
+
Reply.or(q1,q2,q3).all.map(&:id) # equals [1,2,3]
|
49
|
+
|
50
|
+
or1 = Reply.or(q1,q2)
|
51
|
+
or2 = Reply.or(q3,q4)
|
52
|
+
Reply.or(or1,or2).all.map(&:id) # equals [1,2,3,4]
|
53
|
+
|
38
54
|
|
39
55
|
== REQUIREMENTS:
|
40
56
|
|
data/fake_arel.gemspec
CHANGED
data/lib/fake_arel.rb
CHANGED
@@ -25,6 +25,31 @@ module Rails3Finders
|
|
25
25
|
end
|
26
26
|
|
27
27
|
named_scope :where, __where_fn
|
28
|
+
|
29
|
+
__or_fn = lambda do |*scopes|
|
30
|
+
where = []
|
31
|
+
joins = []
|
32
|
+
includes = []
|
33
|
+
|
34
|
+
# for some reason, flatten is actually executing the scope
|
35
|
+
scopes = scopes[0] if scopes.size == 1
|
36
|
+
scopes.each do |s|
|
37
|
+
s = s.proxy_options
|
38
|
+
begin
|
39
|
+
where << merge_conditions(s[:conditions])
|
40
|
+
rescue NoMethodError
|
41
|
+
where << scopes[0].first.class.merge_conditions(s[:conditions])
|
42
|
+
end
|
43
|
+
#where << merge_conditions(s[:conditions])
|
44
|
+
joins << s[:joins] unless s[:joins].nil?
|
45
|
+
includes << s[:include] unless s[:include].nil?
|
46
|
+
end
|
47
|
+
scoped = self
|
48
|
+
scoped = scoped.includes(includes.uniq.flatten) unless includes.blank?
|
49
|
+
scoped = scoped.joins(joins.uniq.flatten) unless joins.blank?
|
50
|
+
scoped.where(where.join(" OR "))
|
51
|
+
end
|
52
|
+
named_scope :or, __or_fn
|
28
53
|
end
|
29
54
|
end
|
30
55
|
end
|
data/spec/fake_arel_spec.rb
CHANGED
@@ -3,7 +3,7 @@ require 'reply'
|
|
3
3
|
require 'topic'
|
4
4
|
require 'author'
|
5
5
|
|
6
|
-
describe "
|
6
|
+
describe "Fake Arel" do
|
7
7
|
it "should accomplish basic where" do
|
8
8
|
Reply.where(:id => 1).first.id.should == 1
|
9
9
|
Reply.where("id = 1").first.id.should == 1
|
@@ -36,15 +36,11 @@ describe "Basics" do
|
|
36
36
|
it "should work with scope and with exclusive scope" do
|
37
37
|
Reply.find_all_but_first.map(&:id).should == [2,3,4,5,6]
|
38
38
|
end
|
39
|
-
end
|
40
39
|
|
41
|
-
describe "to sql" do
|
42
40
|
it "should be able to output sql" do
|
43
41
|
Topic.joins(:replies).limit(1).to_sql
|
44
42
|
end
|
45
|
-
end
|
46
43
|
|
47
|
-
describe "chained nested named scopes" do
|
48
44
|
it "should be able to chain named scopes within a named_scope" do
|
49
45
|
Reply.recent_with_content_like_ar.should == Reply.find(:all, :conditions => "id = 5")
|
50
46
|
Reply.recent_with_content_like_ar_and_id_4.should == []
|
@@ -123,16 +119,11 @@ describe "chained nested named scopes" do
|
|
123
119
|
topic.replies.loaded?.should be_true
|
124
120
|
}
|
125
121
|
end
|
126
|
-
|
127
|
-
end
|
128
122
|
|
129
|
-
describe "keep scoped functionality" do
|
130
123
|
it "should respond to scoped" do
|
131
124
|
Reply.scoped({}).class.should == ActiveRecord::NamedScope::Scope
|
132
125
|
end
|
133
|
-
end
|
134
126
|
|
135
|
-
describe "not generate redundant queries" do
|
136
127
|
# Github issue #8, fake_arel was adding conditions over and over
|
137
128
|
# to a names scope.
|
138
129
|
it "should not add a billion parens and conditions" do
|
@@ -154,5 +145,22 @@ describe "not generate redundant queries" do
|
|
154
145
|
pass_1.should == pass_2
|
155
146
|
pass_2.should == pass_3
|
156
147
|
end
|
148
|
+
|
149
|
+
it "should be able to combine named scopes with or" do
|
150
|
+
q1 = Reply.where(:id => 1)
|
151
|
+
q2 = Reply.where(:id => 2)
|
152
|
+
q3 = Reply.where(:id => 3)
|
153
|
+
q4 = Reply.where(:id => 4)
|
154
|
+
Reply.or(q1,q2).all.map(&:id).should == [1,2]
|
155
|
+
Reply.or(q1,q2,q3).all.map(&:id).should == [1,2,3]
|
156
|
+
|
157
|
+
# here's something crazy
|
158
|
+
or1 = Reply.or(q1,q2)
|
159
|
+
or2 = Reply.or(q3,q4)
|
160
|
+
Reply.or(or1,or2).all.map(&:id).should == [1,2,3,4]
|
161
|
+
|
162
|
+
# an example using joins, as well as a query that returns nothing
|
163
|
+
Reply.or(Reply.recent_joins_topic, Reply.topic_title_is("Nothin")).all.map(&:id).should == [5]
|
164
|
+
end
|
157
165
|
end
|
158
166
|
|
data/spec/fixtures/reply.rb
CHANGED
@@ -7,7 +7,7 @@ class Reply < ActiveRecord::Base
|
|
7
7
|
named_scope :recent_with_content_like_ar, recent.where('lower(replies.content) like ?', "AR%")
|
8
8
|
named_scope :recent_with_content_like_ar_and_id_4, recent.where('lower(replies.content) like ?', "AR%").where("id = 4")
|
9
9
|
named_scope :recent_joins_topic, recent.joins(:topic)
|
10
|
-
named_scope :topic_title_is, lambda {|topic_title| where("topics.title like ?", topic_title + "%") }
|
10
|
+
named_scope :topic_title_is, lambda {|topic_title| joins(:topic).where("topics.title like ?", topic_title + "%") }
|
11
11
|
|
12
12
|
named_scope :join_topic_and_author, joins(:topic => [:author])
|
13
13
|
named_scope :filter_join_topic_and_author, joins(:topic => [:author]).where('lower(replies.content) like ?','AR%')
|
data/spec/test.db
CHANGED
Binary file
|
metadata
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fake_arel
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
|
10
|
-
version: 0.8.1
|
8
|
+
- 9
|
9
|
+
version: "0.9"
|
11
10
|
platform: ruby
|
12
11
|
authors:
|
13
12
|
- Grant Ammons
|
@@ -15,7 +14,7 @@ autorequire:
|
|
15
14
|
bindir: bin
|
16
15
|
cert_chain: []
|
17
16
|
|
18
|
-
date: 2010-
|
17
|
+
date: 2010-10-26 00:00:00 -04:00
|
19
18
|
default_executable:
|
20
19
|
dependencies:
|
21
20
|
- !ruby/object:Gem::Dependency
|
@@ -58,12 +57,12 @@ dependencies:
|
|
58
57
|
requirements:
|
59
58
|
- - ">="
|
60
59
|
- !ruby/object:Gem::Version
|
61
|
-
hash:
|
60
|
+
hash: 19
|
62
61
|
segments:
|
63
62
|
- 2
|
64
63
|
- 6
|
65
|
-
-
|
66
|
-
version: 2.6.
|
64
|
+
- 2
|
65
|
+
version: 2.6.2
|
67
66
|
type: :development
|
68
67
|
version_requirements: *id003
|
69
68
|
description: |-
|