filterer 0.0.3 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/filterer/base.rb +81 -2
- data/lib/filterer/paginator.rb +7 -7
- data/lib/filterer/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7734d274d4ea4be3fa564c0d94a0b1a6fbb0910f
|
4
|
+
data.tar.gz: 09622d7c585ed55a808810ac358a1a052b5f4da5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ba86f46c95b642b8606fdc5aac56e895c5dc5b4a4920f0be1bcb93a1eecf3b064836518b90e45c8657929819010b29bba5152ac605dbbc963231688b203ad6d4
|
7
|
+
data.tar.gz: 928079d0abddddf7a0c6b15a781332bc0bb52935190df90226852a2b417eeb9cd1f0d7a162d174311ceb41ab5e5850c25276c4e6b8313bace2c4c3347fee6aa0
|
data/lib/filterer/base.rb
CHANGED
@@ -1,9 +1,51 @@
|
|
1
1
|
module Filterer
|
2
2
|
class Base
|
3
3
|
|
4
|
+
IGNORED_PARAMS = [:page]
|
5
|
+
|
4
6
|
attr_accessor :results, :meta, :direction, :sort, :params
|
5
7
|
|
6
|
-
|
8
|
+
class << self
|
9
|
+
attr_accessor :sort_options
|
10
|
+
|
11
|
+
def sort_options
|
12
|
+
@sort_options ||= []
|
13
|
+
end
|
14
|
+
|
15
|
+
def inherited(subclass)
|
16
|
+
if @sort_options.present?
|
17
|
+
subclass.sort_options = @sort_options
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def sort_option(key, query_string_or_proc = nil, opts = {})
|
22
|
+
if query_string_or_proc.is_a?(Hash)
|
23
|
+
opts, query_string_or_proc = query_string_or_proc.clone, nil
|
24
|
+
end
|
25
|
+
|
26
|
+
if !query_string_or_proc
|
27
|
+
if key.is_a?(String)
|
28
|
+
query_string_or_proc = key
|
29
|
+
else
|
30
|
+
raise 'Please provide a query string or a proc.'
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
if key.is_a?(Regexp) && opts[:default]
|
35
|
+
raise "Default sort option can't have a Regexp key."
|
36
|
+
end
|
37
|
+
|
38
|
+
if query_string_or_proc.is_a?(Proc) && opts[:tiebreaker]
|
39
|
+
raise "Tiebreaker can't be a proc."
|
40
|
+
end
|
41
|
+
|
42
|
+
sort_options << {
|
43
|
+
key: key,
|
44
|
+
query_string_or_proc: query_string_or_proc,
|
45
|
+
opts: opts
|
46
|
+
}
|
47
|
+
end
|
48
|
+
end
|
7
49
|
|
8
50
|
def initialize(params = {}, opts = {})
|
9
51
|
@params, @opts = params, opts
|
@@ -54,9 +96,46 @@ module Filterer
|
|
54
96
|
end
|
55
97
|
|
56
98
|
def order_results
|
57
|
-
|
99
|
+
@direction = @params[:direction] == 'desc' ? 'DESC' : 'ASC'
|
100
|
+
@sort = (params[:sort] && get_sort_option(params[:sort])) ? params[:sort] : default_sort_param
|
101
|
+
return unless get_sort_option(@sort)
|
102
|
+
|
103
|
+
if get_sort_option(@sort)[:query_string_or_proc].is_a?(String)
|
104
|
+
@results = @results.order %Q{
|
105
|
+
#{get_sort_option(@sort)[:query_string_or_proc]}
|
106
|
+
#{@direction}
|
107
|
+
#{get_sort_option(@sort)[:opts][:nulls_last] ? 'NULLS LAST' : ''}
|
108
|
+
#{tiebreaker_sort_string ? ',' + tiebreaker_sort_string : ''}
|
109
|
+
}.squish
|
110
|
+
elsif get_sort_option(@sort)[:query_string_or_proc].is_a?(Proc)
|
111
|
+
matches = get_sort_option(@sort)[:key].is_a?(Regexp) ? @sort.match(get_sort_option(@sort)[:key]) : nil
|
112
|
+
@results = get_sort_option(@sort)[:query_string_or_proc].call(@results, matches)
|
113
|
+
end
|
58
114
|
end
|
59
115
|
|
116
|
+
def get_sort_option(x)
|
117
|
+
self.class.sort_options.find { |sort_option|
|
118
|
+
if sort_option[:key].is_a?(Regexp)
|
119
|
+
x.match(sort_option[:key])
|
120
|
+
else # String
|
121
|
+
x == sort_option[:key]
|
122
|
+
end
|
123
|
+
}
|
124
|
+
end
|
125
|
+
|
126
|
+
def default_sort_param
|
127
|
+
self.class.sort_options.find { |sort_option|
|
128
|
+
sort_option[:opts][:default]
|
129
|
+
}.try(:[], :key)
|
130
|
+
end
|
131
|
+
|
132
|
+
def tiebreaker_sort_string
|
133
|
+
self.class.sort_options.find { |sort_option|
|
134
|
+
sort_option[:opts][:tiebreaker]
|
135
|
+
}.try(:[], :query_string_or_proc)
|
136
|
+
end
|
137
|
+
|
138
|
+
|
60
139
|
def starting_query
|
61
140
|
raise 'You must override this method!'
|
62
141
|
end
|
data/lib/filterer/paginator.rb
CHANGED
@@ -3,9 +3,9 @@ module Filterer
|
|
3
3
|
|
4
4
|
attr_reader :pages
|
5
5
|
|
6
|
-
def initialize(
|
7
|
-
@
|
8
|
-
return @pages = [1] if @
|
6
|
+
def initialize(filterer)
|
7
|
+
@filterer = filterer
|
8
|
+
return @pages = [1] if @filterer.meta[:last_page] == 1
|
9
9
|
push_default_pages
|
10
10
|
calculate_additional_pages
|
11
11
|
add_breaks
|
@@ -13,14 +13,14 @@ module Filterer
|
|
13
13
|
|
14
14
|
def push_default_pages
|
15
15
|
@pages = [1, 2]
|
16
|
-
push_page(@
|
16
|
+
push_page(@filterer.meta[:last_page], @filterer.meta[:last_page] - 1)
|
17
17
|
end
|
18
18
|
|
19
19
|
def calculate_additional_pages
|
20
20
|
offset = 0
|
21
|
-
current_page = @
|
21
|
+
current_page = @filterer.meta[:page]
|
22
22
|
|
23
|
-
while @pages.length < 11 && ( (current_page - offset >= 1) || (current_page + offset <= @
|
23
|
+
while @pages.length < 11 && ( (current_page - offset >= 1) || (current_page + offset <= @filterer.meta[:last_page]) ) do
|
24
24
|
push_page(current_page - offset, current_page + offset)
|
25
25
|
offset += 1
|
26
26
|
end
|
@@ -43,7 +43,7 @@ module Filterer
|
|
43
43
|
|
44
44
|
def push_page(*args)
|
45
45
|
args.each do |page|
|
46
|
-
@pages.push(page) unless @pages.include?(page) || (page > @
|
46
|
+
@pages.push(page) unless @pages.include?(page) || (page > @filterer.meta[:last_page]) || (page < 1)
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
data/lib/filterer/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: filterer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Becker
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-12-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|