sortabl 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +20 -8
- data/lib/sortabl/sortabl_helper.rb +16 -8
- data/lib/sortabl/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2435ba6e0ee3cef2d9d22e2fd81bb2191ed60a70
|
4
|
+
data.tar.gz: e75b0bc90d7932c4f0d91db505f5db7049eb33f1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e7fedc22f9251162e414449b262ca14920dbae3ea2c2f0aea0964cd2935d9083391cfe9db2ea5f805b12f05701aeb3fdb76de5b26fe541a05ec6a336fb24250a
|
7
|
+
data.tar.gz: aea008d52329c6fd6088babe387c7551a8c1c95fda8ecfc3f0fd819ebb640d4cc52f0615a74223fedef752109ff7971935859383a8455da1f47846cdf9fdef6b
|
data/README.md
CHANGED
@@ -15,22 +15,22 @@ gem 'sortabl'
|
|
15
15
|
### Controller
|
16
16
|
|
17
17
|
```ruby
|
18
|
-
@posts = Post.sortabl(sort_by: params[:
|
18
|
+
@posts = Post.sortabl(sort_by: params[:sortabl])
|
19
19
|
```
|
20
20
|
|
21
|
-
And that's it! Records will be sorted by permitted parameter `:
|
21
|
+
And that's it! Records will be sorted by permitted parameter `:sortabl`. If parameter `:sortabl` isn't permitted, records will be sorted by primary key. If you don't want to sort by primary key by default, you can set another one by:
|
22
22
|
|
23
23
|
```ruby
|
24
|
-
@posts = Post.sortabl(sort_by: params[:
|
24
|
+
@posts = Post.sortabl(sort_by: params[:sortabl], default: :author)
|
25
25
|
```
|
26
26
|
|
27
27
|
Permitted values can be an attribute of model class, followed by `_asc` or `_desc`. For example: `sort: :author_asc`
|
28
28
|
If there's an attribute permitted which doesn't exist in model, it falls back to sort by `default` key. Attributes can be limited with `only` and `except`. For example:
|
29
29
|
|
30
30
|
```ruby
|
31
|
-
@posts = Post.sortabl(sort_by: params[:
|
31
|
+
@posts = Post.sortabl(sort_by: params[:sortabl], only: [:title, :author])
|
32
32
|
# or
|
33
|
-
@posts = Post.sortabl(sort_by: params[:
|
33
|
+
@posts = Post.sortabl(sort_by: params[:sortabl], except: [:text])
|
34
34
|
```
|
35
35
|
|
36
36
|
|
@@ -51,11 +51,11 @@ There's also a view helper for rendering table heads:
|
|
51
51
|
# Will be rendered to:
|
52
52
|
<table>
|
53
53
|
<thead>
|
54
|
-
<th id="author-column" class="author-column"><a href="/posts?
|
54
|
+
<th id="author-column" class="sortabl author-column"><a href="/posts?sortabl=author_asc">Author<i class="fa fa-sort"></i></a></th>
|
55
55
|
# or
|
56
|
-
<th id="author-column" class="author-column"><a href="/posts?
|
56
|
+
<th id="author-column" class="sortabl asc author-column"><a href="/posts?sortabl=author_desc">Author<i class="fa fa-sort-asc"></i></a></th>
|
57
57
|
# or
|
58
|
-
<th id="author-column" class="author-column"><a href="/posts">Author<i class="fa fa-sort-desc"></i></a></th>
|
58
|
+
<th id="author-column" class="sortabl desc author-column"><a href="/posts">Author<i class="fa fa-sort-desc"></i></a></th>
|
59
59
|
</thead>
|
60
60
|
<tbody>
|
61
61
|
...
|
@@ -63,6 +63,18 @@ There's also a view helper for rendering table heads:
|
|
63
63
|
</table>
|
64
64
|
```
|
65
65
|
|
66
|
+
#### Parameter
|
67
|
+
|
68
|
+
By default, the ViewHelper will generate `:sortabl` as parameter. If you need to change name of the parameter, you can do so by simply:
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
# In view
|
72
|
+
<%= sortabl_th 'Author', :author, id: 'author-column', class: 'author-column', sort_param: :my_custom_sort_param %>
|
73
|
+
|
74
|
+
# In controller
|
75
|
+
@posts = Post.sortabl(sort_by: params[:my_custom_sort_param], only: [:title, :author])
|
76
|
+
```
|
77
|
+
|
66
78
|
Which hyperlink will be rendered, depends on permitted values.
|
67
79
|
For now, this gem uses [font-awesome-rails](https://github.com/bokmann/font-awesome-rails) for placing icons. So you may want to install font-awesome gem to your project as well. More icon libaries will be supported in future.
|
68
80
|
Gem works also fine with pagination like [will_paginate](https://github.com/mislav/will_paginate).
|
@@ -8,30 +8,38 @@ module Sortabl
|
|
8
8
|
#
|
9
9
|
def sortabl_th name, attribute, *args
|
10
10
|
|
11
|
+
# Support custom sort_param
|
12
|
+
# If sort_param isn't given, fall back to :sortabl as param
|
13
|
+
sort_by = args[0][:sort_param] || :sortabl
|
14
|
+
|
11
15
|
# To provide full path with all given params
|
12
16
|
# make a copy of incoming params and override sort_param only
|
13
|
-
sort_params = params.except(
|
17
|
+
sort_params = params.except(sort_by)
|
14
18
|
|
15
19
|
# Set default sort path
|
16
|
-
sort_params[
|
20
|
+
sort_params[sort_by] = "#{attribute}_asc"
|
17
21
|
|
18
22
|
# if there is already a sort param set, invert or remove sort direction
|
19
|
-
if params[
|
23
|
+
if params[sort_by].present?
|
20
24
|
|
21
25
|
# if sort param match current attribute, change direction
|
22
|
-
if attribute.to_s == params[
|
26
|
+
if attribute.to_s == params[sort_by].gsub(/(_asc$|_desc$)/, '')
|
23
27
|
|
24
|
-
sort_direction = params[
|
28
|
+
sort_direction = params[sort_by].gsub(/^((?!desc$|asc$).)*/, '')
|
25
29
|
|
26
|
-
sort_params[
|
27
|
-
sort_params[
|
30
|
+
sort_params[sort_by] = "#{attribute}_desc" if sort_direction == 'asc'
|
31
|
+
sort_params[sort_by] = nil if sort_direction == 'desc'
|
28
32
|
|
29
33
|
end
|
30
34
|
end
|
31
35
|
|
36
|
+
# Get element id and classes
|
37
|
+
th_id = args[0][:id] if args.present? and args[0][:id].present?
|
38
|
+
th_class = args[0][:class] if args.present? and args[0][:class].present?
|
39
|
+
|
32
40
|
# Generate HTML Code
|
33
41
|
html = <<-HTML
|
34
|
-
<th
|
42
|
+
<th #{"id=#{th_id}" if th_id.present?} class="sortabl#{sort_direction.present? ? " #{sort_direction}" : ""}#{" #{th_class}" if th_class.present?}" #{args[0].map{ |k,v| "#{k}='#{v}'" unless (k.to_s =~ /^data-(.*)$/).nil? }.join(' ') if args.present?}>
|
35
43
|
<a href='#{url_for(sort_params)}'>#{name}<i class='fa fa-sort#{sort_direction.present? ? "-#{sort_direction}" : ""}'></i></a>
|
36
44
|
</td>
|
37
45
|
HTML
|
data/lib/sortabl/version.rb
CHANGED