seek 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +7 -7
- data/lib/seek.rb +3 -3
- data/lib/seek/sorting.rb +10 -10
- data/lib/seek/version.rb +2 -2
- data/spec/lib/seek_spec.rb +8 -8
- metadata +1 -1
data/README.md
CHANGED
@@ -13,20 +13,20 @@ gem install seek
|
|
13
13
|
# Examples
|
14
14
|
|
15
15
|
```ruby
|
16
|
-
params = { page: '4',
|
16
|
+
params = { page: '4', sort_direction: 'desc', per_page: '1000' }
|
17
17
|
|
18
18
|
seek = Seek.new params,
|
19
19
|
valid_sort_bys: %w{ first_name, last_name, age },
|
20
20
|
max_per_page: 50,
|
21
21
|
default_per_page: 10,
|
22
22
|
default_sort_by: :age,
|
23
|
-
|
23
|
+
default_sort_direction: :asc
|
24
24
|
|
25
|
-
seek.page
|
26
|
-
seek.per_page
|
27
|
-
seek.sort_by
|
28
|
-
seek.
|
29
|
-
seek.order
|
25
|
+
seek.page #=> 4
|
26
|
+
seek.per_page #=> 50
|
27
|
+
seek.sort_by #=> 'age'
|
28
|
+
seek.sort_direction #=> 'desc
|
29
|
+
seek.order #=> 'age desc'
|
30
30
|
|
31
31
|
seek.to_hash #=> Returns a Hash with all the parameters listed above.
|
32
32
|
```
|
data/lib/seek.rb
CHANGED
@@ -16,12 +16,12 @@ class Seek
|
|
16
16
|
self.page = params[:page]
|
17
17
|
self.per_page = params[:per_page]
|
18
18
|
self.sort_by = params[:sort_by]
|
19
|
-
self.
|
19
|
+
self.sort_direction = params[:sort_direction]
|
20
20
|
self.max_per_page = options[:max_per_page]
|
21
21
|
self.valid_sort_bys = options[:valid_sort_bys]
|
22
22
|
self.default_per_page = options[:default_per_page]
|
23
23
|
self.default_sort_by = options[:default_sort_by]
|
24
|
-
self.
|
24
|
+
self.default_sort_direction = options[:default_sort_direction]
|
25
25
|
end
|
26
26
|
|
27
27
|
def to_hash
|
@@ -29,7 +29,7 @@ class Seek
|
|
29
29
|
page: page,
|
30
30
|
per_page: per_page,
|
31
31
|
sort_by: sort_by,
|
32
|
-
|
32
|
+
sort_direction: sort_direction,
|
33
33
|
order: order,
|
34
34
|
}
|
35
35
|
end
|
data/lib/seek/sorting.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
class Seek
|
2
2
|
module Sorting
|
3
3
|
|
4
|
-
attr_writer :sort_by, :
|
4
|
+
attr_writer :sort_by, :sort_direction, :valid_sort_bys, :default_sort_by, :default_sort_direction
|
5
5
|
|
6
|
-
# Returns true if sort_by and
|
6
|
+
# Returns true if sort_by and sort_direction are present.
|
7
7
|
#
|
8
8
|
def sortable?
|
9
9
|
!!order
|
@@ -17,9 +17,9 @@ class Seek
|
|
17
17
|
default_sort_by
|
18
18
|
end
|
19
19
|
|
20
|
-
def
|
21
|
-
result = @
|
22
|
-
|
20
|
+
def sort_direction
|
21
|
+
result = @sort_direction.to_s.downcase
|
22
|
+
valid_sort_directions.include?(result) ? result : valid_sort_directions.first
|
23
23
|
end
|
24
24
|
|
25
25
|
# Useful for queries that require the format "first_name desc" (such as SQL or lucene)
|
@@ -28,7 +28,7 @@ class Seek
|
|
28
28
|
#
|
29
29
|
def order
|
30
30
|
return if sort_by.to_s.empty?
|
31
|
-
[sort_by,
|
31
|
+
[sort_by, sort_direction].join(' ')
|
32
32
|
end
|
33
33
|
|
34
34
|
def valid_sort_bys
|
@@ -42,14 +42,14 @@ class Seek
|
|
42
42
|
valid_sort_bys.include?(result) ? result : valid_sort_bys.first
|
43
43
|
end
|
44
44
|
|
45
|
-
def
|
46
|
-
result = @
|
47
|
-
|
45
|
+
def default_sort_direction
|
46
|
+
result = @default_sort_direction.to_s.downcase
|
47
|
+
valid_sort_directions.include?(result) ? result : valid_sort_directions.first
|
48
48
|
end
|
49
49
|
|
50
50
|
private
|
51
51
|
|
52
|
-
def
|
52
|
+
def valid_sort_directions
|
53
53
|
%w{ asc desc }
|
54
54
|
end
|
55
55
|
|
data/lib/seek/version.rb
CHANGED
data/spec/lib/seek_spec.rb
CHANGED
@@ -2,7 +2,7 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Seek do
|
4
4
|
|
5
|
-
let(:params) { { page: 4, per_page: 20, 'sort_by' => :first_name,
|
5
|
+
let(:params) { { page: 4, per_page: 20, 'sort_by' => :first_name, sort_direction: 'DESC' } }
|
6
6
|
let(:options) { { 'max_per_page' => 30, valid_sort_bys: %w{ age first_name last_name } } }
|
7
7
|
|
8
8
|
let(:seek) { Seek.new params, options }
|
@@ -76,23 +76,23 @@ describe Seek do
|
|
76
76
|
end
|
77
77
|
end
|
78
78
|
|
79
|
-
describe '#
|
79
|
+
describe '#sort_direction' do
|
80
80
|
it 'comes from the params and is a String' do
|
81
|
-
seek.
|
81
|
+
seek.sort_direction.should == 'desc'
|
82
82
|
end
|
83
83
|
|
84
84
|
it 'is ascending if nothing is specified' do
|
85
|
-
Seek.new.
|
85
|
+
Seek.new.sort_direction.should == 'asc'
|
86
86
|
end
|
87
87
|
|
88
88
|
it 'is ascending if something invalid is specified' do
|
89
|
-
Seek.new(
|
89
|
+
Seek.new(sort_direction: 'vertically').sort_direction.should == 'asc'
|
90
90
|
end
|
91
91
|
end
|
92
92
|
end
|
93
93
|
|
94
94
|
describe '#order' do
|
95
|
-
it 'is sort_by and
|
95
|
+
it 'is sort_by and sort_direction separated by a space' do
|
96
96
|
seek.order.should == 'first_name desc'
|
97
97
|
end
|
98
98
|
|
@@ -102,7 +102,7 @@ describe Seek do
|
|
102
102
|
end
|
103
103
|
|
104
104
|
describe '#sortable?' do
|
105
|
-
it 'is true if there is sort_by and
|
105
|
+
it 'is true if there is sort_by and sort_direction' do
|
106
106
|
seek.should be_sortable
|
107
107
|
end
|
108
108
|
|
@@ -113,7 +113,7 @@ describe Seek do
|
|
113
113
|
|
114
114
|
describe '#to_hash' do
|
115
115
|
it 'contains all params' do
|
116
|
-
seek.to_hash.should == { page: 4, per_page: 20, sort_by: 'first_name',
|
116
|
+
seek.to_hash.should == { page: 4, per_page: 20, sort_by: 'first_name', sort_direction: 'desc', order: 'first_name desc' }
|
117
117
|
end
|
118
118
|
end
|
119
119
|
|