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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4ee0cd5215a23ea2f245801bf4802248c2ebca11
4
- data.tar.gz: bac5d1685ff2d1590cfc18780faecd869c382e58
3
+ metadata.gz: 99559990894751321599905a57f89c390caae175
4
+ data.tar.gz: ddaafea268c27f3601084c64e4a324a367c8ab13
5
5
  SHA512:
6
- metadata.gz: 9348020250c407fc833f22a39446bef81965cf3cbe26de457b18fd5e1d07ff73fcbeada5120b0c6e120e1bc369debb2c5e31475e03708ca98b47585d8a76db1f
7
- data.tar.gz: 92c0a290d2e5631eb24dc1b7cd74ed52ba43dd7f5456174648552c584420a2f626f94300adc27155cf120d4b9d3f492b66bee76e4feda08acdd55d8f3e776f06
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 gem
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
- collections = load_all(res.data.hits.hit.map(&:id))
14
+ collection = load_all(res.data.hits.hit.map(&:id))
15
+
17
16
  if @client.parameters[:size] && @client.parameters[:start]
18
- round_correct_page
17
+ total_count = res.data.hits.found
19
18
 
20
19
  Nazrin::PaginationGenerator.generate(
21
- collections,
22
- current_page: @current_page,
20
+ collection,
21
+ current_page: current_page,
23
22
  per_page: @client.parameters[:size],
24
- total_count: res.data.hits.found,
25
- last_page: @last_page)
23
+ total_count: total_count,
24
+ last_page: last_page(total_count))
26
25
  else
27
- collections
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 round_correct_page
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 get_start_position(current_page)
94
- (current_page - 1) * @client.parameters[:size]
50
+ def current_page
51
+ (@client.parameters[:start] / @client.parameters[:size].to_f).ceil + 1
95
52
  end
96
53
  end
97
54
  end
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Nazrin
2
- VERSION = '0.2.0'
2
+ VERSION = '1.0.0'
3
3
  end
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.2.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-19 00:00:00.000000000 Z
11
+ date: 2015-12-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk