sortabl 0.5.1 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bc5b7d426b26777c5cbe848607372226a88e9373
4
- data.tar.gz: 09680458a7901a7b4a1bfc6210fa67a23e7ac927
3
+ metadata.gz: 6552e6114146c0cc491795f78cd3f0f1ac2aa6eb
4
+ data.tar.gz: 8e7e1d4f7152d281b8dac36e425d6fd638ead641
5
5
  SHA512:
6
- metadata.gz: 38d58995baff555155b23b02b5020037fe92d15cd322b04731e2202b4f0801616bcd7da4872226b945b2fab012ef1fbc713913b3c60fb0157a1799c57d9fb8f8
7
- data.tar.gz: c4373fd8cffee63ee7029ad0f18031f93cfd510a5b1af26962a0dd8de578dda095033c71a41f559b21f5245a91f6ca097126cd73f1d7aed8b3ec910115c57cf1
6
+ metadata.gz: 1b9e88c7c059746cbd2a658cff301d44ec16f4c67591e1bcff19f2d7d569518cb5bfefc94b5dada03e9d440a037fdec4d095cb0ac18100e33c9098b0593f1345
7
+ data.tar.gz: 6fe778b7169244eaf6da36650c82be5b50f2b19c7adfc365f583047f9f98cff0cf4300fbf3a5b6b02ad67d20c0686999ad6481d132a56fcf4369b6530926a6fe
data/README.md ADDED
@@ -0,0 +1,109 @@
1
+ # Sortabl
2
+
3
+ A Rails Plugin to simplify sorting tables.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'sortabl'
11
+ ```
12
+
13
+ ## Basic Usage
14
+
15
+ ### Controller
16
+
17
+ #### Default
18
+
19
+ ```ruby
20
+ @posts = Post.sortabl(params[:sortabl])
21
+ ```
22
+
23
+ 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 as default, you can set another one by:
24
+
25
+ ```ruby
26
+ @posts = Post.sortabl(params[:sortabl], default: [author: :asc])
27
+ ```
28
+
29
+ Even default multiple columns sort is possible:
30
+
31
+ ```ruby
32
+ @posts = Post.sortabl(params[:sortabl], default: [author: :asc, created_at: :desc])
33
+ ```
34
+
35
+ #### Limeted Attributes
36
+
37
+ Permitted values can be an attribute of model class, followed by `_asc` or `_desc`. For example: `sort: :author_asc`
38
+ 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:
39
+
40
+ ```ruby
41
+ @posts = Post.sortabl(params[:sortabl], only: [:title, :author])
42
+ # or
43
+ @posts = Post.sortabl(params[:sortabl], except: [:text])
44
+ ```
45
+
46
+ #### Sort by associations
47
+
48
+
49
+
50
+ ### View
51
+
52
+ #### Link Helper
53
+
54
+ There's also a link helper, which generates needed urls. It works just like you would expect from rails default link_to helper. But instead of hand over a path, just place the desired column name. The helper will do the rest for you:
55
+
56
+ ```erb
57
+ <table>
58
+ <thead>
59
+ <th><%= sortabl_link 'Author', :author, id: 'author-column', class: 'author-column' %></th>
60
+ </thead>
61
+ <tbody>
62
+ ...
63
+ </tbody>
64
+ </table>
65
+
66
+ # Will be rendered to:
67
+ <table>
68
+ <thead>
69
+ <th><a id="author-column" class="sortabl author-column" href="/posts?sortabl=author_asc">Author</a></th>
70
+ # or
71
+ <th><a id="author-column" class="sortabl asc author-column" href="/posts?sortabl=author_desc">Author</a></th>
72
+ # or
73
+ <th><a id="author-column" class="sortabl desc author-column" href="/posts">Author</a></th>
74
+ </thead>
75
+ <tbody>
76
+ ...
77
+ </tbody>
78
+ </table>
79
+ ```
80
+
81
+ This link helper can be placed anywhere in the view. There is no need, that it has to be placed in table head. Furthermore you can use `sortabl_link` as block too.
82
+
83
+ ```erb
84
+ <%= sortabl_link :author, id: 'author-column', class: 'author-column' do %>
85
+ <p>Author</p>
86
+ <% end %>
87
+ ```
88
+
89
+ #### Parameter
90
+
91
+ By default, `sortabl_link` will generate `:sortabl` as parameter into the url. If you need to change name of the parameter, you can do so by simply:
92
+
93
+ ```ruby
94
+ # In view
95
+ <%= sortabl_link 'Author', :author, sort_param: :my_custom_sort_param %>
96
+
97
+ # In controller
98
+ @posts = Post.sortabl(params[:my_custom_sort_param], only: [:title, :author])
99
+ ```
100
+
101
+ Which hyperlink will be rendered, depends on permitted values.
102
+ Gem works also fine with pagination like [will_paginate](https://github.com/mislav/will_paginate).
103
+
104
+ Happy sorting ;)
105
+
106
+ ## License
107
+
108
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
109
+
data/lib/sortabl.rb ADDED
@@ -0,0 +1,9 @@
1
+ require "sortabl/version"
2
+ require "sortabl/sortabl_core"
3
+ require "sortabl/sortabl_helper"
4
+
5
+ # Extend ActiveRecord's functionality
6
+ ActiveRecord::Base.send :include, Sortabl::ActiveRecordExtensions::Sortabl
7
+
8
+ # Extend ActionViews's functionality
9
+ ActionView::Base.send :include, Sortabl::ActionViewExtensions::SortablHelper
@@ -0,0 +1,53 @@
1
+ module Sortabl
2
+ module ActiveRecordExtensions
3
+ module Sortabl
4
+
5
+ extend ActiveSupport::Concern
6
+
7
+ module ClassMethods
8
+
9
+ def sortabl parameter, *args
10
+
11
+ # Init
12
+ unless args.empty?
13
+ default = args[0][:default]
14
+ only = args[0][:only]
15
+ except = args[0][:except]
16
+ end
17
+
18
+ raise ArgumentError.new("Do not use 'only' and 'except' together!") if only.present? and except.present?
19
+
20
+ # Set default order attribute
21
+ # default:
22
+ order_by_default = default.present? ? default : self.primary_key
23
+
24
+ # Extract column name and direction from parameter
25
+ if parameter.present?
26
+ column_name = parameter.to_s.gsub(/(_asc$|_desc$)/, '')
27
+ direction = parameter.to_s.gsub(/^((?!desc$|asc$).)*/, '')
28
+
29
+ # Sort by default if column_name is not included in only or column_name is included in except
30
+ return order((order_by_default)) if only.present? and !only.include? column_name.to_sym
31
+ return order((order_by_default)) if except.present? and except.include? column_name.to_sym
32
+ end
33
+
34
+ if column_name.present? and direction.present?
35
+ if column_name.include? "."
36
+ # Sort by associated column
37
+ # Note: alias attributes not supported yet
38
+ sort_column = "#{column_name} #{direction}"
39
+ else
40
+ # Convert param_value to symbol to support alias attributes
41
+ sort_column = { column_name.to_sym => direction.to_sym }
42
+ end
43
+ end
44
+
45
+ # Order class object
46
+ order((sort_column or order_by_default))
47
+ end
48
+
49
+ end
50
+
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,45 @@
1
+ module Sortabl
2
+ module ActionViewExtensions
3
+ module SortablHelper
4
+
5
+ # Renders sortabl link
6
+ #
7
+ def sortabl_link(name = nil, attribute = nil, html_options = nil, &block)
8
+ html_options, attribute, name = attribute, name, block if block_given?
9
+ html_options = html_options || {}
10
+
11
+ # Support custom sort_param
12
+ # If sort_param isn't given, fall back to :sortabl as param
13
+ sort_param = html_options[:sort_param] || :sortabl
14
+ html_options.except!(:sort_param)
15
+
16
+ # Remove current sortabl param from url and add default sortabl param
17
+ sortabl_params = params.permit!.except(sort_param)
18
+ sortabl_params[sort_param] = "#{attribute}_asc"
19
+
20
+ # If there was already a sortabl param, invert direction or remove sortabl param
21
+ if params[sort_param].present?
22
+ if attribute.to_s == params[sort_param].gsub(/(_asc$|_desc$)/, '')
23
+ sort_direction = params[sort_param].gsub(/^((?!desc$|asc$).)*/, '')
24
+
25
+ sortabl_params[sort_param] = "#{attribute}_desc" if sort_direction == 'asc'
26
+ sortabl_params[sort_param] = nil if sort_direction == 'desc'
27
+ end
28
+ end
29
+
30
+ # Add sortabl html class to html_options
31
+ html_options[:class] = 'sortabl' + (sort_direction.present? ? " #{sort_direction}" : "") + (html_options[:class].present? ? " #{html_options[:class]}" : "")
32
+
33
+ # Support remote true
34
+ html_options.except!(:remote).merge!({'data-remote': 'true'}) if html_options[:remote]
35
+
36
+ # Generate url from params
37
+ url = url_for(sortabl_params)
38
+ html_options["href".freeze] ||= url
39
+
40
+ content_tag("a".freeze, name || url, html_options, &block)
41
+ end
42
+
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,3 @@
1
+ module Sortabl
2
+ VERSION = "0.5.2"
3
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sortabl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Frederic Walch
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-29 00:00:00.000000000 Z
11
+ date: 2017-04-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -74,42 +74,42 @@ dependencies:
74
74
  name: bundler
75
75
  requirement: !ruby/object:Gem::Requirement
76
76
  requirements:
77
- - - "~>"
77
+ - - ">="
78
78
  - !ruby/object:Gem::Version
79
79
  version: '1.11'
80
80
  type: :development
81
81
  prerelease: false
82
82
  version_requirements: !ruby/object:Gem::Requirement
83
83
  requirements:
84
- - - "~>"
84
+ - - ">="
85
85
  - !ruby/object:Gem::Version
86
86
  version: '1.11'
87
87
  - !ruby/object:Gem::Dependency
88
88
  name: rake
89
89
  requirement: !ruby/object:Gem::Requirement
90
90
  requirements:
91
- - - "~>"
91
+ - - ">="
92
92
  - !ruby/object:Gem::Version
93
93
  version: '10.0'
94
94
  type: :development
95
95
  prerelease: false
96
96
  version_requirements: !ruby/object:Gem::Requirement
97
97
  requirements:
98
- - - "~>"
98
+ - - ">="
99
99
  - !ruby/object:Gem::Version
100
100
  version: '10.0'
101
101
  - !ruby/object:Gem::Dependency
102
102
  name: rspec
103
103
  requirement: !ruby/object:Gem::Requirement
104
104
  requirements:
105
- - - "~>"
105
+ - - ">="
106
106
  - !ruby/object:Gem::Version
107
107
  version: '3.0'
108
108
  type: :development
109
109
  prerelease: false
110
110
  version_requirements: !ruby/object:Gem::Requirement
111
111
  requirements:
112
- - - "~>"
112
+ - - ">="
113
113
  - !ruby/object:Gem::Version
114
114
  version: '3.0'
115
115
  description: Check github repository for more in-depth information.
@@ -118,7 +118,12 @@ email:
118
118
  executables: []
119
119
  extensions: []
120
120
  extra_rdoc_files: []
121
- files: []
121
+ files:
122
+ - README.md
123
+ - lib/sortabl.rb
124
+ - lib/sortabl/sortabl_core.rb
125
+ - lib/sortabl/sortabl_helper.rb
126
+ - lib/sortabl/version.rb
122
127
  homepage: https://github.com/freder1c/sortabl
123
128
  licenses:
124
129
  - MIT
@@ -139,7 +144,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
139
144
  version: '0'
140
145
  requirements: []
141
146
  rubyforge_project:
142
- rubygems_version: 2.5.1
147
+ rubygems_version: 2.6.11
143
148
  signing_key:
144
149
  specification_version: 4
145
150
  summary: Active Record plugin which helps and simplify sorting tables.