flame-pagination 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
+ SHA256:
3
+ metadata.gz: bd575b8de5fecc260a7584b44a8eceaee33f3b06605ae884894307bfdb280f92
4
+ data.tar.gz: a19fee994fa1d956e2569c49e9871251d7fec20d02f8e964bf7746a7e6803a7e
5
+ SHA512:
6
+ metadata.gz: ebf51d23f547f3324fa880451631f9e5cd1456c82ee9ab381252de0b0fcf69bdda324f17d889b9273a8dc3a7a25211e81d166ee338251a7ebe0055052794e8a6
7
+ data.tar.gz: 9c9b274da5328ac8652f33661c116855a661442e9e3a1a71efcbfc053c9253ee4201920a7c67cc2822b280a93ca264b98389a31aaa04aa656065b0de6edd40b1
@@ -0,0 +1,7 @@
1
+ # Changelog
2
+
3
+ ## master (unreleased)
4
+
5
+ ## 0.1.0 (2020-07-10)
6
+
7
+ * Initial commit
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Alexander Popov
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,101 @@
1
+ # Flame Pagination
2
+
3
+ ![Cirrus CI - Base Branch Build Status](https://img.shields.io/cirrus/github/AlexWayfer/flame-pagination?style=flat-square)
4
+ [![Codecov branch](https://img.shields.io/codecov/c/github/AlexWayfer/flame-pagination/master.svg?style=flat-square)](https://codecov.io/gh/AlexWayfer/flame-pagination)
5
+ [![Code Climate](https://img.shields.io/codeclimate/maintainability/AlexWayfer/flame-pagination.svg?style=flat-square)](https://codeclimate.com/github/AlexWayfer/flame-pagination)
6
+ ![Depfu](https://img.shields.io/depfu/AlexWayfer/flame-pagination?style=flat-square)
7
+ [![Inline docs](https://inch-ci.org/github/AlexWayfer/flame-pagination.svg?branch=master)](https://inch-ci.org/github/AlexWayfer/flame-pagination)
8
+ [![license](https://img.shields.io/github/license/AlexWayfer/flame-pagination.svg?style=flat-square)](https://github.com/AlexWayfer/flame-pagination/blob/master/LICENSE)
9
+ [![Gem](https://img.shields.io/gem/v/flame-pagination.svg?style=flat-square)](https://rubygems.org/gems/flame-pagination)
10
+
11
+ Pagination for Flame application with Formalism forms.
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'flame-pagination'
19
+ ```
20
+
21
+ And then execute:
22
+
23
+ ```shell
24
+ bundle install
25
+ ```
26
+
27
+ Or install it yourself as:
28
+
29
+ ```shell
30
+ gem install flame-pagination
31
+ ```
32
+
33
+ ## Usage
34
+
35
+ Recommended to use only as dependency of
36
+ [`formalism-model_forms`](https://github.com/AlexWayfer/formalism-model_forms).
37
+
38
+ You'll still have to include `Flame::Pagination::ForController` into your model controller.
39
+
40
+ Details below:
41
+
42
+ ```ruby
43
+ require 'flame-pagination'
44
+
45
+ module MyProject
46
+ ## Already in `formalism-model_forms`
47
+ module Forms
48
+ module Model
49
+ ## Base form for model listing
50
+ class List < Base
51
+ include Flame::Pagination::ForForms
52
+ end
53
+ end
54
+
55
+ module User
56
+ class List < Model::List
57
+ end
58
+ end
59
+ end
60
+
61
+ ## Have to be set manually, maybe in some common `ModelController`
62
+ module Controlelrs
63
+ module UsersController
64
+ extend Flame::Controller::Actions
65
+ include Flame::Pagination::ForController
66
+
67
+ def index
68
+ @form = Forms::User::List.new(params[:user])
69
+
70
+ paginate! @form
71
+
72
+ @users = if (form_outcome = @form.run).success?
73
+ form_outcome.result
74
+ end
75
+
76
+ view :index
77
+ end
78
+ end
79
+ end
80
+ end
81
+ ```
82
+
83
+ ## Development
84
+
85
+ After checking out the repo, run `bundle install` to install dependencies.
86
+
87
+ Then, run `toys rspec` to run the tests.
88
+
89
+ To install this gem onto your local machine, run `toys gem install`.
90
+
91
+ To release a new version, run `toys gem release %version%`.
92
+ See how it works [here](https://github.com/AlexWayfer/gem_toys#release).
93
+
94
+ ## Contributing
95
+
96
+ Bug reports and pull requests are welcome on [GitHub](https://github.com/AlexWayfer/flame-pagination).
97
+
98
+ ## License
99
+
100
+ The gem is available as open source under the terms of the
101
+ [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'pagination/version'
4
+ require_relative 'pagination/for_controller'
5
+ require_relative 'pagination/for_forms'
@@ -0,0 +1,97 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'memery'
4
+ require 'module_methods'
5
+
6
+ module Flame
7
+ module Pagination
8
+ ## Mixin for controllers
9
+ module ForController
10
+ PER_PAGE_OPTIONS = [10, 20, 50].freeze
11
+ DEFAULT_PER_PAGE = PER_PAGE_OPTIONS.first
12
+
13
+ extend ::ModuleMethods
14
+
15
+ ## Module with class methods for controllers
16
+ module ClassMethods
17
+ include Memery
18
+
19
+ memoize def records_key_for_session
20
+ "#{instance_collection_name}_on_page"
21
+ end
22
+ end
23
+
24
+ protected
25
+
26
+ def execute(action)
27
+ @action = action
28
+
29
+ @previous_saved_per_page = saved_per_page
30
+
31
+ per_page_change_if_different
32
+
33
+ super
34
+ end
35
+
36
+ private
37
+
38
+ attr_reader :pages_enum
39
+
40
+ def paginate!(form, near_edge: 0, arround_current: 2)
41
+ available_current_page, @pages_count =
42
+ form.paginate! current_page, per_page: current_per_page
43
+
44
+ halt_redirect_to_page available_current_page if available_current_page != current_page
45
+
46
+ @pages_enum = Pages.new(current_page, @pages_count, near_edge, arround_current).to_enum
47
+ end
48
+
49
+ def current_page
50
+ @current_page ||= (params[:page] || 1).to_i
51
+ end
52
+
53
+ def current_per_page
54
+ return @current_per_page if defined? @current_per_page
55
+
56
+ per_page_index = PER_PAGE_OPTIONS.index(
57
+ params.fetch(:per_page, saved_per_page).to_i
58
+ )
59
+
60
+ @current_per_page =
61
+ per_page_index ? PER_PAGE_OPTIONS[per_page_index] : DEFAULT_PER_PAGE
62
+ end
63
+
64
+ def params_with_page(page, per_page)
65
+ params.merge(page: page, per_page: per_page)
66
+ end
67
+
68
+ def saved_per_page
69
+ authenticated.session&.options&.[](self.class.records_key_for_session)
70
+ end
71
+
72
+ def halt_redirect_to_page(page, action = @action)
73
+ halt redirect action, **params_with_page(page, current_per_page)
74
+ end
75
+
76
+ def page_after_per_page_change
77
+ previous_per_page = @previous_saved_per_page || DEFAULT_PER_PAGE
78
+ Rational((current_page - 1) * previous_per_page + 1, current_per_page).ceil
79
+ end
80
+
81
+ def per_page_change_if_different
82
+ return if @previous_saved_per_page == current_per_page
83
+
84
+ authenticated.session&.update(
85
+ options: Sequel.pg_jsonb_op(:options).concat(
86
+ self.class.records_key_for_session => current_per_page
87
+ )
88
+ )
89
+
90
+ return unless authenticated.account
91
+ return unless current_page > 1
92
+
93
+ halt_redirect_to_page page_after_per_page_change, @action
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Flame
4
+ module Pagination
5
+ ## Mixin for forms
6
+ module ForForms
7
+ def paginate!(page, per_page: 10)
8
+ @page = page
9
+ @per_page = per_page
10
+
11
+ pages_count = valid? ? (dataset.count / @per_page.to_f).ceil : 0
12
+ @pages_count = pages_count.positive? ? pages_count : 1
13
+
14
+ [@page.clamp(1, @pages_count), @pages_count]
15
+ end
16
+
17
+ private
18
+
19
+ def limit_by_page
20
+ return unless @page
21
+
22
+ ((@page - 1) * @per_page)...(@page * @per_page)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Flame
4
+ module Pagination
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,223 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: flame-pagination
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alexander Popov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-07-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: alt_memery
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: module_methods
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.1.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.1.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry-byebug
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.9'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.9'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: gem_toys
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.2.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.2.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: toys
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.10.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.10.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: codecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 0.1.0
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 0.1.0
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '3.9'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '3.9'
125
+ - !ruby/object:Gem::Dependency
126
+ name: simplecov
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: 0.18.0
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: 0.18.0
139
+ - !ruby/object:Gem::Dependency
140
+ name: rubocop
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: 0.87.0
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: 0.87.0
153
+ - !ruby/object:Gem::Dependency
154
+ name: rubocop-performance
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '1.0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '1.0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: rubocop-rspec
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - "~>"
172
+ - !ruby/object:Gem::Version
173
+ version: '1.0'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - "~>"
179
+ - !ruby/object:Gem::Version
180
+ version: '1.0'
181
+ description: 'Pagination for Flame application with Formalism forms.
182
+
183
+ '
184
+ email:
185
+ - alex.wayfer@gmail.com
186
+ executables: []
187
+ extensions: []
188
+ extra_rdoc_files: []
189
+ files:
190
+ - CHANGELOG.md
191
+ - LICENSE.txt
192
+ - README.md
193
+ - lib/flame/pagination.rb
194
+ - lib/flame/pagination/for_controller.rb
195
+ - lib/flame/pagination/for_forms.rb
196
+ - lib/flame/pagination/version.rb
197
+ homepage: https://github.com/AlexWayfer/flame-pagination
198
+ licenses:
199
+ - MIT
200
+ metadata:
201
+ source_code_uri: https://github.com/AlexWayfer/flame-pagination
202
+ homepage_uri: https://github.com/AlexWayfer/flame-pagination
203
+ changelog_uri: https://github.com/AlexWayfer/flame-pagination/blob/master/CHANGELOG.md
204
+ post_install_message:
205
+ rdoc_options: []
206
+ require_paths:
207
+ - lib
208
+ required_ruby_version: !ruby/object:Gem::Requirement
209
+ requirements:
210
+ - - ">="
211
+ - !ruby/object:Gem::Version
212
+ version: '2.5'
213
+ required_rubygems_version: !ruby/object:Gem::Requirement
214
+ requirements:
215
+ - - ">="
216
+ - !ruby/object:Gem::Version
217
+ version: '0'
218
+ requirements: []
219
+ rubygems_version: 3.1.2
220
+ signing_key:
221
+ specification_version: 4
222
+ summary: Pagination for Flame application with Formalism forms
223
+ test_files: []