actionpager 0.0.5 → 0.0.6

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
  SHA256:
3
- metadata.gz: f802c6a8d4b24b2acdef2cd58992d777ea2474ca6c87f32762f5d6a0b5ecd78b
4
- data.tar.gz: 26306561adbdcf858e9ee23205806c157380a17fa3295dbf3d0ec0083013f2e1
3
+ metadata.gz: 876f031e85f180e04e799d237f79330a3cc0eb31a7ffde45778a46c2f1077128
4
+ data.tar.gz: 454bc22c406a1e108feb8b3792e187f8a6d72c25d417d7c103a5323b88a8b6f5
5
5
  SHA512:
6
- metadata.gz: a684cc104adbff58048cb94eab079699eb33c555e75cf82fef2dfb9ef53f6514994b1383acfb8da4a12b6f66c6e38d0a0d4819d46345762ca672455e94435065
7
- data.tar.gz: edbf8cd64f0ef6f6bd42b6403dcc63adfaaad97a12552ac64322f40eb1cadcd55fbc0da5d3e0f856048b8bb37db4cbb1227b4aa58c393b7e62272e22847e1b8a
6
+ metadata.gz: 0f216700001f0bd47fd43101bca418ab239407d0805159973abe37516d2d315aea19eb53948bc9a71486ed25fb31c9ac2449af5e199fccf7684e324a958611f0
7
+ data.tar.gz: 8b60339780300597182e3b8d041315ed8e05d57d3098d220ec5949fb1be3b0defa6167402ab404eb1b930c92fa48195b8fa503264ca5f7640a8ee426cdd00394
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- actionpager (0.0.3)
4
+ actionpager (0.0.6)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.ja.md ADDED
@@ -0,0 +1,72 @@
1
+ # Action Pager
2
+
3
+ コレクション(Array, ActiveRecord::Relationなど)で使用できるシンプルで軽量なページネーターです。
4
+
5
+ ## 特徴
6
+
7
+ - シンプルな`pager`オブジェクトがページネーションで使用されます。
8
+ - 元のコレクションを汚染しません。
9
+ - 他のGem (Kaminari, Will Paginateなど)に比べるとメモリ消費が非常に少ないです。
10
+ - カスタマイズが簡単です。
11
+ - ページネーターのビューとヘルパのファイルがアプリ内に生成されます。
12
+ - 用途に応じてカスタマイズできます。
13
+
14
+ ## インストール
15
+
16
+ アプリケーションのGemfile以下の行を追加します。
17
+
18
+ ```ruby
19
+ gem "actionpager"
20
+ ```
21
+
22
+ 以下を実行します。
23
+
24
+ $ bundle
25
+
26
+ または、以下のようにインストールします。
27
+
28
+ $ gem install actionpager
29
+
30
+ Railsで使用するには以下のコマンドを実行します
31
+
32
+ $ rails g action_pager:install
33
+
34
+ 2つのファイルが生成され、カスタマイズすることができます。
35
+
36
+ - `app/helpers/action_pager/pagination_helper.rb`
37
+ - `app/views/action_pager/_pager.html.erb`
38
+
39
+ ## 使い方
40
+
41
+ シンプルな配列で使う場合
42
+
43
+ ```ruby
44
+ collection = (1..128).to_a
45
+ pager = ActionPager::Pager.new(collection, page: 3, per: 10)
46
+
47
+ pager.current_collection # => [21, ..., 30]
48
+ ```
49
+
50
+ ActiveRecordのコレクションで使う場合
51
+
52
+ ```ruby
53
+ resources = Book.recent_published
54
+ @pager = ActionPager::Pager.new(resources, page: 2, per: 25)
55
+ @books = pager.current_collection
56
+ ```
57
+
58
+ Railsのビューでページャーを表示するには、以下のようにコードを記述します。
59
+
60
+ ```erb
61
+ <%= render_pager @pager %>
62
+ ```
63
+
64
+
65
+
66
+ ## License
67
+
68
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
69
+
70
+ ## Code of Conduct
71
+
72
+ Everyone interacting in the Actionpager project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/actionpager/blob/master/CODE_OF_CONDUCT.md).
data/README.md CHANGED
@@ -1,6 +1,15 @@
1
1
  # Action Pager
2
2
 
3
- Simple and lightweight paginator for collections (Array, ActiveRecord::Relation etc.)
3
+ Simple, lightweight and easy to customize paginator for collections (Array, ActiveRecord::Relation etc.)
4
+
5
+ # Features
6
+
7
+ - A simple `pager` object is used for pagination.
8
+ - It does not pollute the original collection.
9
+ - Consumes less memory than other gems (i.e. Kaminari, Will Paginate etc.).
10
+ - Easy to customize.
11
+ - Paginator view and helper files are generated in your app.
12
+ - You can customize for your own purpose.
4
13
 
