effective_bootstrap 0.5.5 → 0.5.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
  SHA1:
3
- metadata.gz: a6d127e5de8a6cd39479cb87dcd48c51e83b39d4
4
- data.tar.gz: 68cf3f3152c1b3e5d5b4ea174a546d588182ae69
3
+ metadata.gz: fed9040bd034c8360e211400e6c028d53fa70fe2
4
+ data.tar.gz: f3012062b2c332d41bed498ce439a9492bba2e7a
5
5
  SHA512:
6
- metadata.gz: 22011bfe4b8c91aed0d7dc6aa7926fdd3f9662053f28c05f6ff8301d786b9d57424ce693a8cbaf3af8290fc4410278c8811b987a79ed26db99daa54fff140884
7
- data.tar.gz: 881a9ef2f3bc8c5172e6d772199fe158587468f35a5392f887037c94e36191541e1bd1c4245d4450c5da146b6ffcdff20a446f22724e7a0b2cb17b0a67e1b7e9
6
+ metadata.gz: ece6c140fdd1789e027d9a003190ae7cc48746bcf1b5f71c3dfcd562991109769f41889841109729914f3390dfc7b5c8a3cf8bdd26571618b7a2e94144c05000
7
+ data.tar.gz: c0fd95a31e68baef499142c267eb92909b1dfdbb94991f4af230e7fe9b98ff7d4fb709aeed17d55b9f451c1e95216a087ccfce8975a0d1aba653d0e6087dffce
data/README.md CHANGED
@@ -116,6 +116,43 @@ https://getbootstrap.com/docs/4.0/components/navbar/
116
116
 
117
117
  `nav_link_to` will automatically insert the `.active` class based on the request path.
118
118
 
119
+ ### Pagination
120
+
121
+ https://getbootstrap.com/docs/4.0/components/pagination/
122
+
123
+ Builds a pagination based on the given collection, current url and params[:page].
124
+
125
+ The collection must be an ActiveRecord relation.
126
+
127
+ ```haml
128
+ = paginate(@posts, per_page: 10)
129
+ ```
130
+
131
+ Add this to your model:
132
+
133
+ ```ruby
134
+ scope :paginate, -> (page: nil, per_page:) {
135
+ page = (page || 1).to_i
136
+ offset = [(page - 1), 0].max * per_page
137
+
138
+ limit(per_page).offset(offset)
139
+ }
140
+ ```
141
+
142
+ Add this to your controller:
143
+
144
+ ```ruby
145
+ def index
146
+ @posts = Post.all.paginate(page: params[:page], per_page: 10)
147
+ end
148
+ ```
149
+
150
+ Add this to your view:
151
+
152
+ ```haml
153
+ %nav= paginate(@posts, per_page: 10)
154
+ ```
155
+
119
156
  ### Tabs
120
157
 
121
158
  https://getbootstrap.com/docs/4.0/components/navs/#tabs
@@ -172,6 +172,57 @@ module EffectiveBootstrapHelper
172
172
  content_tag(:div, '', class: 'dropdown-divider')
173
173
  end
174
174
 
175
+ # Pagination
176
+ #
177
+ # https://getbootstrap.com/docs/4.0/components/pagination/
178
+ # Builds a pagination based on the given collection, current url and params[:page]
179
+ #
180
+ # = paginate(@posts, per_page: 10)
181
+ #
182
+ # Add this to your model
183
+ # scope :paginate, -> (page: nil, per_page: 10) {
184
+ # page = (page || 1).to_i
185
+ # offset = [(page - 1), 0].max * per_page
186
+
187
+ # limit(per_page).offset(offset)
188
+ # }
189
+ #
190
+ # Add this to your controller:
191
+ # @posts = Post.all.paginate(page: params[:page])
192
+ #
193
+ # Add this to your view
194
+ # %nav= paginate(@posts, per_page: 10)
195
+ #
196
+ def paginate(collection, per_page:, url: nil)
197
+ raise 'expected an ActiveRecord::Relation' unless collection.respond_to?(:limit) && collection.respond_to?(:offset)
198
+
199
+ count = collection.limit(nil).offset(nil).count
200
+
201
+ page = (params[:page] || 1).to_i
202
+ pages = (count.to_f / per_page).ceil
203
+
204
+ uri = URI(url || request.fullpath)
205
+ params = Rack::Utils.parse_nested_query(uri.query)
206
+ url = uri.path + '?'
207
+
208
+ content_tag(:ul, class: 'pagination') do
209
+ content_tag(:li, class: ['page-item', ('disabled' if page <= 1)].compact.join(' ')) do
210
+ link_to(url + params.merge('page' => page - 1).to_query, class: 'page-link', 'aria-label': 'Previous', title: 'Previous') do
211
+ content_tag(:span, '&laquo;'.html_safe, 'aria-hidden': true) + content_tag(:span, 'Previous', class: 'sr-only')
212
+ end
213
+ end + (1..pages).map do |index|
214
+ content_tag(:li, class: ['page-item', ('active' if index == page)].compact.join(' '), title: "Page #{index}") do
215
+ link_to(index, (url + params.merge('page' => index).to_query), class: 'page-link')
216
+ end
217
+ end.join.html_safe +
218
+ content_tag(:li, class: ['page-item', ('disabled' if page >= pages)].compact.join(' ')) do
219
+ link_to(url + params.merge('page' => page + 1).to_query, class: 'page-link', 'aria-label': 'Next', title: 'Next') do
220
+ content_tag(:span, '&raquo;'.html_safe, 'aria-hidden': true) + content_tag(:span, 'Next', class: 'sr-only')
221
+ end
222
+ end
223
+ end
224
+ end
225
+
175
226
  # Tabs DSL
176
227
  # Inserts both the tablist and the tabpanel
177
228
 
@@ -1,3 +1,3 @@
1
1
  module EffectiveBootstrap
2
- VERSION = '0.5.5'.freeze
2
+ VERSION = '0.5.6'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: effective_bootstrap
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.5
4
+ version: 0.5.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Code and Effect
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-05-03 00:00:00.000000000 Z
11
+ date: 2019-05-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails