paginate_cached 1.0 → 1.01

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 52bcf6e765191486dc05c806ac23df6bbf0a5d28
4
- data.tar.gz: 7e2a0aa95d1ec2d36c31d83912de74bfe4a71b1b
3
+ metadata.gz: 019c6b81fdcc83fb2d972c248a306481002f57a3
4
+ data.tar.gz: 8fa5db699cb800a2cf4a9bbe2fb51773d7b63f7a
5
5
  SHA512:
6
- metadata.gz: 9eb44dcb40b49136ba50ac91a4dfc4656a0d25c9a307d68a7ad433886faa728856c460105b6f7a5dbcb0d9744c8c89453030abdb7edf09e9444b740b766f31ea
7
- data.tar.gz: 88466bacb8262ca7f7149426f9c1adf58b7954afc2a8e8ed6d49b3c6bd18d2aaa382a7bdbd9fa3c177815ca6624cb05e80fa9f493f6bc72e381bb3a3b319e3d9
6
+ metadata.gz: e4c74129c43bc1e592fc3096411a88bd050e6dd6ac16911dedcc9bbc47434f2d2a16ea0d7d83e0760a101629f0a311b38d0c957df5786aac9ca6eb5435517e79
7
+ data.tar.gz: 447319c0c7e43effd340d8778a83e158421a45cb4add367455be091c922a8747e297f7c445b3f97da5442a5e6541e84fdc4c272064db864c7c8d7811c1a5c0bf
@@ -0,0 +1,37 @@
1
+ module PaginateCached
2
+ module ControllerHelpers
3
+ # Arguments:
4
+ # This function takes :page as the page number, it defaults to 0.
5
+ # The number of results is passed in as :number_of_results. It defaults to 20.
6
+ # The variable used to hold the current state in the pagination should be placed
7
+ # in the :search_id variable.
8
+ #
9
+ # The actual code which produces the result is passed in as a block.
10
+ #
11
+ # Results:
12
+ # This returns a hash in which :results will hold the results, :search_id will hold the search id,
13
+ # next_page: will hold the new page number, and has_more_results will hold whether there are more results.
14
+ #
15
+ def paginate_cached args={}
16
+ page = args[:page] || 1
17
+ number_of_results = args[:number_of_results] || 25
18
+ search_id = args[:search_id]
19
+ offset = (page-1)*number_of_results
20
+
21
+ unless search_id and results = Rails.cache.read(search_id) then
22
+ results = yield
23
+ search_id = "#{results.hash}#{Time.now.to_i}"
24
+ Rails.cache.write search_id, results, expires_in: 15.minutes
25
+ end
26
+
27
+ return {
28
+ results: results[offset, number_of_results],
29
+ search_id: search_id,
30
+ next_page: page+1,
31
+ has_more_results: results[((page)*number_of_results)+1] != nil
32
+ }
33
+
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,5 @@
1
+ require 'paginate_cached/paginate_cached.rb'
2
+
3
+ ActiveSupport.on_load(:action_controller) do
4
+ include PaginateCached::ControllerHelpers
5
+ end
@@ -1,38 +1 @@
1
- module PaginateCached
2
-
3
- # Arguments:
4
- # This function takes :page as the page number, and it is required.
5
- # The number of results is passed in as :number_of_results. It defaults to 20.
6
- # The variable used to hold the current state in the pagination should be placed
7
- # in the :search_id variable.
8
- #
9
- # The actual code which produces the result is passed in as a block.
10
- #
11
- # Results:
12
- # This returns a hash in which :results will hold the results, :search_id will hold the search id,
13
- # next_page: will hold the new page number, and has_more_results will hold whether there are more results.
14
- #
15
- def paginate_cached args={}
16
- page = args[:page] or raise "Page number must be defined."
17
- number_of_results = args[:number_of_results] || 25
18
- search_id = args[:search_id]
19
- offset = (page-1)*number_of_results
20
-
21
- unless search_id and results = Rails.cache.read(search_id) then
22
- results = yield
23
- search_id = "#{results.hash}#{Time.now.to_i}"
24
- Rails.cache.write search_id, results, expires_id: 15.minutes
25
- end
26
-
27
- return {
28
- results: results[offset, number_of_results],
29
- search_id: search_id,
30
- next_page: page+1,
31
- has_more_results: results[((page)*number_of_results)+1] != nil
32
- }
33
-
34
- end
35
-
36
- ActionController::Base.send :include, PaginateCached
37
-
38
- end
1
+ require 'paginate_cached/railtie.rb' if defined?(Rails)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paginate_cached
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.0'
4
+ version: '1.01'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Kyte
@@ -18,6 +18,8 @@ extra_rdoc_files: []
18
18
  files:
19
19
  - lib/paginate_cached.rb
20
20
  - init.rb
21
+ - lib/paginate_cached/paginate_cached.rb
22
+ - lib/paginate_cached/railtie.rb
21
23
  homepage: http://github.com/alexanderkyte/paginate_cached
22
24
  licenses: []
23
25
  metadata: {}