fake_arel 0.9.9 → 1.0.0.a
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/Gemfile +10 -7
- data/Gemfile.lock +11 -27
- data/LICENSE.txt +20 -0
- data/README.md +121 -0
- data/Rakefile +54 -24
- data/VERSION +1 -0
- data/fake_arel.gemspec +81 -18
- data/lib/fake_arel/extensions.rb +9 -1
- data/lib/fake_arel/selectable_includes.rb +40 -0
- data/lib/fake_arel.rb +1 -1
- data/spec/fake_arel_spec.rb +17 -5
- data/spec/fixtures/reply.rb +1 -1
- data/spec/fixtures/topic.rb +4 -3
- data/spec/spec_helper.rb +0 -3
- metadata +101 -44
- data/.gemtest +0 -0
- data/README.rdoc +0 -91
- data/spec/test.db +0 -0
data/Gemfile
CHANGED
@@ -1,13 +1,16 @@
|
|
1
|
-
# A sample Gemfile
|
2
1
|
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
gem 'activerecord', '~> 2.3.11'
|
3
6
|
|
4
|
-
gem
|
5
|
-
|
7
|
+
# Add dependencies to develop your gem here.
|
8
|
+
# Include everything needed to run rake, tests, features, etc.
|
6
9
|
group :development do
|
10
|
+
gem "bundler", "~> 1.0.0"
|
11
|
+
gem "jeweler", "~> 1.6.4"
|
12
|
+
gem "rcov", ">= 0"
|
13
|
+
|
7
14
|
gem 'rspec', '1.3.1'
|
8
15
|
gem "sqlite3-ruby"
|
9
|
-
gem 'newgem'
|
10
|
-
gem 'rake'
|
11
|
-
gem 'hoe'
|
12
|
-
gem 'ruby-debug'
|
13
16
|
end
|
data/Gemfile.lock
CHANGED
@@ -1,44 +1,28 @@
|
|
1
1
|
GEM
|
2
2
|
remote: http://rubygems.org/
|
3
3
|
specs:
|
4
|
-
RedCloth (4.2.7)
|
5
4
|
activerecord (2.3.11)
|
6
5
|
activesupport (= 2.3.11)
|
7
6
|
activesupport (2.3.11)
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
activesupport (~> 2.3.4)
|
16
|
-
hoe (>= 2.4.0)
|
17
|
-
rubigen (>= 1.5.3)
|
18
|
-
syntax (>= 1.0.0)
|
19
|
-
rake (0.8.7)
|
7
|
+
git (1.2.5)
|
8
|
+
jeweler (1.6.4)
|
9
|
+
bundler (~> 1.0)
|
10
|
+
git (>= 1.2.5)
|
11
|
+
rake
|
12
|
+
rake (0.9.2)
|
13
|
+
rcov (0.9.9)
|
20
14
|
rspec (1.3.1)
|
21
|
-
rubigen (1.5.6)
|
22
|
-
activesupport (>= 2.3.5)
|
23
|
-
i18n
|
24
|
-
ruby-debug (0.10.4)
|
25
|
-
columnize (>= 0.1)
|
26
|
-
ruby-debug-base (~> 0.10.4.0)
|
27
|
-
ruby-debug-base (0.10.4)
|
28
|
-
linecache (>= 0.3)
|
29
15
|
sqlite3 (1.3.3)
|
30
16
|
sqlite3-ruby (1.3.3)
|
31
17
|
sqlite3 (>= 1.3.3)
|
32
|
-
syntax (1.0.0)
|
33
18
|
|
34
19
|
PLATFORMS
|
35
20
|
ruby
|
36
21
|
|
37
22
|
DEPENDENCIES
|
38
|
-
activerecord (
|
39
|
-
|
40
|
-
|
41
|
-
|
23
|
+
activerecord (~> 2.3.11)
|
24
|
+
bundler (~> 1.0.0)
|
25
|
+
jeweler (~> 1.6.4)
|
26
|
+
rcov
|
42
27
|
rspec (= 1.3.1)
|
43
|
-
ruby-debug
|
44
28
|
sqlite3-ruby
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Grant Ammons
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
## Fake_arel: Rails 3 Query Interface for Rails 2
|
2
|
+
|
3
|
+
http://github.com/gammons/fake_arel
|
4
|
+
|
5
|
+
## DESCRIPTION:
|
6
|
+
|
7
|
+
* Tired of waiting for Rails 3 and its new super-sweet query interface? Try fake_arel!
|
8
|
+
|
9
|
+
* Fake_arel is a gem that will simulate the new Rails 3 query interface, using named_scopes and a couple small patches to ActiveRecord.
|
10
|
+
|
11
|
+
* This should serve as a nice bridge between Rails 2 and Rails 3 apps, and can be removed once upgrading your app to rails 3, and everything (hopefully) should still work.
|
12
|
+
|
13
|
+
## SYNOPSIS:
|
14
|
+
|
15
|
+
* All the finders described on [Pratiks blog](http://m.onkey.org/2010/1/22/active-record-query-interface) have been implemented.
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
Reply.where(:id => 1)
|
19
|
+
Reply.select("content,id").where("id > 1").order("id desc").limit(1)
|
20
|
+
Topic.joins(:replies).limit(1)
|
21
|
+
```
|
22
|
+
|
23
|
+
* Additionally, named_scopes are very similar to Rails 3 relations, in that they are lazily loaded.
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
Reply.where(:name => "John").class
|
27
|
+
ActiveRecord::NamedScope::Scope
|
28
|
+
```
|
29
|
+
* Also implemented was `to_sql`. `to_sql` will work on any chained query.
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
Topic.joins(:replies).limit(1).to_sql
|
33
|
+
"SELECT \"topics\".* FROM \"topics\" INNER JOIN \"replies\" ON replies.topic_id = topics.id LIMIT 1"
|
34
|
+
```
|
35
|
+
|
36
|
+
* `named_scope` was modified to include other `named_scope`s, so you can chain them together.
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
class Reply < ActiveRecord::Base
|
40
|
+
named_scope :by_john, where(:name => "John")
|
41
|
+
named_scope :recent, lambda {|t| where("created_at > ? ", t.minutes.ago) }
|
42
|
+
named_scope :recent_by_john, recent(15).by_john
|
43
|
+
end
|
44
|
+
```
|
45
|
+
|
46
|
+
## Fakearel-specific items
|
47
|
+
The following items are not necessarily implemented in Rails 3 arel, but they are pretty awesome.
|
48
|
+
|
49
|
+
### OR syntax
|
50
|
+
Because named scopes load lazily, we are able to pass the scope to another scope, in this case, `or`.
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
q1 = Reply.where(:id => 1)
|
54
|
+
q2 = Reply.where(:id => 2)
|
55
|
+
q3 = Reply.where(:id => 3)
|
56
|
+
q4 = Reply.where(:id => 4)
|
57
|
+
|
58
|
+
Reply.or(q1,q2).all.map(&:id) # equals [1,2]
|
59
|
+
Reply.or(q1,q2,q3).all.map(&:id) # equals [1,2,3]
|
60
|
+
|
61
|
+
or1 = Reply.or(q1,q2)
|
62
|
+
or2 = Reply.or(q3,q4)
|
63
|
+
Reply.or(or1,or2).all.map(&:id) # equals [1,2,3,4]
|
64
|
+
```
|
65
|
+
|
66
|
+
### fakearel_find_each
|
67
|
+
|
68
|
+
The `find_each` that ships with ActiveRecord 2.x isn't very scope-friendly, thus using fakearel_find_each makes sense. However I did not want to replace the original find_each functionality, just in case you were using it.
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
Reply.where(:user_id => 1).fakearel_find_each do |reply|
|
72
|
+
...
|
73
|
+
end
|
74
|
+
```
|
75
|
+
|
76
|
+
### fakearel_destroy
|
77
|
+
|
78
|
+
Call destroy on a scoped call. This will run any callbacks on the models to be destroyed.
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
Reply.where(:user_id => 1).fakearel_destroy #will run before_destroy and after_destroy callbacks for affected Replys
|
82
|
+
```
|
83
|
+
|
84
|
+
## REQUIREMENTS:
|
85
|
+
|
86
|
+
* >= ActiveRecord 2.3.5
|
87
|
+
|
88
|
+
## INSTALL:
|
89
|
+
|
90
|
+
`gem install fake_arel`
|
91
|
+
|
92
|
+
## AUTHORS:
|
93
|
+
|
94
|
+
* Grant Ammons
|
95
|
+
* Sokolov Yura
|
96
|
+
|
97
|
+
## LICENSE:
|
98
|
+
|
99
|
+
(The MIT License)
|
100
|
+
|
101
|
+
Copyright (c) 2010 Grant Ammons (grant@pipelinedealsco.com)
|
102
|
+
|
103
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
104
|
+
a copy of this software and associated documentation files (the
|
105
|
+
'Software'), to deal in the Software without restriction, including
|
106
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
107
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
108
|
+
permit persons to whom the Software is furnished to do so, subject to
|
109
|
+
the following conditions:
|
110
|
+
|
111
|
+
The above copyright notice and this permission notice shall be
|
112
|
+
included in all copies or substantial portions of the Software.
|
113
|
+
|
114
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
115
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
116
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
117
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
118
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
119
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
120
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
121
|
+
|
data/Rakefile
CHANGED
@@ -1,24 +1,54 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require '
|
4
|
-
require '
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "fake_arel"
|
18
|
+
gem.homepage = "http://github.com/gammons/fake_arel"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{fake_arel: a Rails 3 query interface to Rails 2}
|
21
|
+
gem.description = %Q{fake_arel will simulate rails 3 arel syntax for Rails 2.}
|
22
|
+
gem.email = "grant@pipelinedealsco.com"
|
23
|
+
gem.authors = ["Grant Ammons"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rake/testtask'
|
29
|
+
Rake::TestTask.new(:test) do |test|
|
30
|
+
test.libs << 'lib' << 'test'
|
31
|
+
test.pattern = 'test/**/test_*.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
|
35
|
+
require 'rcov/rcovtask'
|
36
|
+
Rcov::RcovTask.new do |test|
|
37
|
+
test.libs << 'test'
|
38
|
+
test.pattern = 'test/**/test_*.rb'
|
39
|
+
test.verbose = true
|
40
|
+
test.rcov_opts << '--exclude "gems/*"'
|
41
|
+
end
|
42
|
+
|
43
|
+
Dir['tasks/**/*.rake'].each { |t| load t }
|
44
|
+
task :default => :spec
|
45
|
+
|
46
|
+
require 'rake/rdoctask'
|
47
|
+
Rake::RDocTask.new do |rdoc|
|
48
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
49
|
+
|
50
|
+
rdoc.rdoc_dir = 'rdoc'
|
51
|
+
rdoc.title = "fake_arel_2 #{version}"
|
52
|
+
rdoc.rdoc_files.include('README*')
|
53
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
54
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0.a
|
data/fake_arel.gemspec
CHANGED
@@ -1,21 +1,84 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
1
4
|
# -*- encoding: utf-8 -*-
|
2
|
-
|
3
|
-
$:.unshift lib unless $:.include?(lib)
|
4
|
-
|
5
|
-
require 'bundler/version'
|
6
|
-
|
5
|
+
|
7
6
|
Gem::Specification.new do |s|
|
8
|
-
s.name
|
9
|
-
s.version
|
10
|
-
|
11
|
-
s.
|
12
|
-
s.
|
13
|
-
s.
|
14
|
-
s.
|
15
|
-
|
16
|
-
s.
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
s.
|
7
|
+
s.name = %q{fake_arel}
|
8
|
+
s.version = "1.0.0.a"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Grant Ammons"]
|
12
|
+
s.date = %q{2011-10-27}
|
13
|
+
s.description = %q{fake_arel will simulate rails 3 arel syntax for Rails 2.}
|
14
|
+
s.email = %q{grant@pipelinedealsco.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.md"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
"Gemfile",
|
21
|
+
"Gemfile.lock",
|
22
|
+
"History.txt",
|
23
|
+
"LICENSE.txt",
|
24
|
+
"Manifest.txt",
|
25
|
+
"README.md",
|
26
|
+
"Rakefile",
|
27
|
+
"VERSION",
|
28
|
+
"fake_arel.gemspec",
|
29
|
+
"lib/fake_arel.rb",
|
30
|
+
"lib/fake_arel/calculations.rb",
|
31
|
+
"lib/fake_arel/extensions.rb",
|
32
|
+
"lib/fake_arel/rails_3_finders.rb",
|
33
|
+
"lib/fake_arel/selectable_includes.rb",
|
34
|
+
"lib/fake_arel/singleton_class.rb",
|
35
|
+
"lib/fake_arel/with_scope_replacement.rb",
|
36
|
+
"script/console",
|
37
|
+
"script/destroy",
|
38
|
+
"script/generate",
|
39
|
+
"spec/fake_arel_spec.rb",
|
40
|
+
"spec/fixtures/author.rb",
|
41
|
+
"spec/fixtures/authors.yml",
|
42
|
+
"spec/fixtures/replies.yml",
|
43
|
+
"spec/fixtures/reply.rb",
|
44
|
+
"spec/fixtures/schema.rb",
|
45
|
+
"spec/fixtures/topic.rb",
|
46
|
+
"spec/fixtures/topics.yml",
|
47
|
+
"spec/spec.opts",
|
48
|
+
"spec/spec_helper.rb",
|
49
|
+
"tasks/rspec.rake"
|
50
|
+
]
|
51
|
+
s.homepage = %q{http://github.com/gammons/fake_arel}
|
52
|
+
s.licenses = ["MIT"]
|
53
|
+
s.require_paths = ["lib"]
|
54
|
+
s.rubygems_version = %q{1.4.2}
|
55
|
+
s.summary = %q{fake_arel: a Rails 3 query interface to Rails 2}
|
56
|
+
|
57
|
+
if s.respond_to? :specification_version then
|
58
|
+
s.specification_version = 3
|
59
|
+
|
60
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
61
|
+
s.add_runtime_dependency(%q<activerecord>, ["~> 2.3.11"])
|
62
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
63
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
|
64
|
+
s.add_development_dependency(%q<rcov>, [">= 0"])
|
65
|
+
s.add_development_dependency(%q<rspec>, ["= 1.3.1"])
|
66
|
+
s.add_development_dependency(%q<sqlite3-ruby>, [">= 0"])
|
67
|
+
else
|
68
|
+
s.add_dependency(%q<activerecord>, ["~> 2.3.11"])
|
69
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
70
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
71
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
72
|
+
s.add_dependency(%q<rspec>, ["= 1.3.1"])
|
73
|
+
s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
|
74
|
+
end
|
75
|
+
else
|
76
|
+
s.add_dependency(%q<activerecord>, ["~> 2.3.11"])
|
77
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
78
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
79
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
80
|
+
s.add_dependency(%q<rspec>, ["= 1.3.1"])
|
81
|
+
s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
|
82
|
+
end
|
21
83
|
end
|
84
|
+
|
data/lib/fake_arel/extensions.rb
CHANGED
@@ -33,6 +33,7 @@ module ActiveRecord
|
|
33
33
|
local_scope = proxy_scope
|
34
34
|
ret = proxy_options
|
35
35
|
while local_scope.class == ActiveRecord::NamedScope::Scope
|
36
|
+
ret[:select] = local_scope.proxy_options[:select] unless local_scope.proxy_options[:select].nil?
|
36
37
|
local_conditions = merge_conditions(local_scope.proxy_options[:conditions])
|
37
38
|
if local_conditions && ret[:conditions]
|
38
39
|
if !ret[:conditions].index(local_conditions)
|
@@ -42,7 +43,14 @@ module ActiveRecord
|
|
42
43
|
ret[:conditions] = local_conditions
|
43
44
|
end
|
44
45
|
ret[:include] = merge_includes(ret[:include], local_scope.proxy_options[:include])
|
45
|
-
|
46
|
+
if ret[:joins] || local_scope.proxy_options[:joins]
|
47
|
+
# this is a bit ugly, but I was getting error with using OR.
|
48
|
+
begin
|
49
|
+
ret[:joins] = merge_joins(ret[:joins], local_scope.proxy_options[:joins])
|
50
|
+
rescue ActiveRecord::ConfigurationError
|
51
|
+
ret[:joins] = merge_joins((ret[:joins] || []), (local_scope.proxy_options[:joins] || []))
|
52
|
+
end
|
53
|
+
end
|
46
54
|
ret[:order] = [local_scope.proxy_options[:order], ret[:order]].select{|o| !o.blank?}.join(',') if ret[:order] || local_scope.proxy_options[:order]
|
47
55
|
local_scope = local_scope.proxy_scope
|
48
56
|
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# Politely lifted from fcheung's selectable_includes plugin for ActiveRecord
|
2
|
+
# https://github.com/fcheung/selectable_includes/blob/master/lib/selectable_includes.rb
|
3
|
+
class ActiveRecord::Base
|
4
|
+
class << self
|
5
|
+
def construct_finder_sql_with_included_associations_with_selectable_includes(options, join_dependency)
|
6
|
+
scope = scope(:find)
|
7
|
+
select_options = options[:select] || (scope && scope[:select])
|
8
|
+
sql = construct_finder_sql_with_included_associations_without_selectable_includes(options, join_dependency)
|
9
|
+
unless select_options.blank?
|
10
|
+
sql.sub!(/\ASELECT /, "SELECT #{select_options}, ")
|
11
|
+
end
|
12
|
+
sql
|
13
|
+
end
|
14
|
+
alias_method_chain :construct_finder_sql_with_included_associations, :selectable_includes
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class ActiveRecord::Associations::ClassMethods::JoinDependency
|
19
|
+
def instantiate_with_selectable_includes(rows)
|
20
|
+
unless rows.empty?
|
21
|
+
keys_from_select = rows.first.keys.reject {|k| k =~ /\At\d+_r\d+/}
|
22
|
+
join_base.extra_columns = keys_from_select
|
23
|
+
end
|
24
|
+
instantiate_without_selectable_includes(rows)
|
25
|
+
end
|
26
|
+
alias_method_chain :instantiate, :selectable_includes
|
27
|
+
end
|
28
|
+
|
29
|
+
class ActiveRecord::Associations::ClassMethods::JoinDependency::JoinBase
|
30
|
+
attr_accessor :extra_columns
|
31
|
+
|
32
|
+
def extract_record_with_selectable_includes(row)
|
33
|
+
record = extract_record_without_selectable_includes(row)
|
34
|
+
extra_columns.inject(record){|record, an| record[an] = row[an]; record} if extra_columns
|
35
|
+
record
|
36
|
+
end
|
37
|
+
|
38
|
+
alias_method_chain :extract_record, :selectable_includes
|
39
|
+
end
|
40
|
+
|
data/lib/fake_arel.rb
CHANGED
@@ -7,9 +7,9 @@ require 'fake_arel/extensions'
|
|
7
7
|
require 'fake_arel/with_scope_replacement'
|
8
8
|
require 'fake_arel/rails_3_finders'
|
9
9
|
require 'fake_arel/calculations'
|
10
|
+
require 'fake_arel/selectable_includes'
|
10
11
|
|
11
12
|
module FakeArel
|
12
|
-
VERSION = '0.9.9'
|
13
13
|
ActiveRecord::Base.send :include, Rails3Finders
|
14
14
|
ActiveRecord::Base.send :include, WithScopeReplacement
|
15
15
|
end
|
data/spec/fake_arel_spec.rb
CHANGED
@@ -79,7 +79,7 @@ describe "Fake Arel" do
|
|
79
79
|
it "should properly chain scope in definitions by lambda" do
|
80
80
|
Reply.recent_topic_id(4).all.should == Reply.find_all_by_id(5)
|
81
81
|
end
|
82
|
-
|
82
|
+
|
83
83
|
it "should properly chain order scope in definitions by lambda" do
|
84
84
|
Reply.topic__id_asc(4).all.should == Reply.find(:all, :conditions => {:topic_id => 4}, :order=>'id asc')
|
85
85
|
Reply.order('id desc').topic_id(4).all.should == Reply.find(:all, :conditions => {:topic_id => 4}, :order=>'id desc')
|
@@ -89,7 +89,7 @@ describe "Fake Arel" do
|
|
89
89
|
Reply.topic__id_desc2(4).all.should == topic_4_id_desc
|
90
90
|
Reply.topic__id_desc3(4).all.should == topic_4_id_desc
|
91
91
|
end
|
92
|
-
|
92
|
+
|
93
93
|
it "should chain order scopes" do
|
94
94
|
# see https://rails.lighthouseapp.com/projects/8994/tickets/2810-with_scope-should-accept-and-use-order-option
|
95
95
|
# it works now in reverse order to comply with ActiveRecord 3
|
@@ -104,14 +104,14 @@ describe "Fake Arel" do
|
|
104
104
|
end
|
105
105
|
end.should == Reply.find(:all, :order=>'created_at desc, topic_id asc')
|
106
106
|
end
|
107
|
-
|
107
|
+
|
108
108
|
it "should chain string join scope" do
|
109
109
|
lambda {
|
110
110
|
Topic.join_replies_by_string_and_author.all
|
111
111
|
Topic.join_replies_by_string_and_author_lambda.all
|
112
112
|
}.should_not raise_error
|
113
113
|
end
|
114
|
-
|
114
|
+
|
115
115
|
it "should properly chain with includes" do
|
116
116
|
topics = nil
|
117
117
|
lambda {
|
@@ -174,7 +174,7 @@ describe "Fake Arel" do
|
|
174
174
|
# an example using joins, as well as a query that returns nothing
|
175
175
|
Reply.or(Reply.recent_joins_topic, Reply.topic_title_is("Nothin")).all.map(&:id).should == [5]
|
176
176
|
end
|
177
|
-
|
177
|
+
|
178
178
|
it 'should be able to combine with "or", an empty named scope with another non-empty one' do
|
179
179
|
q1 = Reply.where(:id => 0)
|
180
180
|
q2 = Reply.where(:id => 2)
|
@@ -249,3 +249,15 @@ describe "NameScope bug" do
|
|
249
249
|
end
|
250
250
|
end
|
251
251
|
|
252
|
+
describe "using select with include" do
|
253
|
+
it "should be able to use select with include" do
|
254
|
+
Reply.create(:content => 'yeah', :topic_id => Topic.first)
|
255
|
+
Topic.includes(:replies).select("topics.id as super_topic_id").where("replies.content = 'yeah'").first.super_topic_id.should_not be_nil
|
256
|
+
end
|
257
|
+
|
258
|
+
it "should allow named scopes to use select and include together" do
|
259
|
+
Topic.select_only_id.all.map(&:super_duper_id).each {|t| t.should_not be_nil }
|
260
|
+
Topic.send(:select_only_id).all.map(&:super_duper_id).each {|t| t.should_not be_nil }
|
261
|
+
end
|
262
|
+
end
|
263
|
+
|
data/spec/fixtures/reply.rb
CHANGED
@@ -32,7 +32,7 @@ class Reply < ActiveRecord::Base
|
|
32
32
|
named_scope :topic_id_asc, order('topic_id asc')
|
33
33
|
named_scope :topic_id_asc_id_desc, order('topic_id asc').id_desc
|
34
34
|
named_scope :lam_topic_id_asc_id_desc, lambda{ topic_id_asc.id_desc }
|
35
|
-
|
35
|
+
|
36
36
|
validates_presence_of :content
|
37
37
|
|
38
38
|
def self.find_all_but_first
|
data/spec/fixtures/topic.rb
CHANGED
@@ -3,15 +3,16 @@ class Topic < ActiveRecord::Base
|
|
3
3
|
belongs_to :author
|
4
4
|
|
5
5
|
named_scope :mentions_activerecord, :conditions => ['topics.title LIKE ?', '%ActiveRecord%']
|
6
|
-
|
6
|
+
|
7
7
|
named_scope :with_replies_starting_with, lambda { |text|
|
8
8
|
{ :conditions => "replies.content LIKE '#{text}%' ", :include => :replies }
|
9
9
|
}
|
10
|
-
|
10
|
+
|
11
11
|
named_scope :mentions_activerecord_with_replies, includes(:replies).mentions_activerecord
|
12
12
|
named_scope :by_title_with_replies, lambda {|title| includes(:replies).where('topics.title like ?', title) }
|
13
|
-
|
13
|
+
|
14
14
|
named_scope :join_replies_by_string, joins('inner join replies on topics.id = replies.topic_id')
|
15
15
|
named_scope :join_replies_by_string_and_author, join_replies_by_string.joins(:author)
|
16
16
|
named_scope :join_replies_by_string_and_author_lambda, join_replies_by_string.joins(:author)
|
17
|
+
named_scope :select_only_id, select('id as super_duper_id').includes(:replies)
|
17
18
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -11,9 +11,6 @@ require 'active_record/fixtures'
|
|
11
11
|
require 'fake_arel'
|
12
12
|
gem 'sqlite3-ruby'
|
13
13
|
|
14
|
-
require 'ruby-debug'
|
15
|
-
Debugger.start
|
16
|
-
|
17
14
|
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => 'spec/test.db')
|
18
15
|
ActiveRecord::Base.logger = Logger.new(STDOUT) if $0 == 'irb'
|
19
16
|
|
metadata
CHANGED
@@ -1,13 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fake_arel
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
5
|
-
prerelease:
|
4
|
+
hash: 58
|
5
|
+
prerelease: 6
|
6
6
|
segments:
|
7
|
+
- 1
|
7
8
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
9
|
+
- 0
|
10
|
+
- a
|
11
|
+
version: 1.0.0.a
|
11
12
|
platform: ruby
|
12
13
|
authors:
|
13
14
|
- Grant Ammons
|
@@ -15,68 +16,125 @@ autorequire:
|
|
15
16
|
bindir: bin
|
16
17
|
cert_chain: []
|
17
18
|
|
18
|
-
date: 2011-
|
19
|
+
date: 2011-10-27 00:00:00 -04:00
|
19
20
|
default_executable:
|
20
21
|
dependencies:
|
21
22
|
- !ruby/object:Gem::Dependency
|
23
|
+
prerelease: false
|
24
|
+
name: activerecord
|
25
|
+
type: :runtime
|
22
26
|
version_requirements: &id001 !ruby/object:Gem::Requirement
|
23
27
|
none: false
|
24
28
|
requirements:
|
25
|
-
- -
|
29
|
+
- - ~>
|
26
30
|
- !ruby/object:Gem::Version
|
27
|
-
hash:
|
31
|
+
hash: 21
|
28
32
|
segments:
|
29
33
|
- 2
|
30
34
|
- 3
|
31
|
-
-
|
32
|
-
version: 2.3.
|
33
|
-
type: :runtime
|
35
|
+
- 11
|
36
|
+
version: 2.3.11
|
34
37
|
requirement: *id001
|
35
|
-
name: activerecord
|
36
|
-
prerelease: false
|
37
38
|
- !ruby/object:Gem::Dependency
|
39
|
+
prerelease: false
|
40
|
+
name: bundler
|
41
|
+
type: :development
|
38
42
|
version_requirements: &id002 !ruby/object:Gem::Requirement
|
39
43
|
none: false
|
40
44
|
requirements:
|
41
|
-
- -
|
45
|
+
- - ~>
|
42
46
|
- !ruby/object:Gem::Version
|
43
|
-
hash:
|
47
|
+
hash: 23
|
44
48
|
segments:
|
45
|
-
-
|
46
|
-
-
|
49
|
+
- 1
|
50
|
+
- 0
|
51
|
+
- 0
|
52
|
+
version: 1.0.0
|
53
|
+
requirement: *id002
|
54
|
+
- !ruby/object:Gem::Dependency
|
55
|
+
prerelease: false
|
56
|
+
name: jeweler
|
57
|
+
type: :development
|
58
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ~>
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
hash: 7
|
64
|
+
segments:
|
65
|
+
- 1
|
66
|
+
- 6
|
47
67
|
- 4
|
48
|
-
version:
|
68
|
+
version: 1.6.4
|
69
|
+
requirement: *id003
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
prerelease: false
|
72
|
+
name: rcov
|
49
73
|
type: :development
|
50
|
-
|
51
|
-
|
74
|
+
version_requirements: &id004 !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
hash: 3
|
80
|
+
segments:
|
81
|
+
- 0
|
82
|
+
version: "0"
|
83
|
+
requirement: *id004
|
84
|
+
- !ruby/object:Gem::Dependency
|
52
85
|
prerelease: false
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
86
|
+
name: rspec
|
87
|
+
type: :development
|
88
|
+
version_requirements: &id005 !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - "="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
hash: 25
|
94
|
+
segments:
|
95
|
+
- 1
|
96
|
+
- 3
|
97
|
+
- 1
|
98
|
+
version: 1.3.1
|
99
|
+
requirement: *id005
|
100
|
+
- !ruby/object:Gem::Dependency
|
101
|
+
prerelease: false
|
102
|
+
name: sqlite3-ruby
|
103
|
+
type: :development
|
104
|
+
version_requirements: &id006 !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
hash: 3
|
110
|
+
segments:
|
111
|
+
- 0
|
112
|
+
version: "0"
|
113
|
+
requirement: *id006
|
114
|
+
description: fake_arel will simulate rails 3 arel syntax for Rails 2.
|
115
|
+
email: grant@pipelinedealsco.com
|
61
116
|
executables: []
|
62
117
|
|
63
118
|
extensions: []
|
64
119
|
|
65
120
|
extra_rdoc_files:
|
66
|
-
-
|
67
|
-
-
|
121
|
+
- LICENSE.txt
|
122
|
+
- README.md
|
68
123
|
files:
|
69
124
|
- Gemfile
|
70
125
|
- Gemfile.lock
|
71
126
|
- History.txt
|
127
|
+
- LICENSE.txt
|
72
128
|
- Manifest.txt
|
73
|
-
- README.
|
129
|
+
- README.md
|
74
130
|
- Rakefile
|
131
|
+
- VERSION
|
75
132
|
- fake_arel.gemspec
|
76
133
|
- lib/fake_arel.rb
|
77
134
|
- lib/fake_arel/calculations.rb
|
78
135
|
- lib/fake_arel/extensions.rb
|
79
136
|
- lib/fake_arel/rails_3_finders.rb
|
137
|
+
- lib/fake_arel/selectable_includes.rb
|
80
138
|
- lib/fake_arel/singleton_class.rb
|
81
139
|
- lib/fake_arel/with_scope_replacement.rb
|
82
140
|
- script/console
|
@@ -92,17 +150,14 @@ files:
|
|
92
150
|
- spec/fixtures/topics.yml
|
93
151
|
- spec/spec.opts
|
94
152
|
- spec/spec_helper.rb
|
95
|
-
- spec/test.db
|
96
153
|
- tasks/rspec.rake
|
97
|
-
- .gemtest
|
98
154
|
has_rdoc: true
|
99
155
|
homepage: http://github.com/gammons/fake_arel
|
100
|
-
licenses:
|
101
|
-
|
156
|
+
licenses:
|
157
|
+
- MIT
|
102
158
|
post_install_message:
|
103
|
-
rdoc_options:
|
104
|
-
|
105
|
-
- README.rdoc
|
159
|
+
rdoc_options: []
|
160
|
+
|
106
161
|
require_paths:
|
107
162
|
- lib
|
108
163
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -117,18 +172,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
117
172
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
118
173
|
none: false
|
119
174
|
requirements:
|
120
|
-
- - "
|
175
|
+
- - ">"
|
121
176
|
- !ruby/object:Gem::Version
|
122
|
-
hash:
|
177
|
+
hash: 25
|
123
178
|
segments:
|
124
|
-
-
|
125
|
-
|
179
|
+
- 1
|
180
|
+
- 3
|
181
|
+
- 1
|
182
|
+
version: 1.3.1
|
126
183
|
requirements: []
|
127
184
|
|
128
|
-
rubyforge_project:
|
185
|
+
rubyforge_project:
|
129
186
|
rubygems_version: 1.4.2
|
130
187
|
signing_key:
|
131
188
|
specification_version: 3
|
132
|
-
summary: "
|
189
|
+
summary: "fake_arel: a Rails 3 query interface to Rails 2"
|
133
190
|
test_files: []
|
134
191
|
|
data/.gemtest
DELETED
File without changes
|
data/README.rdoc
DELETED
@@ -1,91 +0,0 @@
|
|
1
|
-
== Fake_arel: Rails 3 Query Interface for Rails 2
|
2
|
-
|
3
|
-
http://github.com/gammons/fake_arel
|
4
|
-
|
5
|
-
== DESCRIPTION:
|
6
|
-
|
7
|
-
* Tired of waiting for Rails 3 and its new super-sweet query interface? Try fake_arel!
|
8
|
-
|
9
|
-
* Fake_arel is a gem that will simulate the new Rails 3 query interface, using named_scopes and a couple small patches to ActiveRecord.
|
10
|
-
|
11
|
-
* This should serve as a nice bridge between Rails 2 and Rails 3 apps, and can be removed once upgrading your app to rails 3, and everything (hopefully) should still work.
|
12
|
-
|
13
|
-
== SYNOPSIS:
|
14
|
-
|
15
|
-
* All the finders described on Pratik's blog have been implemented. (http://m.onkey.org/2010/1/22/active-record-query-interface)
|
16
|
-
|
17
|
-
Reply.where(:id => 1)
|
18
|
-
Reply.select("content,id").where("id > 1").order("id desc").limit(1)
|
19
|
-
Topic.joins(:replies).limit(1)
|
20
|
-
|
21
|
-
* Additionally, named_scopes are very similar to Rails 3 relations, in that they are lazily loaded.
|
22
|
-
|
23
|
-
>> Reply.where(:name => "John").class
|
24
|
-
ActiveRecord::NamedScope::Scope
|
25
|
-
|
26
|
-
* Also implemented was <tt>to_sql</tt>. <tt>to_sql</tt> will work on any chained query.
|
27
|
-
>> Topic.joins(:replies).limit(1).to_sql
|
28
|
-
"SELECT \"topics\".* FROM \"topics\" INNER JOIN \"replies\" ON replies.topic_id = topics.id LIMIT 1"
|
29
|
-
|
30
|
-
* <tt>named_scope</tt> was modified to include other <tt>named_scope</tt>s, so you can chain them together.
|
31
|
-
|
32
|
-
class Reply < ActiveRecord::Base
|
33
|
-
named_scope :by_john, where(:name => "John")
|
34
|
-
named_scope :recent, lambda {|t| where("created_at > ? ", t.minutes.ago) }
|
35
|
-
named_scope :recent_by_john, recent(15).by_john
|
36
|
-
end
|
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
|
-
|
54
|
-
|
55
|
-
== REQUIREMENTS:
|
56
|
-
|
57
|
-
* >= ActiveRecord 2.3.5
|
58
|
-
|
59
|
-
== INSTALL:
|
60
|
-
|
61
|
-
* gem install fake_arel
|
62
|
-
|
63
|
-
== AUTHORS:
|
64
|
-
|
65
|
-
* Grant Ammons
|
66
|
-
* Sokolov Yura
|
67
|
-
|
68
|
-
== LICENSE:
|
69
|
-
|
70
|
-
(The MIT License)
|
71
|
-
|
72
|
-
Copyright (c) 2010 Grant Ammons (grant@pipelinedealsco.com)
|
73
|
-
|
74
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
75
|
-
a copy of this software and associated documentation files (the
|
76
|
-
'Software'), to deal in the Software without restriction, including
|
77
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
78
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
79
|
-
permit persons to whom the Software is furnished to do so, subject to
|
80
|
-
the following conditions:
|
81
|
-
|
82
|
-
The above copyright notice and this permission notice shall be
|
83
|
-
included in all copies or substantial portions of the Software.
|
84
|
-
|
85
|
-
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
86
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
87
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
88
|
-
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
89
|
-
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
90
|
-
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
91
|
-
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/spec/test.db
DELETED
Binary file
|