guess_paging 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: 6c5c44a20928974b3cc25482d83b9e2aa6743257
4
+ data.tar.gz: bd1a108ceb24a9eacbdaf8c9f4b5e60aa1110b9b
5
+ SHA512:
6
+ metadata.gz: 4af71b6d1298ec1d670702a1fb4d2ea71a412fefc4411a5178c283469a512f0a3e7376f5f05567fb3bd06369821f6c56bc645bd3b51fd7392d18d05d4521db7c
7
+ data.tar.gz: e5cfe2a2960e96a232274fc4acf27d2aacfcd1420064dfea310d1c541d1801f9d2c225a95928b081cff62e1f83829eb8ab0988cc0adde1d4dc18f00fb4f000a0
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.11.0
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at h3.poteto@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in guess_paging.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 h3poteto
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,99 @@
1
+ # guess_paging
2
+
3
+ guess_paging is a pagination library, and this library work wonderfully when you treat many records in RDB.
4
+
5
+ ## Features
6
+ ### Light workload
7
+ General pagination libraries calculate records size to display accurate last page number. But sometimes, count query is too heavy workload for database, if you use heavy SQL Query. In the result, page speed slow.
8
+
9
+ guess_paging calculate and cache records size at the first time. From then on, guess_paging not count records size, and display last page number of suitable size. When user click that last page, once again calculate accurate last page number, and refresh cache.
10
+ When user visit first page and halfway pages, count query is not called.
11
+
12
+ So, guess_paging is light workload for database, because it call count query at the first time and last page only.
13
+
14
+ ## Installation
15
+
16
+ Add this line to your application's Gemfile:
17
+
18
+ ```ruby
19
+ gem 'guess_paging'
20
+ ```
21
+
22
+ And then execute:
23
+
24
+ $ bundle
25
+
26
+ Or install it yourself as:
27
+
28
+ $ gem install guess_paging
29
+
30
+ ## Usage
31
+
32
+ ### Setup redis
33
+ This gem requires `redis`, so please prepare `redis-server` and setup redis in initializer.
34
+
35
+ ```ruby
36
+ GuessPaging::RedisClient.setup do |config|
37
+ config.redis_host = '127.0.0.1'
38
+ config.redis_port = 6379
39
+ end
40
+ ```
41
+
42
+ ### Controllers
43
+ ```ruby
44
+ class RecordsController < ApplicationController
45
+ def search
46
+ @guess = GuessPaging::Paginate.new(
47
+ query: Record.where(category_id: params[:category_id].to_i),
48
+ per_page: 10)
49
+ @guess.guess(
50
+ page_params: params[:page]
51
+ )
52
+ end
53
+ end
54
+ ```
55
+
56
+ ### Views
57
+ ```erb
58
+ <%= paging(@guess) %>
59
+ ```
60
+
61
+ ### Helpers
62
+ - output pagination view
63
+
64
+ ```erb
65
+ <%= paging(@guess) %>
66
+ ```
67
+ - current page number
68
+
69
+ ```ruby
70
+ @guess.current_page
71
+ ```
72
+
73
+ - last page number of suitable size
74
+
75
+ ```ruby
76
+ @guess.max_page
77
+ ```
78
+
79
+ - record number of suitable size.
80
+
81
+ ```ruby
82
+ @guess.count
83
+ ```
84
+
85
+ ## Development
86
+
87
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
88
+
89
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
90
+
91
+ ## Contributing
92
+
93
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/guess_paging. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
94
+
95
+
96
+ ## License
97
+
98
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
99
+
@@ -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 "guess_paging"
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
@@ -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,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'guess_paging/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "guess_paging"
8
+ spec.version = GuessPaging::VERSION
9
+ spec.authors = ["h3poteto"]
10
+ spec.email = ["h3.poteto@gmail.com"]
11
+
12
+ spec.summary = %q{Pagination for many records}
13
+ spec.description = %q{Pagination for rails when to treat many records or big table in RDB.}
14
+ spec.homepage = "https://github.com/h3poteto/guess_paging"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ if spec.respond_to?(:metadata)
20
+ #spec.metadata['allowed_push_host'] = "git@github.com:h3poteto/guess_paging"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.11"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ spec.add_development_dependency "rspec", "~> 3.0"
33
+
34
+ spec.add_dependency "redis", "~> 3.2"
35
+ end
@@ -0,0 +1,4 @@
1
+ require "guess_paging/version"
2
+ require "guess_paging/paginate"
3
+ require "guess_paging/engine"
4
+ require "guess_paging/redis_client"
@@ -0,0 +1,11 @@
1
+ require 'guess_paging/guess_helper'
2
+ module GuessPaging
3
+ class Engine < ::Rails::Engine
4
+ isolate_namespace GuessPaging
5
+ initializer 'guess_paging.action_view_helpers' do
6
+ ActiveSupport.on_load :action_view do
7
+ include GuessPaging::GuessHelper
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,91 @@
1
+ module GuessPaging
2
+ module GuessHelper
3
+ def paging(guess)
4
+ hash = request.params
5
+ hash.delete(:controller)
6
+ hash.delete(:action)
7
+ div = content_tag(:div, class: 'guess-paging') do
8
+ content_tag(:ul) do
9
+ if guess.current_page < 5
10
+ first_paging(guess, hash)
11
+ elsif guess.current_page > guess.max_page - 4
12
+ last_paging(guess, hash)
13
+ else
14
+ middle_paging(guess, hash)
15
+ end
16
+ end
17
+ end
18
+ div.html_safe
19
+ end
20
+
21
+ def first_paging(guess, hash)
22
+ li = ""
23
+ (1..4).each do |i|
24
+ li << if i == guess.current_page
25
+ content_tag(:li, class: 'active') do
26
+ "#{i}"
27
+ end
28
+ else
29
+ content_tag(:li) do
30
+ link_to("#{i}", request.path + "?" + hash.merge(page: i).to_param)
31
+ end
32
+ end
33
+ end
34
+ li << content_tag(:li, class: 'truncate') do
35
+ "..."
36
+ end
37
+ li << content_tag(:li, class: 'last') do
38
+ link_to("#{guess.max_page}", request.path + "?" + hash.merge(page: guess.max_page).to_param)
39
+ end
40
+ li.html_safe
41
+ end
42
+
43
+ def last_paging(guess, hash)
44
+ li = content_tag(:li) do
45
+ link_to("1", request.path + "?" + hash.merge(page: 1).to_param)
46
+ end
47
+ li << content_tag(:li, class: 'truncate') do
48
+ "..."
49
+ end
50
+ ((guess.max_page - 3)..guess.max_page).each do |i|
51
+ li << if i == guess.current_page
52
+ content_tag(:li, class: 'active') do
53
+ "#{i}"
54
+ end
55
+ else
56
+ content_tag(:li) do
57
+ link_to("#{i}", request.path + "?" + hash.merge(page: i).to_param)
58
+ end
59
+ end
60
+ end
61
+ li.html_safe
62
+ end
63
+
64
+ def middle_paging(guess, hash)
65
+ li = content_tag(:li) do
66
+ link_to("1", request.path + "?" + hash.merge(page: 1).to_param)
67
+ end
68
+ li << content_tag(:li, class: 'truncate') do
69
+ "..."
70
+ end
71
+ ((guess.current_page - 2)..(guess.current_page + 2)).each do |i|
72
+ li << if guess.current_page == i
73
+ content_tag(:li, class: 'active') do
74
+ "#{i}"
75
+ end
76
+ else
77
+ content_tag(:li) do
78
+ link_to("#{i}", request.path + "?" + hash.merge(page: i).to_param)
79
+ end
80
+ end
81
+ end
82
+ li << content_tag(:li) do
83
+ "..."
84
+ end
85
+ li << content_tag(:li) do
86
+ link_to("#{guess.max_page}", request.path + "?" + hash.merge(page: guess.max_page).to_param)
87
+ end
88
+ li.html_safe
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,54 @@
1
+ module GuessPaging
2
+ class Paginate
3
+ attr_reader :current_page, :max_page, :count, :query, :records
4
+
5
+ def initialize(query: nil, per_page: 10, essential: 3)
6
+ @query = query
7
+ @key = Digest::MD5.hexdigest(query.all.to_sql.to_s)
8
+ @per_page = per_page
9
+ @essential = essential
10
+ end
11
+
12
+ def guess(page_params: nil)
13
+ get_max_page
14
+ if page(page_params).length < @per_page
15
+ last_page = @query.count / @per_page + 1
16
+ GuessPaging::RedisClient.set(@key, last_page) if last_page != get_max_page
17
+ @records = @query.limit(@per_page).offset(@per_page * (last_page - 1))
18
+ @current_page = last_page
19
+ @max_page = last_page
20
+ @count = @query.count
21
+ else
22
+ @current_page = get_page(page_params)
23
+ @max_page = get_max_page
24
+ @count = @max_page * @per_page
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def get_page(page_params)
31
+ page_params ? page_params.to_i : 1
32
+ end
33
+
34
+ def page(page_params)
35
+ @records = @query.limit(@per_page).offset(@per_page * (get_page(page_params) - 1))
36
+ end
37
+
38
+ def get_max_page
39
+ max = GuessPaging::RedisClient.get(@key).to_i
40
+ if max.blank? || max.zero?
41
+ all = @query.count
42
+ max = all % @per_page == 0 ? all / @per_page : (all / @per_page + 1)
43
+ GuessPaging::RedisClient.set(@key, max)
44
+ end
45
+
46
+ count_digit = max.to_s.length
47
+ if count_digit > @essential
48
+ i = max.to_f / (10 ** (count_digit - @essential))
49
+ last_page = (i.ceil * 10 ** (count_digit - @essential)).to_i
50
+ end
51
+ last_page ||= max
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,28 @@
1
+ require 'redis'
2
+
3
+ module GuessPaging
4
+ class RedisClient
5
+ cattr_accessor :redis_host, :redis_port
6
+ cattr_accessor :redis
7
+ @@redis_host = nil
8
+ @@redis_port = nil
9
+ @@redis = nil
10
+ def self.setup
11
+ yield self
12
+ end
13
+
14
+ def self.config
15
+ @@redis = Redis.new(host: @@redis_host, port: @@redis_port)
16
+ end
17
+
18
+ def self.set(key, value)
19
+ config unless @@redis
20
+ @@redis.set(key, value)
21
+ end
22
+
23
+ def self.get(key)
24
+ config unless @@redis
25
+ @@redis.get(key)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,3 @@
1
+ module GuessPaging
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,27 @@
1
+ .guess-paging {
2
+ width: 100%;
3
+ text-align: center;
4
+ ul {
5
+ list-style-type: none;
6
+ padding: 8px;
7
+ li {
8
+ font-size: 11px;
9
+ display: inline-block;
10
+ padding: 6px;
11
+ border-left: 1px solid rgb(180, 180, 180);
12
+ border-top: 1px solid rgb(180, 180, 180);
13
+ border-bottom: 1px solid rgb(180, 180, 180);
14
+ a, a:hover, a:visited {
15
+ color: rgb(66, 139, 202);
16
+ text-decoration: none;
17
+ }
18
+ }
19
+ li.active {
20
+ color: rgb(255, 255, 255);
21
+ background: rgb(66, 139, 202);
22
+ }
23
+ li:last-child {
24
+ border-right: 1px solid rgb(180, 180, 180);
25
+ }
26
+ }
27
+ }
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: guess_paging
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - h3poteto
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-12-17 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.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
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: redis
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.2'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.2'
69
+ description: Pagination for rails when to treat many records or big table in RDB.
70
+ email:
71
+ - h3.poteto@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - CODE_OF_CONDUCT.md
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - guess_paging.gemspec
87
+ - lib/guess_paging.rb
88
+ - lib/guess_paging/engine.rb
89
+ - lib/guess_paging/guess_helper.rb
90
+ - lib/guess_paging/paginate.rb
91
+ - lib/guess_paging/redis_client.rb
92
+ - lib/guess_paging/version.rb
93
+ - vendor/assets/stylesheets/guess_paging/index.css.scss
94
+ homepage: https://github.com/h3poteto/guess_paging
95
+ licenses:
96
+ - MIT
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 2.5.1
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: Pagination for many records
118
+ test_files: []