nazrin 0.2.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +7 -2
- data/lib/nazrin/active_record/data_accessor.rb +11 -54
- data/lib/nazrin/search_client.rb +18 -0
- data/lib/nazrin/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 99559990894751321599905a57f89c390caae175
|
4
|
+
data.tar.gz: ddaafea268c27f3601084c64e4a324a367c8ab13
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 96ccde2bb3cc1b56925d96d9e81caaddef0725d875fe2cffa39e86ab33f691fcc9ea82841a62ec48db836f70af084598b1def681d790052cc6e13c5d54db2589
|
7
|
+
data.tar.gz: 0e6e4559327bc764ec29eeb9f818baaf5fdd148ea4da19767656eeea6f541c3962366f5071b152eeb8eb41019d989650e61cc7b0e6b6809459d002a033150e58
|
data/README.md
CHANGED
@@ -29,7 +29,7 @@ Or install it yourself as:
|
|
29
29
|
### in Ruby on Rails
|
30
30
|
|
31
31
|
```ruby
|
32
|
-
$ bundle exec rails g nazrin:config
|
32
|
+
$ bundle exec rails g nazrin:config # execute before including nazrin to model
|
33
33
|
|
34
34
|
Nazrin.configure do |config|
|
35
35
|
config.debug_mode = false
|
@@ -38,7 +38,7 @@ Nazrin.configure do |config|
|
|
38
38
|
config.region = ''
|
39
39
|
config.access_key_id = ''
|
40
40
|
config.secret_access_key = ''
|
41
|
-
# currently support kaminari
|
41
|
+
# currently support 'kaminari', 'will_paginate' or 'nazrin'
|
42
42
|
config.pagination = 'kaminari'
|
43
43
|
end
|
44
44
|
```
|
@@ -63,6 +63,11 @@ Post.search(where: :foo, includes: :bar).size(1).start(0).query("(and 'content')
|
|
63
63
|
=> [#<Post id: 1, content: "content">]
|
64
64
|
```
|
65
65
|
|
66
|
+
If you want to use other pagination gem, in your Gemfile
|
67
|
+
```ruby
|
68
|
+
gem 'kaminari' # or 'will_paginate'
|
69
|
+
```
|
70
|
+
|
66
71
|
## Development
|
67
72
|
|
68
73
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -1,8 +1,6 @@
|
|
1
1
|
module Nazrin
|
2
2
|
module ActiveRecord
|
3
3
|
class DataAccessor
|
4
|
-
MAX_LIMIT = 10_000
|
5
|
-
|
6
4
|
def initialize(model, options)
|
7
5
|
@model = model
|
8
6
|
@options = options
|
@@ -13,18 +11,19 @@ module Nazrin
|
|
13
11
|
@client = client
|
14
12
|
|
15
13
|
res = @client.search
|
16
|
-
|
14
|
+
collection = load_all(res.data.hits.hit.map(&:id))
|
15
|
+
|
17
16
|
if @client.parameters[:size] && @client.parameters[:start]
|
18
|
-
|
17
|
+
total_count = res.data.hits.found
|
19
18
|
|
20
19
|
Nazrin::PaginationGenerator.generate(
|
21
|
-
|
22
|
-
current_page:
|
20
|
+
collection,
|
21
|
+
current_page: current_page,
|
23
22
|
per_page: @client.parameters[:size],
|
24
|
-
total_count:
|
25
|
-
last_page:
|
23
|
+
total_count: total_count,
|
24
|
+
last_page: last_page(total_count))
|
26
25
|
else
|
27
|
-
|
26
|
+
collection
|
28
27
|
end
|
29
28
|
end
|
30
29
|
|
@@ -44,54 +43,12 @@ module Nazrin
|
|
44
43
|
|
45
44
|
private
|
46
45
|
|
47
|
-
def
|
48
|
-
res = search_for_set_correct_page
|
49
|
-
total_count = res.data.hits.found
|
50
|
-
if over_max_limit_request?
|
51
|
-
if total_count > MAX_LIMIT
|
52
|
-
@current_page = @last_page = get_last_page(MAX_LIMIT)
|
53
|
-
else
|
54
|
-
@current_page = @last_page = get_last_page(total_count)
|
55
|
-
end
|
56
|
-
@client.start(get_start_position(@current_page))
|
57
|
-
elsif res.data.hits.found > 0 && !res.data.hits.hit.present?
|
58
|
-
@current_page = @last_page = get_last_page(total_count)
|
59
|
-
@client.start(get_start_position(@current_page))
|
60
|
-
else
|
61
|
-
@current_page = (
|
62
|
-
@client.parameters[:start] / @client.parameters[:size].to_f
|
63
|
-
).ceil + 1
|
64
|
-
@last_page = get_last_page(total_count)
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
def search_for_set_correct_page
|
69
|
-
if over_max_limit_request?
|
70
|
-
tmp_start = @client.parameters[:start]
|
71
|
-
res = @client.start(0).search
|
72
|
-
@client.start(tmp_start)
|
73
|
-
else
|
74
|
-
res = @client.search
|
75
|
-
end
|
76
|
-
res
|
77
|
-
end
|
78
|
-
|
79
|
-
def over_max_limit_request?
|
80
|
-
return false unless @client.parameters[:start].present?
|
81
|
-
return false unless @client.parameters[:size].present?
|
82
|
-
if @client.parameters[:start] + @client.parameters[:size] > MAX_LIMIT
|
83
|
-
true
|
84
|
-
else
|
85
|
-
false
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
def get_last_page(total_count)
|
46
|
+
def last_page(total_count)
|
90
47
|
(total_count / @client.parameters[:size].to_f).ceil
|
91
48
|
end
|
92
49
|
|
93
|
-
def
|
94
|
-
(
|
50
|
+
def current_page
|
51
|
+
(@client.parameters[:start] / @client.parameters[:size].to_f).ceil + 1
|
95
52
|
end
|
96
53
|
end
|
97
54
|
end
|
data/lib/nazrin/search_client.rb
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
module Nazrin
|
2
|
+
class SearchClientError < StandardError; end
|
3
|
+
|
2
4
|
class SearchClient
|
5
|
+
CLOUD_SEARCH_MAX_LIMIT = 10_000
|
6
|
+
|
3
7
|
attr_accessor :data_accessor
|
4
8
|
attr_reader :parameters
|
5
9
|
|
@@ -104,6 +108,7 @@ module Nazrin
|
|
104
108
|
end
|
105
109
|
|
106
110
|
def search
|
111
|
+
fail SearchClientError if deep_page?
|
107
112
|
@client.search(@parameters)
|
108
113
|
end
|
109
114
|
|
@@ -114,5 +119,18 @@ module Nazrin
|
|
114
119
|
search
|
115
120
|
end
|
116
121
|
end
|
122
|
+
|
123
|
+
private
|
124
|
+
|
125
|
+
def deep_page?
|
126
|
+
if parameters[:start].present? && parameters[:size].present?
|
127
|
+
return true if parameters[:start] + parameters[:size] > CLOUD_SEARCH_MAX_LIMIT
|
128
|
+
elsif parameters[:start].present?
|
129
|
+
return true if parameters[:start] > CLOUD_SEARCH_MAX_LIMIT
|
130
|
+
elsif parameters[:size].present?
|
131
|
+
return true if parameters[:size] > CLOUD_SEARCH_MAX_LIMIT
|
132
|
+
end
|
133
|
+
false
|
134
|
+
end
|
117
135
|
end
|
118
136
|
end
|
data/lib/nazrin/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nazrin
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tomohiro Suwa
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-12-
|
11
|
+
date: 2015-12-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: aws-sdk
|