shopify-kaminari 1.0.0 → 1.1.0

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
  SHA256:
3
- metadata.gz: 570e3cec212e5ffa136c2fdd279ab02c568f252eb41972d3f0d1c339f31999b9
4
- data.tar.gz: d2c2956304dcd1e2819d7da01921ecf401f5bf42079a5ac7fe7519483b9138c6
3
+ metadata.gz: 2a614836a3884fb0529055ef9357624a8fb64d06780605440adfb51facdf0f2f
4
+ data.tar.gz: 3263018947abd0c2f8f9926c5be6304e91ff04311d2b03392ea9557d484e3740
5
5
  SHA512:
6
- metadata.gz: fb6eedc2d04d4c137dce13f6cd2771913e2cf8f411bf415d835055cc4a0580e72ba8f3a77b10d99708751bca08cb3bda02866616215c2e4e61b8aa2dc7c044d8
7
- data.tar.gz: 7945a630991dc163ef1ed01e9c31529bbcc0c9d5e7ae1825b544065762ca925aaa3cc32ac09ec69ebd2ce394fc5a82203466b375c0f3baeff7d57f8a016d6b6c
6
+ metadata.gz: 0b3e08f6d2c6bd8f092501be5fb2932085926692d85488174fc3097bcc1938f59dc520c99b6a3b721ce6a83c44bbd7c472e771e7a866e398d2eb581e65e75ddc
7
+ data.tar.gz: 34599fb94e3578eaf6f67c750fa70abc193c1fb2c7d3a47ba47acd598756fb74a0b072465e3a954c3aa140bb7d61804f946a52c882a49a222e95a247bba13381
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- shopify-kaminari (1.0.0)
4
+ shopify-kaminari (1.1.0)
5
5
  kaminari
6
6
  shopify_api
7
7
 
@@ -1,6 +1,7 @@
1
1
  # coding: utf-8
2
2
  # frozen_string_literal: true
3
3
 
4
+ require 'kaminari'
4
5
  require 'shopify_api'
5
6
 
6
7
  module Shopify
@@ -10,5 +11,12 @@ module Shopify
10
11
  end
11
12
  end
12
13
 
13
- # Set the Shopify API's default collection parser.
14
- ShopifyAPI::Base.collection_parser = Shopify::Kaminari::Collection
14
+ ShopifyAPI::Base.class_eval do
15
+ # Set the default collection parser.
16
+ self.collection_parser = Shopify::Kaminari::Collection
17
+
18
+ # Create the #page method.
19
+ define_singleton_method :"#{Kaminari.config.page_method_name}" do |num = 1|
20
+ all(params: { page: num })
21
+ end
22
+ end
@@ -9,16 +9,49 @@
9
9
  # @example Paginating products.
10
10
  # products = Shopify::Product.all(params: { page: 2, limit: 25 })
11
11
  class Shopify::Kaminari::Collection < ActiveResource::Collection
12
+ include ::Kaminari::ConfigurationMethods
13
+
12
14
  alias_method :model, :resource_class
13
15
 
14
16
  # Shopify returns this many records by default if no limit is given.
15
17
  DEFAULT_LIMIT_VALUE = 50
16
18
 
19
+ ENTRIES_INFO_I18N_KEY = 'helpers.page_entries_info.entry'.freeze
20
+
21
+ self.paginates_per(DEFAULT_LIMIT_VALUE)
22
+
17
23
  # The page that was requested from the API.
18
24
  #
19
25
  # @return [Integer] The current page.
20
26
  def current_page
21
- @current_page ||= original_params.fetch(:page, 1).to_i
27
+ original_params.fetch(:page, 1).to_i
28
+ end
29
+
30
+ # @return [Integer] Current per-page number.
31
+ def current_per_page
32
+ limit_value
33
+ end
34
+
35
+ # Used for page_entries_info ActionView helper.
36
+ #
37
+ # @param [Hash] options Options provided by Kaminari.
38
+ # @return [String] Numbers of displayed vs. total entries.
39
+ def entry_name(options = {})
40
+ options = options.reverse_merge(
41
+ default: model_name.singular.pluralize(options[:count])
42
+ )
43
+
44
+ I18n.t(ENTRIES_INFO_I18N_KEY, options)
45
+ end
46
+
47
+ # @return [true, false] If on the first page.
48
+ def first_page?
49
+ current_page == 1
50
+ end
51
+
52
+ # @return [true, false] If on the last page.
53
+ def last_page?
54
+ current_page == total_pages
22
55
  end
23
56
 
24
57
  # The maximum amount of records each page will have.
@@ -46,32 +79,53 @@ class Shopify::Kaminari::Collection < ActiveResource::Collection
46
79
  #
47
80
  # @return [Integer, NilClass] The next page number, or nil if on the last.
48
81
  def next_page
49
- @next_page ||= current_page + 1 if current_page < total_pages
82
+ current_page + 1 unless last_page? || out_of_range?
83
+ end
84
+
85
+ # Checks if the specified page is out of the range of the collection.
86
+ #
87
+ # @return [true, false] If out of range.
88
+ def out_of_range?
89
+ current_page > total_pages
90
+ end
91
+
92
+ # Fetches a new collection from the Shopify server using the same
93
+ # parameters, except with a new value for limit.
94
+ #
95
+ # @param [Integer] limit New limit to apply to the collection.
96
+ # @return [Shopify::Kaminari::Collection] Updated collection.
97
+ def per(limit)
98
+ params = original_params.with_indifferent_access.merge(limit: limit)
99
+
100
+ resource_class.all(params: params)
50
101
  end
51
102
 
52
103
  # Gets the number of the previous page.
53
104
  #
54
105
  # @return [Integer, NilClass] The previous page number or nil if on the first.
55
106
  def prev_page
56
- @prev_page ||= current_page - 1 if current_page > 1
107
+ current_page - 1 unless first_page? || out_of_range?
57
108
  end
58
109
 
59
- # Calculates the total number of expected pages based on the number
60
- # reported by the API.
110
+ # Gets the total number of entries available on the server for the original
111
+ # parameters used to fetch the collection.
61
112
  #
62
- # @return [Integer] Total number of pages.
63
- def total_pages
64
- @total_pages ||= begin
113
+ # @return [Integer] Total number of entries.
114
+ def total_count
115
+ @total_count ||= begin
65
116
  # Get the parameters without the pagination parameters.
66
- params = original_params.with_indifferent_access.except(:page, :limit)
67
-
68
- options = params.empty? ? {} : params
117
+ options = original_params.with_indifferent_access.except(:page, :limit)
69
118
 
70
119
  # Ask Shopify how many records there are for the given query.
71
120
  count = resource_class.count(options)
72
-
73
- # Calculate the number of pages.
74
- (count.to_f / limit_value.to_f).ceil
75
121
  end
76
122
  end
123
+
124
+ # Calculates the total number of expected pages based on the number
125
+ # reported by the API.
126
+ #
127
+ # @return [Integer] Total number of pages.
128
+ def total_pages
129
+ @total_pages ||= (total_count.to_f / limit_value.to_f).ceil
130
+ end
77
131
  end
@@ -4,6 +4,6 @@
4
4
  module Shopify
5
5
  module Kaminari
6
6
  # The gem's semantic version.
7
- VERSION = '1.0.0'
7
+ VERSION = '1.1.0'
8
8
  end
9
9
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shopify-kaminari
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Travis Haynes
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-04-27 00:00:00.000000000 Z
11
+ date: 2018-05-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: shopify_api