multi_model_paginator 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3d47fb0022a19dd05bec4aa419dd669b210ee6f8
4
+ data.tar.gz: 6dca13c4d02ea93341286fbb688874a85841f8c8
5
+ SHA512:
6
+ metadata.gz: 936924e6004a814da03c4c7903ed88de3af2e0f3aa587cfbef93ade9e629b57f1d0602bee0e70007dc02919f4ca4b5998a290a3ae1826f5f47b7ec1dfcc1df60
7
+ data.tar.gz: a01b9142f61161eb4943cf046b81d423dee366164cebc91373bda4c6e9db498198c980608f7f1f3f0802863d157a0e3033b04d34df4a58d7c524ccd0f96ec1e1
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.16.1
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in multi_model_paginator.gemspec
6
+ gemspec
@@ -0,0 +1,149 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ multi_model_paginator (0.1.0)
5
+ rails (>= 5)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actioncable (5.2.0)
11
+ actionpack (= 5.2.0)
12
+ nio4r (~> 2.0)
13
+ websocket-driver (>= 0.6.1)
14
+ actionmailer (5.2.0)
15
+ actionpack (= 5.2.0)
16
+ actionview (= 5.2.0)
17
+ activejob (= 5.2.0)
18
+ mail (~> 2.5, >= 2.5.4)
19
+ rails-dom-testing (~> 2.0)
20
+ actionpack (5.2.0)
21
+ actionview (= 5.2.0)
22
+ activesupport (= 5.2.0)
23
+ rack (~> 2.0)
24
+ rack-test (>= 0.6.3)
25
+ rails-dom-testing (~> 2.0)
26
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
27
+ actionview (5.2.0)
28
+ activesupport (= 5.2.0)
29
+ builder (~> 3.1)
30
+ erubi (~> 1.4)
31
+ rails-dom-testing (~> 2.0)
32
+ rails-html-sanitizer (~> 1.0, >= 1.0.3)
33
+ activejob (5.2.0)
34
+ activesupport (= 5.2.0)
35
+ globalid (>= 0.3.6)
36
+ activemodel (5.2.0)
37
+ activesupport (= 5.2.0)
38
+ activerecord (5.2.0)
39
+ activemodel (= 5.2.0)
40
+ activesupport (= 5.2.0)
41
+ arel (>= 9.0)
42
+ activestorage (5.2.0)
43
+ actionpack (= 5.2.0)
44
+ activerecord (= 5.2.0)
45
+ marcel (~> 0.3.1)
46
+ activesupport (5.2.0)
47
+ concurrent-ruby (~> 1.0, >= 1.0.2)
48
+ i18n (>= 0.7, < 2)
49
+ minitest (~> 5.1)
50
+ tzinfo (~> 1.1)
51
+ arel (9.0.0)
52
+ builder (3.2.3)
53
+ coderay (1.1.2)
54
+ concurrent-ruby (1.0.5)
55
+ crass (1.0.4)
56
+ diff-lcs (1.3)
57
+ erubi (1.7.1)
58
+ globalid (0.4.1)
59
+ activesupport (>= 4.2.0)
60
+ i18n (1.0.1)
61
+ concurrent-ruby (~> 1.0)
62
+ loofah (2.2.2)
63
+ crass (~> 1.0.2)
64
+ nokogiri (>= 1.5.9)
65
+ mail (2.7.0)
66
+ mini_mime (>= 0.1.1)
67
+ marcel (0.3.2)
68
+ mimemagic (~> 0.3.2)
69
+ method_source (0.9.0)
70
+ mimemagic (0.3.2)
71
+ mini_mime (1.0.0)
72
+ mini_portile2 (2.3.0)
73
+ minitest (5.11.3)
74
+ nio4r (2.3.1)
75
+ nokogiri (1.8.2)
76
+ mini_portile2 (~> 2.3.0)
77
+ pry (0.11.3)
78
+ coderay (~> 1.1.0)
79
+ method_source (~> 0.9.0)
80
+ rack (2.0.5)
81
+ rack-test (1.0.0)
82
+ rack (>= 1.0, < 3)
83
+ rails (5.2.0)
84
+ actioncable (= 5.2.0)
85
+ actionmailer (= 5.2.0)
86
+ actionpack (= 5.2.0)
87
+ actionview (= 5.2.0)
88
+ activejob (= 5.2.0)
89
+ activemodel (= 5.2.0)
90
+ activerecord (= 5.2.0)
91
+ activestorage (= 5.2.0)
92
+ activesupport (= 5.2.0)
93
+ bundler (>= 1.3.0)
94
+ railties (= 5.2.0)
95
+ sprockets-rails (>= 2.0.0)
96
+ rails-dom-testing (2.0.3)
97
+ activesupport (>= 4.2.0)
98
+ nokogiri (>= 1.6)
99
+ rails-html-sanitizer (1.0.4)
100
+ loofah (~> 2.2, >= 2.2.2)
101
+ railties (5.2.0)
102
+ actionpack (= 5.2.0)
103
+ activesupport (= 5.2.0)
104
+ method_source
105
+ rake (>= 0.8.7)
106
+ thor (>= 0.18.1, < 2.0)
107
+ rake (10.5.0)
108
+ rspec (3.7.0)
109
+ rspec-core (~> 3.7.0)
110
+ rspec-expectations (~> 3.7.0)
111
+ rspec-mocks (~> 3.7.0)
112
+ rspec-core (3.7.1)
113
+ rspec-support (~> 3.7.0)
114
+ rspec-expectations (3.7.0)
115
+ diff-lcs (>= 1.2.0, < 2.0)
116
+ rspec-support (~> 3.7.0)
117
+ rspec-mocks (3.7.0)
118
+ diff-lcs (>= 1.2.0, < 2.0)
119
+ rspec-support (~> 3.7.0)
120
+ rspec-support (3.7.1)
121
+ sprockets (3.7.1)
122
+ concurrent-ruby (~> 1.0)
123
+ rack (> 1, < 3)
124
+ sprockets-rails (3.2.1)
125
+ actionpack (>= 4.0)
126
+ activesupport (>= 4.0)
127
+ sprockets (>= 3.0.0)
128
+ sqlite3 (1.3.13)
129
+ thor (0.20.0)
130
+ thread_safe (0.3.6)
131
+ tzinfo (1.2.5)
132
+ thread_safe (~> 0.1)
133
+ websocket-driver (0.7.0)
134
+ websocket-extensions (>= 0.1.0)
135
+ websocket-extensions (0.1.3)
136
+
137
+ PLATFORMS
138
+ ruby
139
+
140
+ DEPENDENCIES
141
+ bundler (~> 1.16)
142
+ multi_model_paginator!
143
+ pry
144
+ rake (~> 10.0)
145
+ rspec (~> 3.0)
146
+ sqlite3
147
+
148
+ BUNDLED WITH
149
+ 1.16.1
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 jiikko
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,49 @@
1
+ # MultiModelPaginator
2
+ * 複数のモデルでページネーションを行うgemです
3
+ * 追加されたクエリ毎のカウントをとっていい感じにページネーションします
4
+
5
+ ## Dependencies
6
+ * ActiveRecord
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'multi_model_paginator', git: 'https://github.com/jiikko/multi-model-paginator'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install multi_model_paginator
23
+
24
+ ## Usage
25
+
26
+ ```ruby
27
+ Account.count # => 3
28
+ Item.count # => 5
29
+
30
+ paginator = MultiModelPaginator.new(per: 2, page: 0)
31
+ paginator.add(Account.all)
32
+ paginator.add(Item.all)
33
+ # D, [2018-05-29T22:28:55.104813 #7189] DEBUG -- : CACHE (0.0ms) SELECT COUNT(*) FROM "accounts"
34
+ # D, [2018-05-29T22:28:55.105422 #7189] DEBUG -- : CACHE Account Load (0.0ms) SELECT "accounts".* FROM "accounts" LIMIT 2 OFFSET 0
35
+ paginator.result.map(&:class).map(&:to_s) # =>["Account", "Account"]
36
+
37
+ paginator = MultiModelPaginator.new(per: 2, page: 1)
38
+ paginator.add(Account.all)
39
+ paginator.add(Item.all)
40
+ # D, [2018-05-29T22:29:37.069335 #7189] DEBUG -- : CACHE (0.0ms) SELECT COUNT(*) FROM "accounts"
41
+ # D, [2018-05-29T22:29:37.069765 #7189] DEBUG -- : CACHE Account Load (0.0ms) SELECT "accounts".* FROM "accounts" LIMIT 2 OFFSET 2
42
+ # D, [2018-05-29T22:29:37.070255 #7189] DEBUG -- : CACHE (0.0ms) SELECT COUNT(*) FROM "items"
43
+ # D, [2018-05-29T22:29:37.070821 #7189] DEBUG -- : CACHE Item Load (0.0ms) SELECT "items".* FROM "items" LIMIT 2 OFFSET 0
44
+ paginator.result.map(&:class).map(&:to_s) # => ["Account", "Item"]
45
+ ```
46
+
47
+ ## License
48
+
49
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "multi_model_paginator"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,73 @@
1
+ require "multi_model_paginator/version"
2
+
3
+ module MultiModelPaginator
4
+ class QueryStruct
5
+ attr_reader :query
6
+
7
+ def initialize(query, select, count)
8
+ @query = query
9
+ @select = select
10
+ @count = count
11
+ end
12
+
13
+ def with_select
14
+ @query.select(@select)
15
+ end
16
+
17
+ def count
18
+ @cached_count ||=
19
+ if @count.nil?
20
+ @query.count
21
+ else
22
+ @count.call
23
+ end
24
+ end
25
+ end
26
+
27
+ class Builder
28
+ def initialize(per, page)
29
+ @query_list = []
30
+ @per = per
31
+ @page = page.to_i
32
+ @list = []
33
+ end
34
+
35
+ def add(query, select: nil, count: nil)
36
+ @query_list.push(QueryStruct.new(query, select, count))
37
+ end
38
+
39
+ def count_all
40
+ @query_list.map(&:count).sum
41
+ end
42
+
43
+ def result
44
+ remain = @per
45
+ position = @page * @per
46
+ @query_list.reduce([]) do |accumulator, query_struct|
47
+ prev_total_count = @query_list.reduce(0) { |a, q| q == query_struct ? (break(a)) : (a += q.count) }
48
+ current_range = (prev_total_count...(prev_total_count + query_struct.count))
49
+ if !current_range.include?(position)
50
+ next(accumulator)
51
+ end
52
+ prev_query_offset = current_range.first
53
+ # テーブルt1に6レコード, テーブルt2に10レコードある場合、per:10で読んだ時にpage:2はテーブルt2の5個目が先頭になる
54
+ # |t1:......|t2:....[.].......|
55
+ offset = (@page * @per) - prev_query_offset
56
+ offset = 0 if offset.negative?
57
+ list = query_struct.with_select.limit(@per).offset(offset).to_a.first(remain)
58
+ accumulator.concat(list)
59
+ remain = remain - list.size
60
+ if remain == 0
61
+ break(accumulator)
62
+ else
63
+ position = prev_total_count + query_struct.count
64
+ next(accumulator)
65
+ end
66
+ end
67
+ end
68
+ end
69
+
70
+ def self.new(per: , page: )
71
+ Builder.new(per, page)
72
+ end
73
+ end
@@ -0,0 +1,3 @@
1
+ module MultiModelPaginator
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,31 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "multi_model_paginator/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "multi_model_paginator"
8
+ spec.version = MultiModelPaginator::VERSION
9
+ spec.authors = ["jiikko"]
10
+ spec.email = ["n905i.1214@gmail.com"]
11
+
12
+ spec.summary = %q{multi_models_paginator}
13
+ spec.description = spec.summary
14
+ spec.homepage = "https://github.com/ubiregiinc/multi-model-paginator"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.16"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ spec.add_development_dependency "sqlite3"
28
+ spec.add_development_dependency "pry"
29
+
30
+ spec.add_dependency "rails", ">= 5"
31
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: multi_model_paginator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - jiikko
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-09-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.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: pry
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: rails
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '5'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '5'
97
+ description: multi_models_paginator
98
+ email:
99
+ - n905i.1214@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - Gemfile
108
+ - Gemfile.lock
109
+ - LICENSE.txt
110
+ - README.md
111
+ - Rakefile
112
+ - bin/console
113
+ - bin/setup
114
+ - lib/multi_model_paginator.rb
115
+ - lib/multi_model_paginator/version.rb
116
+ - multi_model_paginator.gemspec
117
+ homepage: https://github.com/ubiregiinc/multi-model-paginator
118
+ licenses:
119
+ - MIT
120
+ metadata: {}
121
+ post_install_message:
122
+ rdoc_options: []
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubyforge_project:
137
+ rubygems_version: 2.6.11
138
+ signing_key:
139
+ specification_version: 4
140
+ summary: multi_models_paginator
141
+ test_files: []