filterer 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Rakefile +24 -0
- data/app/helpers/filterer/pagination_helper.rb +51 -0
- data/lib/filterer.rb +6 -0
- data/lib/filterer/base.rb +70 -0
- data/lib/filterer/engine.rb +5 -0
- data/lib/filterer/paginator.rb +51 -0
- data/lib/filterer/version.rb +3 -0
- data/lib/tasks/filterer_tasks.rake +4 -0
- metadata +184 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f9c55094d34e875077e3903ba1ed4acbb282d7c9
|
4
|
+
data.tar.gz: 5b92580dcd86e828e61d4f65f0ace15355dce657
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8e94fc28aba2a14e917d69b0b9c2eb0de0200f4c2e1c7853567d5a5456707147d3cfce297e01a690e30db5934a15bfdd1cc3e7619a7c70aa2ed0603c5f69daed
|
7
|
+
data.tar.gz: c8d31b9d5bcd03cca0e9647317d3a50cdd657f17811bf39fe0debb2640fcf99c830fef4556b86b83a1a997c611b1d3d37b3c22fd5bc9b912d11ef72c53e04d96
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'Formbuilder'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.rdoc')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
|
18
|
+
load 'rails/tasks/engine.rake'
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
Bundler::GemHelper.install_tasks
|
23
|
+
|
24
|
+
require 'rake/testtask'
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module Filterer
|
2
|
+
module PaginationHelper
|
3
|
+
|
4
|
+
def render_filterer_pagination(filterer)
|
5
|
+
content_tag(:div, class: 'pagination-wrapper') do
|
6
|
+
content_tag(:ul, class: 'unstyled') do
|
7
|
+
render_filterer_previous_link(filterer) +
|
8
|
+
filterer.paginator.pages.map { |p| render_filterer_page_link(filterer, p) }.join('').html_safe +
|
9
|
+
render_filterer_next_link(filterer)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
def calculate_filterer_pagination_url(page)
|
16
|
+
url_for(params.merge(page: page))
|
17
|
+
end
|
18
|
+
|
19
|
+
def render_filterer_previous_link(filterer)
|
20
|
+
content_tag(:li, class: filterer.meta[:page] == 1 ? "disabled" : '') do
|
21
|
+
if filterer.meta[:page] == 1
|
22
|
+
content_tag(:span) { '‹' }
|
23
|
+
else
|
24
|
+
content_tag(:a, class: 'pagination-previous',
|
25
|
+
href: calculate_filterer_pagination_url(filterer.meta[:page] - 1)) { '‹' }
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def render_filterer_next_link(filterer)
|
31
|
+
content_tag(:li, class: filterer.meta[:page] == filterer.meta[:last_page] ? "disabled" : '') do
|
32
|
+
if filterer.meta[:page] == filterer.meta[:last_page]
|
33
|
+
content_tag(:span) { '›' }
|
34
|
+
else
|
35
|
+
content_tag(:a, class: 'pagination-next', href: calculate_filterer_pagination_url(filterer.meta[:page] + 1)) { '›' }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def render_filterer_page_link(filterer, p)
|
41
|
+
if p == 'break'
|
42
|
+
"<li><span>…</span></li>"
|
43
|
+
else
|
44
|
+
content_tag(:li, class: p == filterer.meta[:page] ? 'active' : '') do
|
45
|
+
content_tag(:a, href: calculate_filterer_pagination_url(p)) { p.to_s }
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
data/lib/filterer.rb
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
module Filterer
|
2
|
+
class Base
|
3
|
+
|
4
|
+
attr_accessor :results, :meta, :direction, :sort, :params
|
5
|
+
|
6
|
+
IGNORED_PARAMS = [:page]
|
7
|
+
|
8
|
+
def initialize(params = {}, opts = {})
|
9
|
+
@params, @opts = params, opts
|
10
|
+
setup_meta
|
11
|
+
find_results
|
12
|
+
end
|
13
|
+
|
14
|
+
def paginator
|
15
|
+
@paginator ||= Filterer::Paginator.new(self)
|
16
|
+
end
|
17
|
+
|
18
|
+
def setup_meta
|
19
|
+
@meta = {
|
20
|
+
page: [@params[:page].to_i, 1].max,
|
21
|
+
per_page: 10
|
22
|
+
}
|
23
|
+
end
|
24
|
+
|
25
|
+
def find_results
|
26
|
+
@results = starting_query
|
27
|
+
|
28
|
+
# Add params
|
29
|
+
add_params_to_query
|
30
|
+
|
31
|
+
# Order results
|
32
|
+
order_results
|
33
|
+
|
34
|
+
@meta[:total] = @results.count
|
35
|
+
@meta[:last_page] = [(@meta[:total].to_f / @meta[:per_page]).ceil, 1].max
|
36
|
+
@meta[:page] = [@meta[:last_page], @meta[:page]].min
|
37
|
+
|
38
|
+
return if @opts[:count_only]
|
39
|
+
|
40
|
+
# Add custom meta data if we've defined the method
|
41
|
+
@meta.merge!(self.custom_meta_data) if self.respond_to?(:custom_meta_data)
|
42
|
+
|
43
|
+
@results = @results.limit(@meta[:per_page]).offset((@meta[:page] - 1)*@meta[:per_page])
|
44
|
+
end
|
45
|
+
|
46
|
+
def add_params_to_query
|
47
|
+
@params.reject { |k, v| k.in?(IGNORED_PARAMS) }
|
48
|
+
.select { |k, v| v.present? }
|
49
|
+
.each do |k, v|
|
50
|
+
|
51
|
+
method_name = :"param_#{k}"
|
52
|
+
@results = respond_to?(method_name) ? send(method_name, v) : @results
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def order_results
|
57
|
+
# noop
|
58
|
+
end
|
59
|
+
|
60
|
+
def starting_query
|
61
|
+
raise 'You must override this method!'
|
62
|
+
end
|
63
|
+
|
64
|
+
def self.count(params = {}, opts = {})
|
65
|
+
filterer = self.new(params, { count_only: true }.merge(opts))
|
66
|
+
return filterer.meta[:total]
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module Filterer
|
2
|
+
class Paginator
|
3
|
+
|
4
|
+
attr_reader :pages
|
5
|
+
|
6
|
+
def initialize(searcher)
|
7
|
+
@searcher = searcher
|
8
|
+
return @pages = [1] if @searcher.meta[:last_page] == 1
|
9
|
+
push_default_pages
|
10
|
+
calculate_additional_pages
|
11
|
+
add_breaks
|
12
|
+
end
|
13
|
+
|
14
|
+
def push_default_pages
|
15
|
+
@pages = [1, 2]
|
16
|
+
push_page(@searcher.meta[:last_page], @searcher.meta[:last_page] - 1)
|
17
|
+
end
|
18
|
+
|
19
|
+
def calculate_additional_pages
|
20
|
+
offset = 0
|
21
|
+
current_page = @searcher.meta[:page]
|
22
|
+
|
23
|
+
while @pages.length < 11 && ( (current_page - offset >= 1) || (current_page + offset <= @searcher.meta[:last_page]) ) do
|
24
|
+
push_page(current_page - offset, current_page + offset)
|
25
|
+
offset += 1
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def add_breaks
|
30
|
+
pages_without_breaks = @pages.sort
|
31
|
+
pages_with_breaks = []
|
32
|
+
|
33
|
+
pages_without_breaks.each_with_index do |p, i|
|
34
|
+
if pages_without_breaks[i - 1] && (p - pages_without_breaks[i - 1] > 1)
|
35
|
+
pages_with_breaks.push 'break'
|
36
|
+
end
|
37
|
+
|
38
|
+
pages_with_breaks.push p
|
39
|
+
end
|
40
|
+
|
41
|
+
@pages = pages_with_breaks
|
42
|
+
end
|
43
|
+
|
44
|
+
def push_page(*args)
|
45
|
+
args.each do |page|
|
46
|
+
@pages.push(page) unless @pages.include?(page) || (page > @searcher.meta[:last_page]) || (page < 1)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
metadata
ADDED
@@ -0,0 +1,184 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: filterer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Adam Becker
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-11-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 4.0.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 4.0.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: capybara
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: coveralls
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: database_cleaner
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: guard-rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: launchy
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rspec-rails
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - '>='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: sqlite3
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - '>='
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: terminal-notifier-guard
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - '>='
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - '>='
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
description: Filterer lets your users easily filter results from your ActiveRecord
|
140
|
+
models. What does that mean? Let's imagine a page in your application that lists
|
141
|
+
the results of Person.all. What if you want to let your users filter the results
|
142
|
+
by name? Or email? Or whether or not the Person is an admin? Where does that logic
|
143
|
+
go? One answer could be your controller. That gets ugly fast. Another answer could
|
144
|
+
be in your models. But passing a bunch of query parameters to a model isn't really
|
145
|
+
a good practice either. Enter Filterer.
|
146
|
+
email:
|
147
|
+
- adam@dobt.co
|
148
|
+
executables: []
|
149
|
+
extensions: []
|
150
|
+
extra_rdoc_files: []
|
151
|
+
files:
|
152
|
+
- app/helpers/filterer/pagination_helper.rb
|
153
|
+
- lib/filterer/base.rb
|
154
|
+
- lib/filterer/engine.rb
|
155
|
+
- lib/filterer/paginator.rb
|
156
|
+
- lib/filterer/version.rb
|
157
|
+
- lib/filterer.rb
|
158
|
+
- lib/tasks/filterer_tasks.rake
|
159
|
+
- Rakefile
|
160
|
+
homepage: https://github.com/dobtco/filterer
|
161
|
+
licenses:
|
162
|
+
- MIT
|
163
|
+
metadata: {}
|
164
|
+
post_install_message:
|
165
|
+
rdoc_options: []
|
166
|
+
require_paths:
|
167
|
+
- lib
|
168
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
169
|
+
requirements:
|
170
|
+
- - '>='
|
171
|
+
- !ruby/object:Gem::Version
|
172
|
+
version: '0'
|
173
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
174
|
+
requirements:
|
175
|
+
- - '>='
|
176
|
+
- !ruby/object:Gem::Version
|
177
|
+
version: '0'
|
178
|
+
requirements: []
|
179
|
+
rubyforge_project:
|
180
|
+
rubygems_version: 2.1.11
|
181
|
+
signing_key:
|
182
|
+
specification_version: 4
|
183
|
+
summary: Easily filter results from your ActiveRecord models.
|
184
|
+
test_files: []
|