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 CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZDEwZmY4ZGVhMTNmZWQ5Mzk0ZDk3NTZhNTk0NzdjN2FkYjNlNGI0MQ==
4
+ ZGQwNTNhY2Q5MWZjZDE5NWM2NzhhYTdlODJmMTVlY2MwMTEyZmU1Yg==
5
5
  data.tar.gz: !binary |-
6
- OGYwYTQ1MWM0OTQ5NzhlNjhlYmNhYTkwNjU1ZWM2ZTMwNzljNWIxNA==
6
+ MGYzNjBlNzlhZmViMDljODQwODc1YjA5MTVlZTUyMWE0MmM0NTIxNg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- YTcwNGZkZWIxM2Q0NDhkNGYyY2MzOTI4NmUyMjhjMTNhYzViMzQyMzMxMDU4
10
- MTliZDAyNzY2MzQ2NzgyMzEwMDBiMmU4NDM4ZWU3MDJhNTk3YmY1MTE4OGZj
11
- ZDRmMDBhNjU4MmRmMmUwZTgzZDE3ZDFhZWIzNzYwNjg1MTg5NDI=
9
+ N2I4YTI4YjQwMTRjZWQyNzUzNDJkMDc1ODlhNjUyMGU0YTczNzYwZjUwNDRi
10
+ ZmI4YTIyYWEzNjA5ZDQ3ODNkNzY0YTJhMDAwNWYwNjFkMDFmZTBjZWY5Yjk1
11
+ NDZhZWJlZDZlODZiMDAxN2JjYTc1MGVjZDJhNDJlZGVmYTg1NDg=
12
12
  data.tar.gz: !binary |-
13
- NDljZDRlMWJiMGE1ZWNmYTU2NTg3MTQ5NGFmMjBiYmE2NjVmN2ExNGJjYzZi
14
- YmFmMjcxODg5Y2Q3NDNiZjA4NmNmYTVhOWVmYTdkOWQxMWQ2ZDM3ZTI0OWRl
15
- ZDM3NTNkNDI1NDE2ZGZjM2MwOTVhMmU5YzllOWYxMWUzNzA5ZTE=
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
- 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)
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/<my-github-username>/proximal_records/fork )
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
- with_near_by = scope.select("articles.*, LAG(articles.id) #{orders} AS previous_id, LEAD(articles.id) #{orders} AS next_id")
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('articles'))
12
- a = self.class.from(as.to_sql).where(articles: {id: id}).limit(1)
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 = 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
+ [(klass.find_by_id(a.previous_id)), (klass.find_by_id(a.next_id))]
17
18
  end
18
19
  end
19
20
  end
@@ -1,3 +1,3 @@
1
1
  module ProximalRecords
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: proximal_records
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmitry Polushkin