search_sort_paginate 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/README.md +29 -0
- data/lib/search_sort_paginate.rb +11 -0
- data/lib/search_sort_paginate/application_helper.rb +22 -0
- data/lib/search_sort_paginate/controller.rb +42 -0
- data/lib/search_sort_paginate/engine.rb +12 -0
- data/lib/search_sort_paginate/version.rb +3 -0
- data/vendor/assets/images/asc.gif +0 -0
- data/vendor/assets/images/desc.gif +0 -0
- data/vendor/assets/stylesheets/sorting.css.scss +12 -0
- metadata +108 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
YmY5ODBiODJkNmE0ZDUwZjE3NGE1YTcwNTU3NjcyMjg3MDE2NDViZQ==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
NTc2ZDRhZTI5NzEwZjRkNzQ2ZGZjZjZmMTk5MDc2ZWNhNGZiNTJhZQ==
|
7
|
+
!binary "U0hBNTEy":
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
MWMzYzU1ODcwZTBjMzM4NWY4MzkxMjg1NmUwYjQ0NTNiMTljNjQyZWM4NWY2
|
10
|
+
MWZjYzJhMjc3OGE3NWE1NTgyODRiYTBkYjZiMjYzMjM4ZTllYzFiYzJkOTcw
|
11
|
+
YmRjOTk2ZGY1NTNkZjYxMDIzNDA4MDQ4ZDM3MTY3NGZmNjIyNzA=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
ODE5MjJlMTRmMjhiZWE1NTMzYjk5YmMxZDg1YjcyNGZjNWQ2ODMzODcxNGRk
|
14
|
+
MWE0YmU0YWJiNTIxNzNjM2IzYzk4MTJmYzFjMWYzNDdmODI2MDU3MDIyM2Fi
|
15
|
+
Y2MwOTY2MDgwMGM3MjFlZWVlOWViOWNhZDcwZTI4YmRmY2M1ODc=
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
search_sort_paginate
|
2
|
+
====================
|
3
|
+
|
4
|
+
Copyright Adam Hallett 2013
|
5
|
+
|
6
|
+
|
7
|
+
In your application stylesheet:
|
8
|
+
|
9
|
+
```scss
|
10
|
+
@import "sorting";
|
11
|
+
```
|
12
|
+
|
13
|
+
In your controller:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
def index
|
17
|
+
@units = search_sort_paginate(params, Unit.scoped)
|
18
|
+
end
|
19
|
+
```
|
20
|
+
|
21
|
+
In your model add the fields that can be searched:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
def self.searchable_fields
|
25
|
+
[
|
26
|
+
:name
|
27
|
+
]
|
28
|
+
end
|
29
|
+
```
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'search_sort_paginate/controller'
|
2
|
+
require 'search_sort_paginate/application_helper'
|
3
|
+
require 'search_sort_paginate/engine'
|
4
|
+
|
5
|
+
ActiveSupport.on_load(:action_controller) do
|
6
|
+
include SearchSortPaginate::Controller
|
7
|
+
end
|
8
|
+
ActiveSupport.on_load(:action_view) do
|
9
|
+
include SearchSortPaginate::ApplicationHelper
|
10
|
+
end
|
11
|
+
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module SearchSortPaginate
|
2
|
+
module ApplicationHelper
|
3
|
+
# http://railscasts.com/episodes/228-sortable-table-columns
|
4
|
+
def sort_link(column, opts = {})
|
5
|
+
title = opts[:title] || column.to_s.titleize
|
6
|
+
css_class = column == params[:sort].try(:to_sym) ? "current #{sort_direction}" : nil
|
7
|
+
|
8
|
+
opts.merge!( class: css_class ) if css_class
|
9
|
+
direction = sort_direction == 'asc' ? 'desc' : 'asc'
|
10
|
+
|
11
|
+
link_to title, params.merge(:sort => column, :direction => direction, :page => nil), opts
|
12
|
+
end
|
13
|
+
|
14
|
+
def sort_direction
|
15
|
+
%w[asc desc].include?(params[:direction]) ? params[:direction] : 'desc'
|
16
|
+
end
|
17
|
+
|
18
|
+
def page_size
|
19
|
+
params[:page].to_i || 8
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module SearchSortPaginate
|
2
|
+
module Controller
|
3
|
+
def search_sort_paginate( params, relation, artificial_attributes = {}, opts = {} )
|
4
|
+
relation = search( params, relation, opts )
|
5
|
+
relation = sort( params, relation, artificial_attributes, opts )
|
6
|
+
relation = paginate( relation, params, opts )
|
7
|
+
end
|
8
|
+
|
9
|
+
def search( params, relation, opts = {} )
|
10
|
+
search = params[:search]
|
11
|
+
if search.present?
|
12
|
+
searchable_fields = relation.klass.searchable_fields || []
|
13
|
+
if searchable_fields.present?
|
14
|
+
search_sql = searchable_fields.map{|s_f| ActiveRecord::Base.send(:sanitize_sql_array,["#{s_f} LIKE ?", "%#{search}%"]) }.join(' || ')
|
15
|
+
relation = relation.where(search_sql)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
relation
|
19
|
+
end
|
20
|
+
|
21
|
+
def paginate( relation, params, opts = {})
|
22
|
+
page_number = params[:page] || 1
|
23
|
+
page_size = params[:page_size] || 8
|
24
|
+
relation.page( page_number ).per( page_size )
|
25
|
+
end
|
26
|
+
|
27
|
+
def sort( params, relation, artificial_attributes, opts = {} )
|
28
|
+
|
29
|
+
column = params[:sort]
|
30
|
+
if column && artificial_attributes[column.to_sym].present?
|
31
|
+
column = artificial_attributes[column.to_sym]
|
32
|
+
end
|
33
|
+
|
34
|
+
direction = params[:direction]
|
35
|
+
if column && direction
|
36
|
+
relation.order(column + ' ' + direction)
|
37
|
+
else
|
38
|
+
relation
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module SearchSortPaginate
|
2
|
+
class Engine < ::Rails::Engine
|
3
|
+
initializer 'search_sort_paginate.assets.precompile' do |app|
|
4
|
+
# assets = %w(images/asc.gif images/desc.gif stylesheets/sorting.css.scss)
|
5
|
+
# asset_path = File.expand_path('../../../vendor/assets',__FILE__)
|
6
|
+
|
7
|
+
# app.config.assets.precompile += assets.map{|asset| File.join(asset_path, asset)}
|
8
|
+
# app.config.action_view.javascript_expansions[:defaults] << assets.map{|asset| File.join(asset_path, asset)}
|
9
|
+
# puts assets.map{|asset| File.join(asset_path, asset)}
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
Binary file
|
Binary file
|
@@ -0,0 +1,12 @@
|
|
1
|
+
.table th a.current.asc {
|
2
|
+
background-image: asset-url("asc.gif", image);
|
3
|
+
cursor: pointer;
|
4
|
+
background-repeat: no-repeat;
|
5
|
+
background-position: center right;
|
6
|
+
padding-right: 16px !important; }
|
7
|
+
.table th a.current.desc {
|
8
|
+
background-image: asset-url("desc.gif", image);
|
9
|
+
cursor: pointer;
|
10
|
+
background-repeat: no-repeat;
|
11
|
+
background-position: center right;
|
12
|
+
padding-right: 16px !important; }
|
metadata
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: search_sort_paginate
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Adam Hallett
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-05-31 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ! '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ! '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rails
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
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: railties
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
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: kaminari
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ! '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: SearchSortPaginate has convenience methods that hook into your controllers.
|
70
|
+
email:
|
71
|
+
- adam.t.hallett@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- lib/search_sort_paginate/application_helper.rb
|
77
|
+
- lib/search_sort_paginate/controller.rb
|
78
|
+
- lib/search_sort_paginate/engine.rb
|
79
|
+
- lib/search_sort_paginate/version.rb
|
80
|
+
- lib/search_sort_paginate.rb
|
81
|
+
- vendor/assets/images/asc.gif
|
82
|
+
- vendor/assets/images/desc.gif
|
83
|
+
- vendor/assets/stylesheets/sorting.css.scss
|
84
|
+
- README.md
|
85
|
+
homepage: http://github.com/atomical/search_sort_paginate
|
86
|
+
licenses: []
|
87
|
+
metadata: {}
|
88
|
+
post_install_message:
|
89
|
+
rdoc_options: []
|
90
|
+
require_paths:
|
91
|
+
- lib
|
92
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ! '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
requirements: []
|
103
|
+
rubyforge_project:
|
104
|
+
rubygems_version: 2.0.3
|
105
|
+
signing_key:
|
106
|
+
specification_version: 4
|
107
|
+
summary: An easy way to do vanilla search, sort, and paginate on a collection.
|
108
|
+
test_files: []
|