5
14
  ## Installation
6
15
 
@@ -18,9 +27,40 @@ Or install it yourself as:
18
27
 
19
28
  $ gem install actionpager
20
29
 
30
+ Use with Rails
31
+
32
+ $ rails g action_pager:install
33
+
34
+ It generates two files and you can customize.
35
+
36
+ - `app/helpers/action_pager/pagination_helper.rb`
37
+ - `app/views/action_pager/_pager.html.erb`
38
+
21
39
  ## Usage
22
40
 
23
- TODO: Write usage instructions here
41
+ Use with a simple array:
42
+
43
+ ```ruby
44
+ collection = (1..128).to_a
45
+ pager = ActionPager::Pager.new(collection, page: 3, per: 10)
46
+
47
+ pager.current_collection # => [21, ..., 30]
48
+ ```
49
+
50
+ Use with an ActiveRecord collection
51
+
52
+ ```ruby
53
+ resources = Book.recent_published
54
+ @pager = ActionPager::Pager.new(resources, page: 2, per: 25)
55
+ @books = pager.current_collection
56
+ ```
57
+
58
+ To show the pager in your Rails app's view, add code like below:
59
+
60
+ ```erb
61
+ <%= render_pager @pager %>
62
+ ```
63
+
24
64
 
25
65
  ## License
26
66
 
@@ -20,7 +20,8 @@ module ActionPager
20
20
  current_collection
21
21
  end
22
22
 
23
- def current_collection
23
+ def collection_at(page)
24
+ offset = offset_at(page)
24
25
  if collection.is_a?(Array)
25
26
  collection.drop(offset).first(per_page)
26
27
  else # for ActiveRecord::Relation and other OR mapper collections
@@ -28,8 +29,48 @@ module ActionPager
28
29
  end
29
30
  end
30
31
 
31
- def offset
32
- (current_page - 1) * per_page
32
+ def current_collection
33
+ collection_at(current_page)
34
+ end
35
+
36
+ def first_collection
37
+ collection_at(first_page)
38
+ end
39
+
40
+ def last_collection
41
+ collection_at(last_page)
42
+ end
43
+
44
+ def prev_collection
45
+ collection_at(prev_page)
46
+ end
47
+
48
+ def next_collection
49
+ collection_at(next_page)
50
+ end
51
+
52
+ def offset_at(page)
53
+ (page - 1) * per_page
54
+ end
55
+
56
+ def current_offset
57
+ offset_at(current_page)
58
+ end
59
+
60
+ def first_offset
61
+ offset_at(first_page)
62
+ end
63
+
64
+ def last_offset
65
+ offset_at(last_page)
66
+ end
67
+
68
+ def prev_offset
69
+ offset_at(prev_page)
70
+ end
71
+
72
+ def next_offset
73
+ offset_at(next_page)
33
74
  end
34
75
 
35
76
  def collection_count
@@ -0,0 +1,14 @@
1
+ module ActionPager
2
+ module Pagination
3
+ DEFAULTS = { page: 1, per: 5 }.freeze
4
+
5
+ def pager_for(collection, options={})
6
+ opts = DEFAULTS.merge(options.reject { |_k, v| v.to_i.zero? })
7
+ ActionPager::Pager.new(
8
+ collection,
9
+ page: opts[:page].to_i,
10
+ per: opts[:per].to_i
11
+ )
12
+ end
13
+ end
14
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActionPager
4
- VERSION = "0.0.5"
4
+ VERSION = "0.0.6"
5
5
  end
data/lib/actionpager.rb CHANGED
@@ -2,3 +2,4 @@
2
2
 
3
3
  require "action_pager/version"
4
4
  require "action_pager/pager"
5
+ require "action_pager/pagination"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: actionpager
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Hashimoto
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-11-08 00:00:00.000000000 Z
11
+ date: 2018-11-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -65,10 +65,12 @@ files:
65
65
  - Gemfile
66
66
  - Gemfile.lock
67
67
  - LICENSE
68
+ - README.ja.md
68
69
  - README.md
69
70
  - Rakefile
70
71
  - actionpager.gemspec
71
72
  - lib/action_pager/pager.rb
73
+ - lib/action_pager/pagination.rb
72
74
  - lib/action_pager/version.rb
73
75
  - lib/actionpager.rb
74
76
  - lib/generators/action_pager/install_generator.rb