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 +4 -4
- data/Gemfile.lock +1 -1
- data/lib/shopify-kaminari.rb +10 -2
- data/lib/shopify/kaminari/collection.rb +68 -14
- data/lib/shopify/kaminari/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2a614836a3884fb0529055ef9357624a8fb64d06780605440adfb51facdf0f2f
|
4
|
+
data.tar.gz: 3263018947abd0c2f8f9926c5be6304e91ff04311d2b03392ea9557d484e3740
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0b3e08f6d2c6bd8f092501be5fb2932085926692d85488174fc3097bcc1938f59dc520c99b6a3b721ce6a83c44bbd7c472e771e7a866e398d2eb581e65e75ddc
|
7
|
+
data.tar.gz: 34599fb94e3578eaf6f67c750fa70abc193c1fb2c7d3a47ba47acd598756fb74a0b072465e3a954c3aa140bb7d61804f946a52c882a49a222e95a247bba13381
|
data/Gemfile.lock
CHANGED
data/lib/shopify-kaminari.rb
CHANGED
@@ -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
|
-
|
14
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
107
|
+
current_page - 1 unless first_page? || out_of_range?
|
57
108
|
end
|
58
109
|
|
59
|
-
#
|
60
|
-
#
|
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
|
63
|
-
def
|
64
|
-
@
|
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
|
-
|
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
|
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.
|
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-
|
11
|
+
date: 2018-05-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: shopify_api
|