proximal_records 0.0.1 → 0.0.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.
- checksums.yaml +8 -8
- data/README.md +13 -5
- data/lib/proximal_records/adapters/postgresql.rb +7 -6
- data/lib/proximal_records/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
ZGQwNTNhY2Q5MWZjZDE5NWM2NzhhYTdlODJmMTVlY2MwMTEyZmU1Yg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
MGYzNjBlNzlhZmViMDljODQwODc1YjA5MTVlZTUyMWE0MmM0NTIxNg==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
N2I4YTI4YjQwMTRjZWQyNzUzNDJkMDc1ODlhNjUyMGU0YTczNzYwZjUwNDRi
|
10
|
+
ZmI4YTIyYWEzNjA5ZDQ3ODNkNzY0YTJhMDAwNWYwNjFkMDFmZTBjZWY5Yjk1
|
11
|
+
NDZhZWJlZDZlODZiMDAxN2JjYTc1MGVjZDJhNDJlZGVmYTg1NDg=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
MmEzNjdlMTYyZjRlOTA3NWNjMTM1NzIzMDkxOTYxN2U4NDA1MDQxZTMzNjA1
|
14
|
+
M2NkMTNkMGEzYjlmNmFlMTEzMDkyMTRmYWQ3NjcxMWY1YzEwMzJiZjhjMDIz
|
15
|
+
ZjM5MDM3ZmNkZDI3NjZjMGI4NzRiYTA3MDA3ZmY1ZmFlODlhNTA=
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# ProximalRecords
|
1
|
+
# ProximalRecords 0.0.2
|
2
2
|
|
3
3
|
ActiveRecord extension to find out near by (proximal) records (previous and next) from the ActiveRelation scopes (using AREL).
|
4
4
|
|
@@ -34,10 +34,17 @@ Or install it yourself as:
|
|
34
34
|
|
35
35
|
## Usage
|
36
36
|
|
37
|
+
Include `ProximalRecords` module into your ActiveRecord model and use it with `proximal_records` method, that returns previous and next records in the array. If previous or next records didn't found, then returns nil.
|
38
|
+
|
37
39
|
```ruby
|
38
|
-
|
39
|
-
|
40
|
-
|
40
|
+
class Article < ActiveRecord::Base
|
41
|
+
include ProximalRecords
|
42
|
+
end
|
43
|
+
|
44
|
+
|
45
|
+
scope = Article.title_like('proximal').order('created_at DESC, title ASC')
|
46
|
+
current_record = Article.find(20) # it's part of the scope
|
47
|
+
p, n = current_record.proximal_records(scope)
|
41
48
|
```
|
42
49
|
|
43
50
|
After that you will get `previous` and `next` records, that are proximal (near by) records of current_record.
|
@@ -48,9 +55,10 @@ After that you will get `previous` and `next` records, that are proximal (near b
|
|
48
55
|
- https://github.com/charly/nexter
|
49
56
|
- https://github.com/glebm/order_query
|
50
57
|
|
58
|
+
|
51
59
|
## Contributing
|
52
60
|
|
53
|
-
1. Fork it ( http://github.com
|
61
|
+
1. Fork it ( http://github.com/dmitry/proximal_records/fork )
|
54
62
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
55
63
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
56
64
|
4. Push to the branch (`git push origin my-new-feature`)
|
@@ -2,18 +2,19 @@ module ProximalRecords
|
|
2
2
|
module Adapters
|
3
3
|
module Postgresql
|
4
4
|
def proximal_records(scope)
|
5
|
+
klass = self.class
|
6
|
+
|
5
7
|
orders = scope.orders.join(', ')
|
6
8
|
|
7
9
|
orders = "OVER(#{"ORDER BY #{orders}" if orders.present?})"
|
8
|
-
|
10
|
+
primary_key = "#{klass.table_name}.#{klass.primary_key}"
|
11
|
+
with_near_by = scope.select("#{klass.table_name}.*, LAG(#{primary_key}) #{orders} AS previous_id, LEAD(#{primary_key}) #{orders} AS next_id")
|
9
12
|
|
10
13
|
table = with_near_by.arel
|
11
|
-
as = table.as(Arel.sql('
|
12
|
-
a =
|
14
|
+
as = table.as(Arel.sql('z'))
|
15
|
+
a = klass.select('z.*').from(as.to_sql).where(z: {klass.primary_key => id}).limit(1)[0]
|
13
16
|
|
14
|
-
a
|
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
|
+
[(klass.find_by_id(a.previous_id)), (klass.find_by_id(a.next_id))]
|
17
18
|
end
|
18
19
|
end
|
19
20
|
end
|