sorting_helper 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/.editorconfig +10 -0
- data/.gitignore +7 -0
- data/.rspec +2 -0
- data/Gemfile +3 -0
- data/README.md +81 -0
- data/Rakefile +6 -0
- data/lib/sorting_helper.rb +33 -0
- data/lib/sorting_helper/version.rb +3 -0
- data/sorting_helper.gemspec +28 -0
- data/spec/controllers/application_controller_spec.rb +29 -0
- data/spec/dummy/app/controllers/application_controller.rb +3 -0
- data/spec/dummy/config/application.rb +18 -0
- data/spec/dummy/config/boot.rb +5 -0
- data/spec/dummy/config/environment.rb +3 -0
- data/spec/dummy/config/routes.rb +3 -0
- data/spec/helpers/sorting_helper_spec.rb +49 -0
- data/spec/spec_helper.rb +11 -0
- metadata +153 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d0b53202a195f03e728498aa65dbb4ef679ac38e
|
4
|
+
data.tar.gz: 464a0a28652e258a2fab8f587f49c27cc7a93e48
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: daf4f07a85718c717a461dd4a389680219fef89aa41d20840a080938a2bf2f0959b7bebbbf5793d2e1ae1a0279abfc79e686339a456d444e2347349a2e688b4f
|
7
|
+
data.tar.gz: f8bf227c2fb7c4e6a2f546dc711bce072e15d5882a10f7b8f24bafcf0483472d889e4f8f55fae53e4e7e7d95bd819c5e6bf94f9e1119d9cec98e82f031689dc0
|
data/.editorconfig
ADDED
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
# SortingHelper
|
2
|
+
|
3
|
+
Rails helpers for building "sort by column" links.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'sorting_helper'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install sorting_helper
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
# app/controllers/users_controller.rb
|
25
|
+
def index
|
26
|
+
@users = User.order(sorting)
|
27
|
+
end
|
28
|
+
|
29
|
+
...
|
30
|
+
|
31
|
+
protected
|
32
|
+
|
33
|
+
def sorting
|
34
|
+
column = sorting_column
|
35
|
+
|
36
|
+
if %w[email name].include?(column)
|
37
|
+
"#{column} #{sorting_direction}"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
```
|
41
|
+
|
42
|
+
Example view:
|
43
|
+
```html
|
44
|
+
<table>
|
45
|
+
<tr>
|
46
|
+
<th><%= sorting_link :email, 'Email' %></th>
|
47
|
+
<th><%= sorting_link :name, 'Name' %></th>
|
48
|
+
</tr>
|
49
|
+
<% @users.each do |user| %>
|
50
|
+
<tr>
|
51
|
+
<td><%= user.email %></td>
|
52
|
+
<td><%= user.name %></td>
|
53
|
+
</tr>
|
54
|
+
<% end %>
|
55
|
+
</table>
|
56
|
+
```
|
57
|
+
|
58
|
+
## License
|
59
|
+
|
60
|
+
Copyright (c) 2014 Okhlopkov Anatoly
|
61
|
+
|
62
|
+
MIT License
|
63
|
+
|
64
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
65
|
+
a copy of this software and associated documentation files (the
|
66
|
+
"Software"), to deal in the Software without restriction, including
|
67
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
68
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
69
|
+
permit persons to whom the Software is furnished to do so, subject to
|
70
|
+
the following conditions:
|
71
|
+
|
72
|
+
The above copyright notice and this permission notice shall be
|
73
|
+
included in all copies or substantial portions of the Software.
|
74
|
+
|
75
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
76
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
77
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
78
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
79
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
80
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
81
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'sorting_helper/version'
|
2
|
+
require 'active_support/all'
|
3
|
+
|
4
|
+
module SortingHelper
|
5
|
+
def sorting_column
|
6
|
+
params[:sort][/^\-?(.+)/, 1] if params[:sort].present?
|
7
|
+
end
|
8
|
+
|
9
|
+
def sorting_direction
|
10
|
+
params[:sort] =~ /^\-/ ? :desc : :asc if params[:sort].present?
|
11
|
+
end
|
12
|
+
|
13
|
+
def sorting_url(column, direction = nil, &blk)
|
14
|
+
direction = sorting_direction || :asc
|
15
|
+
|
16
|
+
column = column.to_s
|
17
|
+
column.prepend('-') if direction == :asc
|
18
|
+
|
19
|
+
blk.call(url_for(sort: column), direction)
|
20
|
+
end
|
21
|
+
|
22
|
+
def sorting_link(column, label)
|
23
|
+
sorting_url(column) do |url, direction|
|
24
|
+
link = link_to(label, url)
|
25
|
+
|
26
|
+
if column.to_s == sorting_column
|
27
|
+
link += direction == :asc ? ' ▴' : ' ▾'
|
28
|
+
end
|
29
|
+
|
30
|
+
raw link
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
|
4
|
+
require 'sorting_helper/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "sorting_helper"
|
8
|
+
spec.version = SortingHelper::VERSION
|
9
|
+
spec.authors = ["Okhlopkov Anatoly"]
|
10
|
+
spec.email = ["ohlopkov0211@gmail.com"]
|
11
|
+
spec.summary = ''
|
12
|
+
spec.description = ''
|
13
|
+
spec.homepage = 'https://github.com/VortexGrenade/sorting_helper'
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency 'activesupport', '>= 3'
|
22
|
+
spec.add_dependency 'rails', '>= 4.0.0'
|
23
|
+
|
24
|
+
spec.add_development_dependency 'bundler', '~> 1'
|
25
|
+
spec.add_development_dependency 'rake', '~> 10'
|
26
|
+
spec.add_development_dependency 'rspec', '~> 3'
|
27
|
+
spec.add_development_dependency 'rspec-rails'
|
28
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'sorting_helper'
|
3
|
+
|
4
|
+
describe ApplicationController, type: [:controller, :request] do
|
5
|
+
describe '#sorting_column' do
|
6
|
+
it 'returns column name without direction prefix' do
|
7
|
+
controller.params[:sort] = '-column'
|
8
|
+
expect(controller.sorting_column).to eq('column')
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'returns nil if sort is not specified' do
|
12
|
+
expect(controller.sorting_column).to eq(nil)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe '#sorting_direction' do
|
17
|
+
it 'returns direction type based on column prefix' do
|
18
|
+
controller.params[:sort] = '-column'
|
19
|
+
expect(controller.sorting_direction).to eq(:desc)
|
20
|
+
|
21
|
+
controller.params[:sort] = 'column'
|
22
|
+
expect(controller.sorting_direction).to eq(:asc)
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'returns nil if sort is not specified' do
|
26
|
+
expect(controller.sorting_direction).to eq(nil)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require File.expand_path('../boot', __FILE__)
|
2
|
+
require 'action_controller/railtie'
|
3
|
+
Bundler.require(*Rails.groups)
|
4
|
+
require 'sorting_helper'
|
5
|
+
|
6
|
+
module Dummy
|
7
|
+
class Application < Rails::Application
|
8
|
+
config.secret_key_base = 'dummy_key'
|
9
|
+
config.session_store :cookie_store, key: '_dummy_session'
|
10
|
+
config.cache_classes = true
|
11
|
+
config.eager_load = false
|
12
|
+
config.consider_all_requests_local = true
|
13
|
+
config.action_dispatch.show_exceptions = false
|
14
|
+
config.action_controller.perform_caching = false
|
15
|
+
config.action_controller.allow_forgery_protection = false
|
16
|
+
config.active_support.deprecation = :stderr
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'sorting_helper'
|
3
|
+
|
4
|
+
describe SortingHelper, type: [:helper] do
|
5
|
+
before do
|
6
|
+
def controller.default_url_options
|
7
|
+
{ controller: :application }
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
describe '#sorting_url' do
|
12
|
+
it 'pass reversed sorting url and current direction to given block' do
|
13
|
+
controller.params[:sort] = '-column'
|
14
|
+
|
15
|
+
helper.sorting_url(:column) do |url, direction|
|
16
|
+
expect(url).to eq('/?sort=column')
|
17
|
+
expect(direction).to eq(:desc)
|
18
|
+
end
|
19
|
+
|
20
|
+
controller.params[:sort] = 'column'
|
21
|
+
|
22
|
+
helper.sorting_url(:column) do |url, direction|
|
23
|
+
expect(url).to eq('/?sort=-column')
|
24
|
+
expect(direction).to eq(:asc)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'default direction is asc' do
|
29
|
+
helper.sorting_url(:column) do |url, direction|
|
30
|
+
expect(url).to eq('/?sort=-column')
|
31
|
+
expect(direction).to eq(:asc)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe '#sorting_link' do
|
37
|
+
it 'generates html link for given column and label' do
|
38
|
+
expect(helper.sorting_link(:column, 'label')).to eq('<a href="/?sort=-column">label</a>')
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'inverts sorting for current column' do
|
42
|
+
controller.params[:sort] = 'column'
|
43
|
+
expect(helper.sorting_link(:column, 'label')).to eq('<a href="/?sort=-column">label</a> ▴')
|
44
|
+
|
45
|
+
controller.params[:sort] = '-column'
|
46
|
+
expect(helper.sorting_link(:column, 'label')).to eq('<a href="/?sort=column">label</a> ▾')
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
ENV['RAILS_ENV'] = 'test'
|
2
|
+
|
3
|
+
require File.expand_path('../dummy/config/environment.rb', __FILE__)
|
4
|
+
require 'rspec/rails'
|
5
|
+
|
6
|
+
require 'dummy/app/controllers/application_controller'
|
7
|
+
require 'dummy/config/routes'
|
8
|
+
|
9
|
+
RSpec.configure do |config|
|
10
|
+
config.order = 'random'
|
11
|
+
end
|
metadata
ADDED
@@ -0,0 +1,153 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sorting_helper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Okhlopkov Anatoly
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-11-09 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: '3'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rails
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 4.0.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 4.0.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec-rails
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: ''
|
98
|
+
email:
|
99
|
+
- ohlopkov0211@gmail.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".editorconfig"
|
105
|
+
- ".gitignore"
|
106
|
+
- ".rspec"
|
107
|
+
- Gemfile
|
108
|
+
- README.md
|
109
|
+
- Rakefile
|
110
|
+
- lib/sorting_helper.rb
|
111
|
+
- lib/sorting_helper/version.rb
|
112
|
+
- sorting_helper.gemspec
|
113
|
+
- spec/controllers/application_controller_spec.rb
|
114
|
+
- spec/dummy/app/controllers/application_controller.rb
|
115
|
+
- spec/dummy/config/application.rb
|
116
|
+
- spec/dummy/config/boot.rb
|
117
|
+
- spec/dummy/config/environment.rb
|
118
|
+
- spec/dummy/config/routes.rb
|
119
|
+
- spec/helpers/sorting_helper_spec.rb
|
120
|
+
- spec/spec_helper.rb
|
121
|
+
homepage: https://github.com/VortexGrenade/sorting_helper
|
122
|
+
licenses:
|
123
|
+
- MIT
|
124
|
+
metadata: {}
|
125
|
+
post_install_message:
|
126
|
+
rdoc_options: []
|
127
|
+
require_paths:
|
128
|
+
- lib
|
129
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
130
|
+
requirements:
|
131
|
+
- - ">="
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
requirements: []
|
140
|
+
rubyforge_project:
|
141
|
+
rubygems_version: 2.4.2
|
142
|
+
signing_key:
|
143
|
+
specification_version: 4
|
144
|
+
summary: ''
|
145
|
+
test_files:
|
146
|
+
- spec/controllers/application_controller_spec.rb
|
147
|
+
- spec/dummy/app/controllers/application_controller.rb
|
148
|
+
- spec/dummy/config/application.rb
|
149
|
+
- spec/dummy/config/boot.rb
|
150
|
+
- spec/dummy/config/environment.rb
|
151
|
+
- spec/dummy/config/routes.rb
|
152
|
+
- spec/helpers/sorting_helper_spec.rb
|
153
|
+
- spec/spec_helper.rb
|