orderable2 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +107 -0
- data/Rakefile +23 -0
- data/lib/orderable2.rb +22 -0
- data/lib/orderable2/orderable_helper.rb +37 -0
- data/lib/orderable2/railtie.rb +11 -0
- data/lib/orderable2/version.rb +3 -0
- data/lib/tasks/orderable2_tasks.rake +4 -0
- metadata +81 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 59d76b8dcf698b6b2e6fbfee7b6b96cee201918e
|
4
|
+
data.tar.gz: 88b254c8f472f62a9e34422129a573b9a31778b9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 20ee516e198007afdc41c88d19a759843285acfbc5b37cb58e0e96a691c6226067c6f269a04353808fcf507a2dfeefad2987f6160abcfc4ee77732579b4dfff0
|
7
|
+
data.tar.gz: 7c00d66ca944aba80e803ef2f64ef292f3feef2990e4d5613daa30abec6f24a78587c9ad20dc2a8c19fb88a6261363171dab279d1fabbd5bb4448e1da3ffaca3
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2017 Lukas_Skywalker
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
# Orderable2
|
2
|
+
|
3
|
+
User defined sorting of ActiveRecord queries for Rails
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'orderable2'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
```bash
|
15
|
+
$ bundle install
|
16
|
+
```
|
17
|
+
|
18
|
+
## Usage
|
19
|
+
|
20
|
+
### Controllers
|
21
|
+
|
22
|
+
* Include the gem in the controller you wish to be sortable
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
class UsersController < ApplicationController
|
26
|
+
include Orderable2
|
27
|
+
|
28
|
+
[...]
|
29
|
+
end
|
30
|
+
```
|
31
|
+
|
32
|
+
* Order your collection using the `sort_order` method
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
class UsersController < ApplicationController
|
36
|
+
include Orderable2
|
37
|
+
|
38
|
+
def index
|
39
|
+
@users = User.all.order(sort_order)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
```
|
43
|
+
|
44
|
+
This uses the order provided in the params or falls back to ordering by ascending ID if no order parameter is present in the request.
|
45
|
+
|
46
|
+
You can also override the default order in the sort_order call:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
class UsersController < ApplicationController
|
50
|
+
include Orderable2
|
51
|
+
|
52
|
+
def index
|
53
|
+
@users = User.all.order(sort_order(email: :asc, id: :asc))
|
54
|
+
end
|
55
|
+
end
|
56
|
+
```
|
57
|
+
|
58
|
+
This would sort by ascending email and ascending ID if no ordering params are present in the request.
|
59
|
+
|
60
|
+
To set the ordering using request parameters, set a parameter named `order` containing a sorting definition in the following format:
|
61
|
+
|
62
|
+
```
|
63
|
+
attribute:direction
|
64
|
+
```
|
65
|
+
|
66
|
+
For example:
|
67
|
+
|
68
|
+
```
|
69
|
+
country:asc
|
70
|
+
```
|
71
|
+
|
72
|
+
Multiple columns may be defined by separating them by comma:
|
73
|
+
|
74
|
+
```
|
75
|
+
country:asc,id:asc
|
76
|
+
```
|
77
|
+
|
78
|
+
So, to create a link that sorts users by country, you could use the following
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
users_path(order: 'country:asc')
|
82
|
+
```
|
83
|
+
|
84
|
+
### Views
|
85
|
+
|
86
|
+
Orderable2 includes view helpers that simplify usage in the views.
|
87
|
+
|
88
|
+
In order to print the label of a table column or filter, use the following helper
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
<%= orderable_for :country %>
|
92
|
+
```
|
93
|
+
|
94
|
+
This automatically creates the necessary label and displays the current sort order using ↑ or ↓. Clicking on the label reorders by that column.
|
95
|
+
|
96
|
+
By default, the helper lets you sort by multiple columns, for example username ASC, country DESC. If you wish to disable this behaviour and only allow sorting based on a single column, use the `single` option:
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
<%= orderable_for :country, single: true %>
|
100
|
+
```
|
101
|
+
|
102
|
+
## Contributing
|
103
|
+
|
104
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/code-fabrik/orderable2.
|
105
|
+
|
106
|
+
## License
|
107
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
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 = 'Orderable2'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.md')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
require 'bundler/gem_tasks'
|
23
|
+
|
data/lib/orderable2.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'orderable2/railtie' if defined?(Rails)
|
2
|
+
|
3
|
+
module Orderable2
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
def sort_order(order = nil)
|
7
|
+
calculate_order(order)
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def calculate_order(default_order = nil)
|
13
|
+
if params[:order]
|
14
|
+
params[:order].split(',').map do |stmt|
|
15
|
+
stmt_array = stmt.split(':')
|
16
|
+
{ stmt_array.first.to_sym => stmt_array.last.to_sym }
|
17
|
+
end.reduce({}, :merge)
|
18
|
+
else
|
19
|
+
default_order || { id: :asc }
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module OrderableHelper
|
2
|
+
def orderable_for(attr, options = {})
|
3
|
+
label = I18n.t("orderable.#{attr}", default: attr.to_s.humanize)
|
4
|
+
sort_params = params[:order] || ''
|
5
|
+
|
6
|
+
this_clauses, other_clauses = sort_params.split(',').partition do |sort_param|
|
7
|
+
sort_param.split(':').first == attr.to_s
|
8
|
+
end
|
9
|
+
|
10
|
+
this_clause = this_clauses.first
|
11
|
+
this_clause ||= ''
|
12
|
+
|
13
|
+
direction = this_clause.split(':').last
|
14
|
+
|
15
|
+
if direction.nil?
|
16
|
+
arrow = ''
|
17
|
+
next_dir = "#{attr}:asc"
|
18
|
+
elsif direction == 'asc'
|
19
|
+
arrow = '↑'
|
20
|
+
next_dir = "#{attr}:desc"
|
21
|
+
elsif direction == 'desc'
|
22
|
+
arrow = '↓'
|
23
|
+
next_dir = ''
|
24
|
+
else
|
25
|
+
raise StandardError, direction
|
26
|
+
end
|
27
|
+
label = label + ' ' + arrow
|
28
|
+
|
29
|
+
if options[:single]
|
30
|
+
new_order = [next_dir]
|
31
|
+
else
|
32
|
+
new_order = other_clauses + [next_dir]
|
33
|
+
end
|
34
|
+
|
35
|
+
link_to label.html_safe, request.query_parameters.merge(order: new_order.join(','))
|
36
|
+
end
|
37
|
+
end
|
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: orderable2
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Lukas_Skywalker
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-05-10 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: 5.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: 5.0.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: pg
|
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
|
+
description: Allows to set the order of AR queries using URL parameters
|
42
|
+
email:
|
43
|
+
- lukas.diener@hotmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- MIT-LICENSE
|
49
|
+
- README.md
|
50
|
+
- Rakefile
|
51
|
+
- lib/orderable2.rb
|
52
|
+
- lib/orderable2/orderable_helper.rb
|
53
|
+
- lib/orderable2/railtie.rb
|
54
|
+
- lib/orderable2/version.rb
|
55
|
+
- lib/tasks/orderable2_tasks.rake
|
56
|
+
homepage: https://code-fabrik.ch/gems/orderable2
|
57
|
+
licenses:
|
58
|
+
- MIT
|
59
|
+
metadata: {}
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
requirements: []
|
75
|
+
rubyforge_project:
|
76
|
+
rubygems_version: 2.4.8
|
77
|
+
signing_key:
|
78
|
+
specification_version: 4
|
79
|
+
summary: Let users define order rules for records
|
80
|
+
test_files: []
|
81
|
+
has_rdoc:
|