proximal_records 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +18 -0
- data/.rspec +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +17 -0
- data/Appraisals +11 -0
- data/Gemfile +5 -0
- data/LICENSE.txt +22 -0
- data/README.md +57 -0
- data/Rakefile +17 -0
- data/gemfiles/rails_3.2.gemfile +7 -0
- data/gemfiles/rails_3.2.gemfile.lock +55 -0
- data/gemfiles/rails_4.0.gemfile +7 -0
- data/gemfiles/rails_4.0.gemfile.lock +61 -0
- data/gemfiles/rails_4.1.gemfile +7 -0
- data/gemfiles/rails_4.1.gemfile.lock +60 -0
- data/lib/proximal_records/adapters/mysql2.rb +31 -0
- data/lib/proximal_records/adapters/postgresql.rb +20 -0
- data/lib/proximal_records/version.rb +3 -0
- data/lib/proximal_records.rb +18 -0
- data/proximal_records.gemspec +28 -0
- data/spec/config.yml +20 -0
- data/spec/database.rb +11 -0
- data/spec/proximal_records_spec.rb +22 -0
- data/spec/schema.rb +10 -0
- data/spec/spec_helper.rb +22 -0
- data/tasks/database.rake +42 -0
- metadata +161 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
ZDEwZmY4ZGVhMTNmZWQ5Mzk0ZDk3NTZhNTk0NzdjN2FkYjNlNGI0MQ==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
OGYwYTQ1MWM0OTQ5NzhlNjhlYmNhYTkwNjU1ZWM2ZTMwNzljNWIxNA==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
YTcwNGZkZWIxM2Q0NDhkNGYyY2MzOTI4NmUyMjhjMTNhYzViMzQyMzMxMDU4
|
10
|
+
MTliZDAyNzY2MzQ2NzgyMzEwMDBiMmU4NDM4ZWU3MDJhNTk3YmY1MTE4OGZj
|
11
|
+
ZDRmMDBhNjU4MmRmMmUwZTgzZDE3ZDFhZWIzNzYwNjg1MTg5NDI=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
NDljZDRlMWJiMGE1ZWNmYTU2NTg3MTQ5NGFmMjBiYmE2NjVmN2ExNGJjYzZi
|
14
|
+
YmFmMjcxODg5Y2Q3NDNiZjA4NmNmYTVhOWVmYTdkOWQxMWQ2ZDM3ZTI0OWRl
|
15
|
+
ZDM3NTNkNDI1NDE2ZGZjM2MwOTVhMmU5YzllOWYxMWUzNzA5ZTE=
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--format documentation --color
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.1.0
|
data/.travis.yml
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
rvm:
|
2
|
+
- 1.9.3
|
3
|
+
- 2.0.0
|
4
|
+
- 2.1.1
|
5
|
+
|
6
|
+
before_script: 'bundle exec rake db:create'
|
7
|
+
|
8
|
+
script: 'bundle exec rake spec'
|
9
|
+
|
10
|
+
env:
|
11
|
+
- 'DATABASE=mysql2'
|
12
|
+
- 'DATABASE=postgresql'
|
13
|
+
|
14
|
+
gemfile:
|
15
|
+
- gemfiles/rails_3.2.gemfile
|
16
|
+
- gemfiles/rails_4.0.gemfile
|
17
|
+
- gemfiles/rails_4.1.gemfile
|
data/Appraisals
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Dmitry Polushkin
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# ProximalRecords
|
2
|
+
|
3
|
+
ActiveRecord extension to find out near by (proximal) records (previous and next) from the ActiveRelation scopes (using AREL).
|
4
|
+
|
5
|
+
The benefit of this gem, it can take any scope, and it does subquery for taking previous and next records.
|
6
|
+
|
7
|
+
### Supported adapters
|
8
|
+
|
9
|
+
* mysql2
|
10
|
+
* postgresql
|
11
|
+
|
12
|
+
### Tested on
|
13
|
+
|
14
|
+
* Ruby: 1.9.3, 2.0.0, 2.1.1
|
15
|
+
* ActiveRecord: 3.2, 4.0, 4.1
|
16
|
+
|
17
|
+
[![Build Status](https://travis-ci.org/dmitry/proximal_records.svg?branch=master)](https://travis-ci.org/dmitry/proximal_records)
|
18
|
+
|
19
|
+
|
20
|
+
## Installation
|
21
|
+
|
22
|
+
Add this line to your application's Gemfile:
|
23
|
+
|
24
|
+
gem 'proximal_records'
|
25
|
+
|
26
|
+
And then execute:
|
27
|
+
|
28
|
+
$ bundle
|
29
|
+
|
30
|
+
Or install it yourself as:
|
31
|
+
|
32
|
+
$ gem install proximal_records
|
33
|
+
|
34
|
+
|
35
|
+
## Usage
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
scope = Article.title_like('proximal').order('created_at DESC, title ASC')
|
39
|
+
current_record = Article.find(20) # it's part of the scope
|
40
|
+
p, n = current_record.proximal_records(scope)
|
41
|
+
```
|
42
|
+
|
43
|
+
After that you will get `previous` and `next` records, that are proximal (near by) records of current_record.
|
44
|
+
|
45
|
+
|
46
|
+
## Alternatives
|
47
|
+
|
48
|
+
- https://github.com/charly/nexter
|
49
|
+
- https://github.com/glebm/order_query
|
50
|
+
|
51
|
+
## Contributing
|
52
|
+
|
53
|
+
1. Fork it ( http://github.com/<my-github-username>/proximal_records/fork )
|
54
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
55
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
56
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
57
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'bundler/setup'
|
2
|
+
require 'appraisal'
|
3
|
+
require 'bundler/gem_tasks'
|
4
|
+
|
5
|
+
require 'rspec/core/rake_task'
|
6
|
+
RSpec::Core::RakeTask.new(:spec)
|
7
|
+
|
8
|
+
|
9
|
+
load 'tasks/database.rake'
|
10
|
+
|
11
|
+
|
12
|
+
desc 'Default: run unit tests.'
|
13
|
+
task :default => [:spec]
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
|
@@ -0,0 +1,55 @@
|
|
1
|
+
PATH
|
2
|
+
remote: ../
|
3
|
+
specs:
|
4
|
+
proximal_records (0.0.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
activemodel (3.2.17)
|
10
|
+
activesupport (= 3.2.17)
|
11
|
+
builder (~> 3.0.0)
|
12
|
+
activerecord (3.2.17)
|
13
|
+
activemodel (= 3.2.17)
|
14
|
+
activesupport (= 3.2.17)
|
15
|
+
arel (~> 3.0.2)
|
16
|
+
tzinfo (~> 0.3.29)
|
17
|
+
activesupport (3.2.17)
|
18
|
+
i18n (~> 0.6, >= 0.6.4)
|
19
|
+
multi_json (~> 1.0)
|
20
|
+
appraisal (1.0.0)
|
21
|
+
bundler
|
22
|
+
rake
|
23
|
+
thor (>= 0.14.0)
|
24
|
+
arel (3.0.3)
|
25
|
+
builder (3.0.4)
|
26
|
+
diff-lcs (1.2.5)
|
27
|
+
i18n (0.6.9)
|
28
|
+
multi_json (1.9.0)
|
29
|
+
mysql2 (0.3.15)
|
30
|
+
pg (0.17.1)
|
31
|
+
rake (10.3.1)
|
32
|
+
rspec (2.14.1)
|
33
|
+
rspec-core (~> 2.14.0)
|
34
|
+
rspec-expectations (~> 2.14.0)
|
35
|
+
rspec-mocks (~> 2.14.0)
|
36
|
+
rspec-core (2.14.8)
|
37
|
+
rspec-expectations (2.14.5)
|
38
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
39
|
+
rspec-mocks (2.14.6)
|
40
|
+
sqlite3 (1.3.9)
|
41
|
+
thor (0.19.1)
|
42
|
+
tzinfo (0.3.39)
|
43
|
+
|
44
|
+
PLATFORMS
|
45
|
+
ruby
|
46
|
+
|
47
|
+
DEPENDENCIES
|
48
|
+
activerecord (~> 3.2)
|
49
|
+
appraisal
|
50
|
+
mysql2
|
51
|
+
pg
|
52
|
+
proximal_records!
|
53
|
+
rake
|
54
|
+
rspec
|
55
|
+
sqlite3
|
@@ -0,0 +1,61 @@
|
|
1
|
+
PATH
|
2
|
+
remote: ../
|
3
|
+
specs:
|
4
|
+
proximal_records (0.0.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
activemodel (4.0.4)
|
10
|
+
activesupport (= 4.0.4)
|
11
|
+
builder (~> 3.1.0)
|
12
|
+
activerecord (4.0.4)
|
13
|
+
activemodel (= 4.0.4)
|
14
|
+
activerecord-deprecated_finders (~> 1.0.2)
|
15
|
+
activesupport (= 4.0.4)
|
16
|
+
arel (~> 4.0.0)
|
17
|
+
activerecord-deprecated_finders (1.0.3)
|
18
|
+
activesupport (4.0.4)
|
19
|
+
i18n (~> 0.6, >= 0.6.9)
|
20
|
+
minitest (~> 4.2)
|
21
|
+
multi_json (~> 1.3)
|
22
|
+
thread_safe (~> 0.1)
|
23
|
+
tzinfo (~> 0.3.37)
|
24
|
+
appraisal (1.0.0)
|
25
|
+
bundler
|
26
|
+
rake
|
27
|
+
thor (>= 0.14.0)
|
28
|
+
arel (4.0.1)
|
29
|
+
builder (3.1.4)
|
30
|
+
diff-lcs (1.2.5)
|
31
|
+
i18n (0.6.9)
|
32
|
+
minitest (4.7.5)
|
33
|
+
multi_json (1.9.0)
|
34
|
+
mysql2 (0.3.15)
|
35
|
+
pg (0.17.1)
|
36
|
+
rake (10.3.1)
|
37
|
+
rspec (2.14.1)
|
38
|
+
rspec-core (~> 2.14.0)
|
39
|
+
rspec-expectations (~> 2.14.0)
|
40
|
+
rspec-mocks (~> 2.14.0)
|
41
|
+
rspec-core (2.14.8)
|
42
|
+
rspec-expectations (2.14.5)
|
43
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
44
|
+
rspec-mocks (2.14.6)
|
45
|
+
sqlite3 (1.3.9)
|
46
|
+
thor (0.19.1)
|
47
|
+
thread_safe (0.3.3)
|
48
|
+
tzinfo (0.3.39)
|
49
|
+
|
50
|
+
PLATFORMS
|
51
|
+
ruby
|
52
|
+
|
53
|
+
DEPENDENCIES
|
54
|
+
activerecord (~> 4.0.4)
|
55
|
+
appraisal
|
56
|
+
mysql2
|
57
|
+
pg
|
58
|
+
proximal_records!
|
59
|
+
rake
|
60
|
+
rspec
|
61
|
+
sqlite3
|
@@ -0,0 +1,60 @@
|
|
1
|
+
PATH
|
2
|
+
remote: ../
|
3
|
+
specs:
|
4
|
+
proximal_records (0.0.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
activemodel (4.1.0)
|
10
|
+
activesupport (= 4.1.0)
|
11
|
+
builder (~> 3.1)
|
12
|
+
activerecord (4.1.0)
|
13
|
+
activemodel (= 4.1.0)
|
14
|
+
activesupport (= 4.1.0)
|
15
|
+
arel (~> 5.0.0)
|
16
|
+
activesupport (4.1.0)
|
17
|
+
i18n (~> 0.6, >= 0.6.9)
|
18
|
+
json (~> 1.7, >= 1.7.7)
|
19
|
+
minitest (~> 5.1)
|
20
|
+
thread_safe (~> 0.1)
|
21
|
+
tzinfo (~> 1.1)
|
22
|
+
appraisal (1.0.0)
|
23
|
+
bundler
|
24
|
+
rake
|
25
|
+
thor (>= 0.14.0)
|
26
|
+
arel (5.0.1.20140414130214)
|
27
|
+
builder (3.2.2)
|
28
|
+
diff-lcs (1.2.5)
|
29
|
+
i18n (0.6.9)
|
30
|
+
json (1.8.1)
|
31
|
+
minitest (5.3.3)
|
32
|
+
mysql2 (0.3.15)
|
33
|
+
pg (0.17.1)
|
34
|
+
rake (10.3.1)
|
35
|
+
rspec (2.14.1)
|
36
|
+
rspec-core (~> 2.14.0)
|
37
|
+
rspec-expectations (~> 2.14.0)
|
38
|
+
rspec-mocks (~> 2.14.0)
|
39
|
+
rspec-core (2.14.8)
|
40
|
+
rspec-expectations (2.14.5)
|
41
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
42
|
+
rspec-mocks (2.14.6)
|
43
|
+
sqlite3 (1.3.9)
|
44
|
+
thor (0.19.1)
|
45
|
+
thread_safe (0.3.3)
|
46
|
+
tzinfo (1.1.0)
|
47
|
+
thread_safe (~> 0.1)
|
48
|
+
|
49
|
+
PLATFORMS
|
50
|
+
ruby
|
51
|
+
|
52
|
+
DEPENDENCIES
|
53
|
+
activerecord (~> 4.1.0)
|
54
|
+
appraisal
|
55
|
+
mysql2
|
56
|
+
pg
|
57
|
+
proximal_records!
|
58
|
+
rake
|
59
|
+
rspec
|
60
|
+
sqlite3
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module ProximalRecords
|
2
|
+
module Adapters
|
3
|
+
module Mysql2
|
4
|
+
def proximal_records(scope)
|
5
|
+
klass = self.class
|
6
|
+
|
7
|
+
z = klass.select("#{klass.table_name}.*, IF(rownum < @num, 'previous', 'next') AS direction, rownum").arel
|
8
|
+
t = klass.select("z.*, @rownum := @rownum + 1 AS rownum, CASE WHEN z.id = #{id} THEN @num := @rownum END").arel
|
9
|
+
|
10
|
+
t.join(Arel.sql('JOIN (SELECT @rownum := 0, @num := NULL) n'))
|
11
|
+
|
12
|
+
t.from("(#{scope.to_sql}) z")
|
13
|
+
z.from("(#{t.to_sql}) #{klass.table_name}")
|
14
|
+
|
15
|
+
sql = z.where(Arel.sql('rownum > @num - 2 AND rownum < @num + 2 AND rownum <> @num')).to_sql
|
16
|
+
|
17
|
+
items = klass.find_by_sql(sql)
|
18
|
+
|
19
|
+
if items[0]
|
20
|
+
if items[0].direction == 'previous'
|
21
|
+
[items[0], items[1]]
|
22
|
+
else
|
23
|
+
[nil, items[0]]
|
24
|
+
end
|
25
|
+
else
|
26
|
+
[nil, nil]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module ProximalRecords
|
2
|
+
module Adapters
|
3
|
+
module Postgresql
|
4
|
+
def proximal_records(scope)
|
5
|
+
orders = scope.orders.join(', ')
|
6
|
+
|
7
|
+
orders = "OVER(#{"ORDER BY #{orders}" if orders.present?})"
|
8
|
+
with_near_by = scope.select("articles.*, LAG(articles.id) #{orders} AS previous_id, LEAD(articles.id) #{orders} AS next_id")
|
9
|
+
|
10
|
+
table = with_near_by.arel
|
11
|
+
as = table.as(Arel.sql('articles'))
|
12
|
+
a = self.class.from(as.to_sql).where(articles: {id: id}).limit(1)
|
13
|
+
|
14
|
+
a = a.first
|
15
|
+
previous_id, next_id = a.previous_id, a.next_id
|
16
|
+
[(self.class.find_by_id(previous_id)), (self.class.find_by_id(next_id))]
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require "proximal_records/version"
|
2
|
+
|
3
|
+
require 'active_support/core_ext/string'
|
4
|
+
|
5
|
+
require 'proximal_records/adapters/mysql2'
|
6
|
+
require 'proximal_records/adapters/postgresql'
|
7
|
+
|
8
|
+
module ProximalRecords
|
9
|
+
def self.included(base)
|
10
|
+
raise 'Should be included to the ActiveRecord::Base class' if base.is_a?(ActiveRecord::Base)
|
11
|
+
|
12
|
+
adapter_class_name = base.connection.adapter_name.downcase.capitalize
|
13
|
+
module_path = "ProximalRecords::Adapters::#{adapter_class_name}"
|
14
|
+
mod = module_path.constantize
|
15
|
+
|
16
|
+
base.send(:include, mod)
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'proximal_records/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'proximal_records'
|
8
|
+
spec.version = ProximalRecords::VERSION
|
9
|
+
spec.authors = ['Dmitry Polushkin']
|
10
|
+
spec.email = ['dmitry.polushkin@gmail.com']
|
11
|
+
spec.summary = %q{Find next / previous ActiveRecord records out of the scope in one query}
|
12
|
+
spec.description = %q{ActiveRecord extension to find out near by (proximal) previous and next records from the ActiveRelation scopes (using AREL). Uses special sub-queries. Supported mysql2 and postgresql adapters.}
|
13
|
+
spec.homepage = ''
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_development_dependency 'appraisal'
|
22
|
+
spec.add_development_dependency 'rspec'
|
23
|
+
spec.add_development_dependency 'rake'
|
24
|
+
|
25
|
+
spec.add_development_dependency 'sqlite3'
|
26
|
+
spec.add_development_dependency 'mysql2'
|
27
|
+
spec.add_development_dependency 'pg'
|
28
|
+
end
|
data/spec/config.yml
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
mysql2:
|
2
|
+
adapter: mysql2
|
3
|
+
database: proximal_records
|
4
|
+
username: root
|
5
|
+
encoding: utf8
|
6
|
+
|
7
|
+
postgresql:
|
8
|
+
adapter: postgresql
|
9
|
+
database: proximal_records
|
10
|
+
username: postgres
|
11
|
+
password:
|
12
|
+
min_messages: warning
|
13
|
+
|
14
|
+
sqlite3:
|
15
|
+
database: fixture_database.sqlite3
|
16
|
+
timeout: 5000
|
17
|
+
|
18
|
+
sqlite3_mem:
|
19
|
+
adapter: sqlite3
|
20
|
+
database: ':memory:'
|
data/spec/database.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
|
4
|
+
describe ProximalRecords do
|
5
|
+
it 'normal' do
|
6
|
+
a1 = Article.create!(title: '1')
|
7
|
+
a2 = Article.create!(title: '2')
|
8
|
+
a3 = Article.create!(title: '3')
|
9
|
+
|
10
|
+
|
11
|
+
p, n = a3.proximal_records(Article.where(title: %w(1 2 3)))
|
12
|
+
|
13
|
+
p.should eq a2
|
14
|
+
n.should be_nil
|
15
|
+
|
16
|
+
|
17
|
+
p, n = a3.proximal_records(Article.where(title: %w(1 2 3)).order('title DESC'))
|
18
|
+
|
19
|
+
n.should eq a2
|
20
|
+
p.should be_nil
|
21
|
+
end
|
22
|
+
end
|
data/spec/schema.rb
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require 'active_record'
|
3
|
+
require 'proximal_records'
|
4
|
+
require 'database'
|
5
|
+
|
6
|
+
# ActiveRecord::Base.logger = Logger.new('test.log')
|
7
|
+
# ActiveRecord::Base.logger = nil
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
ActiveRecord::Base.establish_connection(Database.config[ENV['DATABASE'] || 'mysql2'])
|
12
|
+
|
13
|
+
# def setup_db
|
14
|
+
ActiveRecord::Migration.verbose = false
|
15
|
+
load 'schema.rb'
|
16
|
+
# end
|
17
|
+
|
18
|
+
def teardown_db
|
19
|
+
ActiveRecord::Base.connection.tables.each do |table|
|
20
|
+
ActiveRecord::Base.connection.drop_table(table)
|
21
|
+
end
|
22
|
+
end
|
data/tasks/database.rake
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__)) + '/../spec/database'
|
2
|
+
|
3
|
+
namespace :db do
|
4
|
+
desc 'Build MySQL and PostgreSQL test databases'
|
5
|
+
task create: ['mysql:build_databases', 'postgresql:build_databases']
|
6
|
+
desc 'Drop MySQL and PostgreSQL test databases'
|
7
|
+
task drop: ['mysql:drop_databases', 'postgresql:drop_databases']
|
8
|
+
end
|
9
|
+
|
10
|
+
namespace :mysql do
|
11
|
+
config = Database.config['mysql2']
|
12
|
+
|
13
|
+
desc 'Build the MySQL test databases'
|
14
|
+
task :build_databases do
|
15
|
+
%x( mysql --user=#{config['username']} -e "create DATABASE #{config['database']} DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_unicode_ci ")
|
16
|
+
end
|
17
|
+
|
18
|
+
desc 'Drop the MySQL test databases'
|
19
|
+
task :drop_databases do
|
20
|
+
%x( mysqladmin --user=#{config['username']} -f drop #{config['database']} )
|
21
|
+
end
|
22
|
+
|
23
|
+
desc 'Rebuild the MySQL test databases'
|
24
|
+
task :rebuild_databases => [:drop_databases, :build_databases]
|
25
|
+
end
|
26
|
+
|
27
|
+
namespace :postgresql do
|
28
|
+
config = Database.config['postgresql']
|
29
|
+
|
30
|
+
desc 'Build the PostgreSQL test databases'
|
31
|
+
task :build_databases do
|
32
|
+
%x( createdb -E UTF8 -T template0 #{config['database']} )
|
33
|
+
end
|
34
|
+
|
35
|
+
desc 'Drop the PostgreSQL test databases'
|
36
|
+
task :drop_databases do
|
37
|
+
%x( dropdb #{config['database']} )
|
38
|
+
end
|
39
|
+
|
40
|
+
desc 'Rebuild the PostgreSQL test databases'
|
41
|
+
task :rebuild_databases => [:drop_databases, :build_databases]
|
42
|
+
end
|
metadata
ADDED
@@ -0,0 +1,161 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: proximal_records
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dmitry Polushkin
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-04-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: appraisal
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ! '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ! '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: sqlite3
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ! '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: mysql2
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ! '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: pg
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ! '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ! '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: ActiveRecord extension to find out near by (proximal) previous and next
|
98
|
+
records from the ActiveRelation scopes (using AREL). Uses special sub-queries. Supported
|
99
|
+
mysql2 and postgresql adapters.
|
100
|
+
email:
|
101
|
+
- dmitry.polushkin@gmail.com
|
102
|
+
executables: []
|
103
|
+
extensions: []
|
104
|
+
extra_rdoc_files: []
|
105
|
+
files:
|
106
|
+
- .gitignore
|
107
|
+
- .rspec
|
108
|
+
- .ruby-version
|
109
|
+
- .travis.yml
|
110
|
+
- Appraisals
|
111
|
+
- Gemfile
|
112
|
+
- LICENSE.txt
|
113
|
+
- README.md
|
114
|
+
- Rakefile
|
115
|
+
- gemfiles/rails_3.2.gemfile
|
116
|
+
- gemfiles/rails_3.2.gemfile.lock
|
117
|
+
- gemfiles/rails_4.0.gemfile
|
118
|
+
- gemfiles/rails_4.0.gemfile.lock
|
119
|
+
- gemfiles/rails_4.1.gemfile
|
120
|
+
- gemfiles/rails_4.1.gemfile.lock
|
121
|
+
- lib/proximal_records.rb
|
122
|
+
- lib/proximal_records/adapters/mysql2.rb
|
123
|
+
- lib/proximal_records/adapters/postgresql.rb
|
124
|
+
- lib/proximal_records/version.rb
|
125
|
+
- proximal_records.gemspec
|
126
|
+
- spec/config.yml
|
127
|
+
- spec/database.rb
|
128
|
+
- spec/proximal_records_spec.rb
|
129
|
+
- spec/schema.rb
|
130
|
+
- spec/spec_helper.rb
|
131
|
+
- tasks/database.rake
|
132
|
+
homepage: ''
|
133
|
+
licenses:
|
134
|
+
- MIT
|
135
|
+
metadata: {}
|
136
|
+
post_install_message:
|
137
|
+
rdoc_options: []
|
138
|
+
require_paths:
|
139
|
+
- lib
|
140
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
141
|
+
requirements:
|
142
|
+
- - ! '>='
|
143
|
+
- !ruby/object:Gem::Version
|
144
|
+
version: '0'
|
145
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
146
|
+
requirements:
|
147
|
+
- - ! '>='
|
148
|
+
- !ruby/object:Gem::Version
|
149
|
+
version: '0'
|
150
|
+
requirements: []
|
151
|
+
rubyforge_project:
|
152
|
+
rubygems_version: 2.2.2
|
153
|
+
signing_key:
|
154
|
+
specification_version: 4
|
155
|
+
summary: Find next / previous ActiveRecord records out of the scope in one query
|
156
|
+
test_files:
|
157
|
+
- spec/config.yml
|
158
|
+
- spec/database.rb
|
159
|
+
- spec/proximal_records_spec.rb
|
160
|
+
- spec/schema.rb
|
161
|
+
- spec/spec_helper.rb
|