yap 0.0.2 → 0.1.1
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/lib/yap.rb +22 -11
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a6132b923e8420349b899bae89b59facfd4b3e3f
|
4
|
+
data.tar.gz: a951a562f70ad37f9ab830f4816a028c08da99da
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ca05f1a2d8d221441b19bd30936a57868b726263f00425feac3ac8aa2f3075508c3b36cc148419a0c4ae0e91ce8f87f78d9e20da5e51c8d01b629b29a3cf4e26
|
7
|
+
data.tar.gz: 195d596aae8ef0dcf931642b68049fce688220ef00b4a92609e513a8287c41c49b2cd040927c7730af09a2d98baaa2cc9beb371ee76abbceb9644979869b458e
|
data/lib/yap.rb
CHANGED
@@ -18,8 +18,8 @@ require 'active_support/concern'
|
|
18
18
|
module Yap
|
19
19
|
extend ActiveSupport::Concern
|
20
20
|
|
21
|
-
DEFAULTS = Struct.new(:page, :per_page, :sort, :direction)
|
22
|
-
.new(1, 10,
|
21
|
+
DEFAULTS = Struct.new(:page, :per_page, :sort, :direction, :disable_warnings)
|
22
|
+
.new(1, 10, 'id', 'ASC', false)
|
23
23
|
|
24
24
|
def self.configure
|
25
25
|
raise ArgumentError, 'No block given.' unless block_given?
|
@@ -30,8 +30,8 @@ module Yap
|
|
30
30
|
extend ClassMethods
|
31
31
|
|
32
32
|
scope :paginate, -> (params = {}) {
|
33
|
-
page, per_page,
|
34
|
-
limit(per_page).offset((page-1)*per_page).order(
|
33
|
+
page, per_page, column, direction = extract_pagination_params(params)
|
34
|
+
limit(per_page).offset((page-1)*per_page).order("#{column} #{direction}")
|
35
35
|
}
|
36
36
|
|
37
37
|
private
|
@@ -39,7 +39,7 @@ module Yap
|
|
39
39
|
def self.extract_pagination_params(params)
|
40
40
|
page = extract_number(params[:page], DEFAULTS.page)
|
41
41
|
per_page = extract_number(params[:per_page], DEFAULTS.per_page)
|
42
|
-
sort =
|
42
|
+
sort = extract_column(params[:sort])
|
43
43
|
direction = extract_direction(params[:direction])
|
44
44
|
|
45
45
|
return page, per_page, sort, direction
|
@@ -56,17 +56,28 @@ module Yap
|
|
56
56
|
number
|
57
57
|
end
|
58
58
|
|
59
|
-
def self.
|
60
|
-
|
61
|
-
raise PaginationError.new("Cannot sort by '#{sort}'.") unless
|
62
|
-
|
59
|
+
def self.extract_column(sort)
|
60
|
+
column = map_column(sort.present? ? sort.to_s.downcase : DEFAULTS.sort)
|
61
|
+
raise PaginationError.new("Cannot sort by '#{sort}'.") unless column
|
62
|
+
column
|
63
63
|
end
|
64
64
|
|
65
65
|
def self.extract_direction(direction)
|
66
|
-
dir = direction.present? ? direction.
|
67
|
-
raise PaginationError.new("'#{direction}' is not a valid direction. Use 'asc' or 'desc'.") unless [
|
66
|
+
dir = direction.present? ? direction.to_s.upcase : DEFAULTS.direction
|
67
|
+
raise PaginationError.new("'#{direction}' is not a valid direction. Use 'asc' or 'desc'.") unless %w[ASC DESC].include? dir
|
68
68
|
dir
|
69
69
|
end
|
70
|
+
|
71
|
+
private
|
72
|
+
|
73
|
+
def self.map_column(name)
|
74
|
+
begin
|
75
|
+
map_name_to_column(name)
|
76
|
+
rescue
|
77
|
+
warn "#{self.name} does not implement map_name_to_column. If you do not need column mapping set disable_warnings=true" unless DEFAULTS.disable_warnings
|
78
|
+
nil
|
79
|
+
end || (column_names.include?(name) ? name : nil)
|
80
|
+
end
|
70
81
|
end
|
71
82
|
|
72
83
|
module ClassMethods
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yap
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Finn Glöe
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-01-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -66,7 +66,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
66
66
|
version: '0'
|
67
67
|
requirements: []
|
68
68
|
rubyforge_project:
|
69
|
-
rubygems_version: 2.4.
|
69
|
+
rubygems_version: 2.4.5
|
70
70
|
signing_key:
|
71
71
|
specification_version: 4
|
72
72
|
summary: Yet another paginator for Ruby on Rails
|