sortabl 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d1490175b83dc4c1ecf163ed9d46d0d07a9f4f08
4
- data.tar.gz: 5320c74587adf23634d65b16b89cb600e6d03f82
3
+ metadata.gz: 2435ba6e0ee3cef2d9d22e2fd81bb2191ed60a70
4
+ data.tar.gz: e75b0bc90d7932c4f0d91db505f5db7049eb33f1
5
5
  SHA512:
6
- metadata.gz: 01c673da47ab2844dd0ad60fb7feaae55f6e50d8cdb1c3c0d4c84e920311b8dc6346a17c59e92453b44d43e3bfa8ba9c27c5aafe2b509b9109e0bdbb89d93835
7
- data.tar.gz: b0ce7e421845e877429781953677cfb8890aa68d02ff174c32051b5392c9d4389130012edb0810f5ef8e712020f77c0e12553363d8d1ffc4e11dbececf62d170
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[:sort])
18
+ @posts = Post.sortabl(sort_by: params[:sortabl])
19
19
  ```
20
20
 
21
- And that's it! Records will be sorted by permitted parameter `:sort`. If parameter `:sort` 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:
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[:sort], default: :author)
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[:sort], only: [:title, :author])
31
+ @posts = Post.sortabl(sort_by: params[:sortabl], only: [:title, :author])
32
32
  # or
33
- @posts = Post.sortabl(sort_by: params[:sort], except: [:text])
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?sort=author_asc">Author<i class="fa fa-sort"></i></a></th>
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?sort=author_desc">Author<i class="fa fa-sort-asc"></i></a></th>
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(:sort)
17
+ sort_params = params.except(sort_by)
14
18
 
15
19
  # Set default sort path
16
- sort_params[:sort] = "#{attribute}_asc"
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[:sort].present?
23
+ if params[sort_by].present?
20
24
 
21
25
  # if sort param match current attribute, change direction
22
- if attribute.to_s == params[:sort].gsub(/(_asc$|_desc$)/, '')
26
+ if attribute.to_s == params[sort_by].gsub(/(_asc$|_desc$)/, '')
23
27
 
24
- sort_direction = params[:sort].gsub(/^((?!desc$|asc$).)*/, '')
28
+ sort_direction = params[sort_by].gsub(/^((?!desc$|asc$).)*/, '')
25
29
 
26
- sort_params[:sort] = "#{attribute}_desc" if sort_direction == 'asc'
27
- sort_params[:sort] = nil if sort_direction == 'desc'
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 id="#{args[0][:id] if args.present?}" class="#{args[0][:class] if args.present?}" #{args[0].map{ |k,v| "#{k}='#{v}'" unless (k.to_s =~ /^data-(.*)$/).nil? }.join(' ') if args.present?}>
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
@@ -1,3 +1,3 @@
1
1
  module Sortabl
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sortabl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Frederic Walch