api_helper 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/api_helper/sortable.rb +14 -12
- data/lib/api_helper/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4c9251a106ef4147f1bdeafd3c63c6bb772bcefe
|
4
|
+
data.tar.gz: 3f4b4a7493bfe73371c3d9f0879263b367d8b14d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 994a9811ac0a139ceff2cc1c80af5e2503d41fb30d39ad2c9b3d32a1f1e37759133ee422dc1ce8e40e6c027c2fc82c68b877bbc2ec4b9a2f39813841075eed48
|
7
|
+
data.tar.gz: 1eac3f4b577c4107ca7031d060f4390509d544f8a51ee13c27c5d2a2fbc2799ebef267bbd8582f853d770709a14b8773f971d5164bd6a4a16739ef7e480428d7
|
data/lib/api_helper/sortable.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'active_support'
|
2
2
|
|
3
|
-
# =
|
3
|
+
# = Sortable
|
4
4
|
#
|
5
5
|
# A Sortable Resource API gives the flexibility to change how the returned data
|
6
6
|
# is sorted to the client. Clients can use the +sort+ URL parameter to control
|
@@ -22,7 +22,7 @@ require 'active_support'
|
|
22
22
|
# or in your Grape API class:
|
23
23
|
#
|
24
24
|
# class SampleAPI < Grape::API
|
25
|
-
#
|
25
|
+
# helpers APIHelper::Sortable
|
26
26
|
# end
|
27
27
|
#
|
28
28
|
# then use the +sortable+ method like this:
|
@@ -30,15 +30,16 @@ require 'active_support'
|
|
30
30
|
# resources :posts do
|
31
31
|
# get do
|
32
32
|
# sortable default_order: { created_at: :desc }
|
33
|
-
#
|
34
|
-
#
|
33
|
+
# @posts = Post.order(sortable_sort)
|
34
|
+
#
|
35
35
|
# # ...
|
36
36
|
# end
|
37
37
|
# end
|
38
|
+
#
|
38
39
|
module APIHelper::Sortable
|
39
40
|
extend ActiveSupport::Concern
|
40
41
|
|
41
|
-
# Gets the
|
42
|
+
# Gets the +sort+ parameter with the format 'resourses?sort=-created_at,name',
|
42
43
|
# verify and converts it into an safe Hash that can be passed into the .order
|
43
44
|
# method.
|
44
45
|
#
|
@@ -46,31 +47,32 @@ module APIHelper::Sortable
|
|
46
47
|
#
|
47
48
|
# +default_order+::
|
48
49
|
# +Hash+ the default value to return if the sort parameter is not provided
|
50
|
+
#
|
49
51
|
def sortable(default_order: {})
|
50
52
|
# get the parameter
|
51
53
|
sort_by = params[:sort] || params[:sort_by]
|
52
54
|
|
53
|
-
if sort_by.is_a?
|
55
|
+
if sort_by.is_a?(String)
|
54
56
|
# split it
|
55
57
|
sort_by_attrs = sort_by.gsub(/[^a-zA-Z0-9\-_,]/, '').split(',')
|
56
58
|
|
57
59
|
# save it
|
58
|
-
@
|
60
|
+
@sortable_sort = {}
|
59
61
|
sort_by_attrs.each do |attrb|
|
60
62
|
if attrb.match(/^-/)
|
61
|
-
@
|
63
|
+
@sortable_sort[attrb.gsub(/^-/, '')] = :desc
|
62
64
|
else
|
63
|
-
@
|
65
|
+
@sortable_sort[attrb] = :asc
|
64
66
|
end
|
65
67
|
end
|
66
68
|
else
|
67
|
-
@
|
69
|
+
@sortable_sort = default_order
|
68
70
|
end
|
69
71
|
end
|
70
72
|
|
71
73
|
# Helper to get the sort data
|
72
|
-
def
|
73
|
-
@
|
74
|
+
def sortable_sort
|
75
|
+
@sortable_sort
|
74
76
|
end
|
75
77
|
|
76
78
|
# Return the 'sort' param description
|
data/lib/api_helper/version.rb
CHANGED