cursor_page 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 20c87b6d5e00835d8b3ea037f05717811b32cdaa
4
+ data.tar.gz: 1c5b2ff7f2f945450e92414ce4a8a3a09a85b726
5
+ SHA512:
6
+ metadata.gz: f9dc086329acef7f22b5b83c3acf5146f034f09daf87594116bd119f7ae257d3086215a8270e21c94bb2acbf1ff2cda58f99b741a06205e09a9ee64273bf60ff
7
+ data.tar.gz: 86de021ad49b40920f5ab52fb697ea1f43e6180b9099df92d0c19b65d44e5ad24aae079724fb3ac6b2c6dc2919f156311f1e972dba4ca1f792875d7d4f2f0df8
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cursor_page.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,71 @@
1
+ # CursorPage
2
+
3
+ Cursor based pagination gem.
4
+
5
+ 무한 스크롤 UI에서 흔히 사용하는 커서 기반의 페이징 플러그인입니다.
6
+
7
+ 이 Gem은 [루비 대림절 달력](https://ruby-korea.github.io/advent-calendar/)을 위해 제작하였습니다. 구현 내용을 설명하는 글을 작성하려고 했지만, 소스가 너무 간단한 관계로 작성할 내용이 없어져버렸습니다;;;
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'cursor_page'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install cursor_page
24
+
25
+ ## Usage
26
+
27
+ ### The `cursor_page` Scope
28
+
29
+ To fetch the posts that ID value less then 2.
30
+
31
+ ```ruby
32
+ Post.cursor_page(before: 2)
33
+ ```
34
+
35
+ To fetch the 10 posts that ID value greater then 10.
36
+
37
+ ```ruby
38
+ Post.cursor_page(after: 10).limit(10)
39
+ ```
40
+
41
+ To fetch the posts that ts value greater then 1482495565.
42
+
43
+ ```ruby
44
+ Post.cursor_page(key: 'ts', after: 1482495565)
45
+ ```
46
+
47
+ ### The `cursor` Value
48
+
49
+ Get current cursor value
50
+
51
+ ```ruby
52
+ @posts = Post.cursor_page(before: 2)
53
+ @posts.cursor # {:key=>"id", :before=>2, :after=>nil}
54
+ ```
55
+
56
+ ### `to_cursor_param` method
57
+
58
+ Get cursor parameters for response json
59
+
60
+ ```ruby
61
+ @posts = Post.cursor_page(before: 3)
62
+ @posts.to_cursor_param # {:before=>"MQ==", :after=>"Mg=="}
63
+ ```
64
+
65
+ ## Todo
66
+
67
+ - [ ] Add test code
68
+
69
+ ## Contributing
70
+
71
+ Bug reports and pull requests are welcome on GitHub at https://github.com/remotty/cursor_page.
data/Rakefile ADDED
@@ -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
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "cursor_page"
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
data/bin/setup ADDED
@@ -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,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cursor_page/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cursor_page"
8
+ spec.version = CursorPage::VERSION
9
+ spec.authors = ["subicura"]
10
+ spec.email = ["subicura@subicura.com"]
11
+
12
+ spec.summary = %q{A cursor based pagination plugin for Rails 4+}
13
+ spec.description = %q{CursorPage is a cursor based pagination plugin}
14
+ spec.homepage = "https://github.com/remotty/cursor_page"
15
+
16
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
17
+ # delete this section to allow pushing this gem to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
20
+ else
21
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
22
+ end
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_dependency 'activesupport', '>= 4.1.0'
30
+
31
+ spec.add_development_dependency "bundler", "~> 1.11"
32
+ spec.add_development_dependency "rake", "~> 10.0"
33
+ spec.add_development_dependency "rspec", "~> 3.0"
34
+ end
@@ -0,0 +1,2 @@
1
+ require "cursor_page/version"
2
+ require 'cursor_page/activerecord'
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+ require 'active_support/lazy_load_hooks'
3
+
4
+ ActiveSupport.on_load :active_record do
5
+ require 'cursor_page/activerecord/extension'
6
+ ::ActiveRecord::Base.send :include, CursorPage::ActiveRecordExtension
7
+ ::ActiveRecord::Relation.send :include, CursorPage::ActiveRecordRelationExtension
8
+ end
@@ -0,0 +1,62 @@
1
+ module CursorPage
2
+ module ActiveRecordExtension
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+ def cursor_page(options = {})
7
+ key = options.fetch(:key, self.primary_key)
8
+ before = options.fetch(:before, nil)
9
+ after = options.fetch(:after, nil)
10
+
11
+ scope = if before
12
+ where("#{key} < ?", before).order(Hash[key, :desc])
13
+ elsif after
14
+ where("#{key} > ?", after).order(Hash[key, :asc])
15
+ else
16
+ order("#{key} desc")
17
+ end
18
+
19
+ scope.cursor = {
20
+ key: key,
21
+ before: before,
22
+ after: after
23
+ }
24
+ scope
25
+ end
26
+ end
27
+ end
28
+
29
+ module ActiveRecordRelationExtension
30
+ extend ActiveSupport::Concern
31
+
32
+ included do
33
+ attr_accessor :cursor
34
+ end
35
+
36
+ def reset #:nodoc:
37
+ @cursor = nil
38
+ super
39
+ end
40
+
41
+ def to_cursor_param
42
+ cursor_param = {}
43
+
44
+ if @cursor[:after].nil?
45
+ cursor_param = {
46
+ before: Base64.strict_encode64(last.try(@cursor[:key]).to_s),
47
+ after: Base64.strict_encode64(first.try(@cursor[:key]).to_s)
48
+ }
49
+ else
50
+ cursor_param = {
51
+ before: Base64.strict_encode64(first.try(@cursor[:key]).to_s),
52
+ after: Base64.strict_encode64(last.try(@cursor[:key]).to_s)
53
+ }
54
+ end
55
+
56
+ cursor_param
57
+ end
58
+ end
59
+ end
60
+
61
+ ActiveRecord::Base.send(:include, CursorPage::ActiveRecordExtension)
62
+ ActiveRecord::Relation.send(:include, CursorPage::ActiveRecordRelationExtension)
@@ -0,0 +1,3 @@
1
+ module CursorPage
2
+ VERSION = "0.1.1"
3
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cursor_page
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - subicura
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-12-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 4.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 4.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.11'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.11'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: CursorPage is a cursor based pagination plugin
70
+ email:
71
+ - subicura@subicura.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - README.md
81
+ - Rakefile
82
+ - bin/console
83
+ - bin/setup
84
+ - cursor_page.gemspec
85
+ - lib/cursor_page.rb
86
+ - lib/cursor_page/activerecord.rb
87
+ - lib/cursor_page/activerecord/extension.rb
88
+ - lib/cursor_page/version.rb
89
+ homepage: https://github.com/remotty/cursor_page
90
+ licenses: []
91
+ metadata:
92
+ allowed_push_host: https://rubygems.org
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.5.1
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: A cursor based pagination plugin for Rails 4+
113
+ test_files: []