yaqb 0.3.1 → 0.4.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/yaqb/{configuration.rb → config.rb} +20 -4
- data/lib/yaqb/hooks.rb +20 -4
- data/lib/yaqb/query_builders/paginate.rb +3 -28
- data/lib/yaqb/query_builders/paginators/kaminari_helper.rb +44 -0
- data/lib/yaqb/query_builders/paginators/will_paginate_helper.rb +44 -0
- data/lib/yaqb/version.rb +3 -1
- data/lib/yaqb.rb +7 -17
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4c6f3df2bfa123895902a6cc6482f20097c9f9a85164c50c81a6ba8878eb9199
|
4
|
+
data.tar.gz: 602dd55100b586b9716009085115f3d760ed0504fe157740ce1c04e75ea21835
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4c2dc6e22a9891e8e48b0a7d488ae590a4bd17f93472f04dfec041c8e425fa9be3e61910a619e219744e3be1ef3dd578de8a73ecca43cb06f38dd9f26b722e3c
|
7
|
+
data.tar.gz: 6fe88fe10f56aa19394e68df9ce4d17b335bc493349c965e7f3a918afe44beeb453e7d0c8eed5681f2e18999f7dc5a6520d4f107fc250729ecd4cff5e8ff323c
|
data/Gemfile.lock
CHANGED
@@ -1,15 +1,27 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Yaqb
|
4
|
-
class
|
4
|
+
class << self
|
5
|
+
def configure
|
6
|
+
yield(config)
|
7
|
+
end
|
8
|
+
|
9
|
+
def config
|
10
|
+
@config ||= Config.new
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class Config
|
5
15
|
def paginator
|
6
|
-
@paginator
|
16
|
+
instance_variable_defined?(:@paginator) ? @paginator : set_paginator
|
7
17
|
end
|
8
18
|
|
9
19
|
def paginator=(paginator)
|
10
20
|
case paginator.to_sym
|
11
21
|
when :kaminari
|
12
22
|
@paginator = :kaminari
|
23
|
+
when :will_paginate
|
24
|
+
@paginator = :will_paginate
|
13
25
|
else
|
14
26
|
raise StandardError, paginator_error_message(paginator)
|
15
27
|
end
|
@@ -18,12 +30,16 @@ module Yaqb
|
|
18
30
|
private
|
19
31
|
|
20
32
|
def set_paginator
|
21
|
-
@paginator =
|
33
|
+
@paginator = if defined?(Kaminari)
|
34
|
+
:kaminari
|
35
|
+
elsif defined?(WillPaginate::CollectionMethods)
|
36
|
+
:will_paginate
|
37
|
+
end
|
22
38
|
end
|
23
39
|
|
24
40
|
def paginator_error_message(paginator)
|
25
41
|
<<~HEREDOC.chomp
|
26
|
-
Invalid Paginator: (#{paginator}). Currently supported paginators are: [Kaminari].
|
42
|
+
Invalid Paginator: (#{paginator}). Currently supported paginators are: [Kaminari, WillPaginate].
|
27
43
|
HEREDOC
|
28
44
|
end
|
29
45
|
end
|
data/lib/yaqb/hooks.rb
CHANGED
@@ -1,11 +1,27 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
begin; require 'kaminari'; rescue LoadError; end
|
4
|
+
begin; require 'will_paginate'; rescue LoadError; end
|
4
5
|
|
5
|
-
unless defined?(Kaminari)
|
6
|
+
unless defined?(Kaminari) || defined?(WillPaginate::CollectionMethods)
|
6
7
|
Kernel.warn <<~HEREDOC
|
7
|
-
Warning: Yaqb relies on Kaminari. Please
|
8
|
-
install dependency by adding the following to your Gemfile:
|
9
|
-
gem 'kaminari'
|
8
|
+
Warning: Yaqb relies on Kaminari or WillPaginate. Please
|
9
|
+
install dependency by adding one of the following to your Gemfile:
|
10
|
+
gem 'kaminari'
|
11
|
+
gem 'will_paginate'\n
|
10
12
|
HEREDOC
|
11
13
|
end
|
14
|
+
|
15
|
+
begin
|
16
|
+
if defined?(Kaminari)
|
17
|
+
require 'yaqb/query_builders/paginators/kaminari_helper'
|
18
|
+
Yaqb::QueryBuilders::Paginate.send(:include, Yaqb::QueryBuilders::Paginators::KaminariHelper)
|
19
|
+
elsif defined?(WillPaginate::CollectionMethods)
|
20
|
+
require 'yaqb/query_builders/paginators/will_paginate_helper'
|
21
|
+
Yaqb::QueryBuilders::Paginate.send(:include, Yaqb::QueryBuilders::Paginators::WillPaginateHelper)
|
22
|
+
else
|
23
|
+
raise LoadError
|
24
|
+
end
|
25
|
+
rescue LoadError
|
26
|
+
exit
|
27
|
+
end
|
@@ -6,8 +6,8 @@ module Yaqb
|
|
6
6
|
def initialize(scope, query_params, url)
|
7
7
|
@query_params = query_params
|
8
8
|
@page = validate_param!(:page, 1)
|
9
|
-
@per = validate_param!(:per,
|
10
|
-
@scope = scope
|
9
|
+
@per = validate_param!(:per, default_per_page)
|
10
|
+
@scope = paginate!(scope)
|
11
11
|
@url = url
|
12
12
|
end
|
13
13
|
|
@@ -24,37 +24,12 @@ module Yaqb
|
|
24
24
|
|
25
25
|
private
|
26
26
|
|
27
|
-
def show_first_link?
|
28
|
-
@scope.total_pages > 1 && !@scope.first_page?
|
29
|
-
end
|
30
|
-
|
31
|
-
def show_previous_link?
|
32
|
-
!@scope.first_page?
|
33
|
-
end
|
34
|
-
|
35
|
-
def show_next_link?
|
36
|
-
!@scope.last_page?
|
37
|
-
end
|
38
|
-
|
39
|
-
def show_last_link?
|
40
|
-
@scope.total_pages > 1 && !@scope.last_page?
|
41
|
-
end
|
42
|
-
|
43
|
-
def pages
|
44
|
-
@pages ||= {}.tap do |h|
|
45
|
-
h[:first] = 1 if show_first_link?
|
46
|
-
h[:prev] = @scope.current_page - 1 if show_previous_link?
|
47
|
-
h[:next] = @scope.current_page + 1 if show_next_link?
|
48
|
-
h[:last] = @scope.total_pages if show_last_link?
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
27
|
def validate_param!(name, default)
|
53
28
|
return default unless @query_params[name]
|
54
29
|
|
55
30
|
unless @query_params[name] =~ /\A\d+\z/
|
56
31
|
raise Errors::QueryBuilderError.new("#{name}=#{@query_params[name]}"),
|
57
|
-
'Invalid
|
32
|
+
'Invalid pagination params. Only numbers are supported for "page" and "per"'
|
58
33
|
end
|
59
34
|
|
60
35
|
@query_params[name]
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Yaqb
|
4
|
+
module QueryBuilders
|
5
|
+
module Paginators
|
6
|
+
module KaminariHelper
|
7
|
+
private
|
8
|
+
|
9
|
+
def paginate!(scope)
|
10
|
+
scope.page(@page).per(@per)
|
11
|
+
end
|
12
|
+
|
13
|
+
def show_first_link?
|
14
|
+
@scope.total_pages > 1 && !@scope.first_page?
|
15
|
+
end
|
16
|
+
|
17
|
+
def show_previous_link?
|
18
|
+
!@scope.first_page?
|
19
|
+
end
|
20
|
+
|
21
|
+
def show_next_link?
|
22
|
+
!@scope.last_page?
|
23
|
+
end
|
24
|
+
|
25
|
+
def show_last_link?
|
26
|
+
@scope.total_pages > 1 && !@scope.last_page?
|
27
|
+
end
|
28
|
+
|
29
|
+
def pages
|
30
|
+
@pages ||= {}.tap do |h|
|
31
|
+
h[:first] = 1 if show_first_link?
|
32
|
+
h[:prev] = @scope.current_page - 1 if show_previous_link?
|
33
|
+
h[:next] = @scope.current_page + 1 if show_next_link?
|
34
|
+
h[:last] = @scope.total_pages if show_last_link?
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def default_per_page
|
39
|
+
Kaminari.config.default_per_page
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Yaqb
|
4
|
+
module QueryBuilders
|
5
|
+
module Paginators
|
6
|
+
module WillPaginateHelper
|
7
|
+
private
|
8
|
+
|
9
|
+
def paginate!(scope)
|
10
|
+
scope.paginate(page: @page, per_page: @per)
|
11
|
+
end
|
12
|
+
|
13
|
+
def show_first_link?
|
14
|
+
@scope.total_pages > 1 && !!@scope.previous_page
|
15
|
+
end
|
16
|
+
|
17
|
+
def show_previous_link?
|
18
|
+
!!@scope.previous_page
|
19
|
+
end
|
20
|
+
|
21
|
+
def show_next_link?
|
22
|
+
!!@scope.next_page
|
23
|
+
end
|
24
|
+
|
25
|
+
def show_last_link?
|
26
|
+
@scope.total_pages > 1 && !!@scope.next_page
|
27
|
+
end
|
28
|
+
|
29
|
+
def pages
|
30
|
+
@pages ||= {}.tap do |h|
|
31
|
+
h[:first] = 1 if show_first_link?
|
32
|
+
h[:prev] = @scope.current_page - 1 if show_previous_link?
|
33
|
+
h[:next] = @scope.current_page + 1 if show_next_link?
|
34
|
+
h[:last] = @scope.total_pages if show_last_link?
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def default_per_page
|
39
|
+
WillPaginate.per_page
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/lib/yaqb/version.rb
CHANGED
data/lib/yaqb.rb
CHANGED
@@ -1,21 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
require 'yaqb/hooks'
|
5
|
-
require 'yaqb/configuration'
|
6
|
-
require 'yaqb/presenter'
|
7
|
-
require 'yaqb/base'
|
8
|
-
require 'yaqb/errors/query_builder_error'
|
3
|
+
module Yaqb; end
|
9
4
|
|
10
|
-
|
11
|
-
class << self
|
12
|
-
def configure
|
13
|
-
yield(configuration)
|
14
|
-
end
|
5
|
+
begin; require 'rails'; rescue LoadError; end
|
15
6
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
end
|
7
|
+
require 'yaqb/config'
|
8
|
+
require 'yaqb/errors/query_builder_error'
|
9
|
+
require 'yaqb/presenter'
|
10
|
+
require 'yaqb/base'
|
11
|
+
require 'yaqb/hooks'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yaqb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ronald Chacon
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-10-
|
11
|
+
date: 2019-10-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -86,12 +86,14 @@ files:
|
|
86
86
|
- bin/setup
|
87
87
|
- lib/yaqb.rb
|
88
88
|
- lib/yaqb/base.rb
|
89
|
-
- lib/yaqb/
|
89
|
+
- lib/yaqb/config.rb
|
90
90
|
- lib/yaqb/errors/query_builder_error.rb
|
91
91
|
- lib/yaqb/hooks.rb
|
92
92
|
- lib/yaqb/presenter.rb
|
93
93
|
- lib/yaqb/query_builders/filter.rb
|
94
94
|
- lib/yaqb/query_builders/paginate.rb
|
95
|
+
- lib/yaqb/query_builders/paginators/kaminari_helper.rb
|
96
|
+
- lib/yaqb/query_builders/paginators/will_paginate_helper.rb
|
95
97
|
- lib/yaqb/query_builders/query_orchestrator.rb
|
96
98
|
- lib/yaqb/query_builders/sort.rb
|
97
99
|
- lib/yaqb/version.rb
|